当前位置:网站首页>C language: sanziqi
C language: sanziqi
2022-06-13 09:12:00 【Caixinzhi】
List of articles
Preface
As for C A deep understanding of language , In my spare time , Just write a program to enrich yourself , Although it can not include all the knowledge points learned , But there will be other procedures later .
Code
The following is the main body of this article , The following cases can be used for reference
This procedure is conducted in VS Implemented in environment , It is divided into three files ( A header file game.h And two source files test.c&game.c), The following is the main introduction test.c The contents of the document ( That is, the overall framework ) and game.c Implementation method in the file .
game.c( The code explanation is in the comments below ):
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void Initboard(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
// Initialize chessboard , For example, a three row and three column matrix will be assigned an empty string .
void Displayboard(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
{
printf("|");
}
}
}
printf("\n");
}
}
// Print out the chessboard ( visualization ), Here we need to make a partition map
void player_move(char board[ROW][COL], int row, int col)
{
printf(" Players play chess :");
int x = 0;
int y = 0;
while (1)
{
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf(" The location is already occupied , Please re-enter :");
}
}
else
{
printf(" It's illegal to enter coordinates , Please re-enter :");
}
}
}
// Judge whether players play chess regularly , Specification will break skip , If it is not standardized, it will pass while Cycle control re-enter
void computer_move(char board[ROW][COL], int row, int col)
{
printf(" The computer plays chess :\n");
int x = 1;
int y = 1;
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
// Computer chess is done by generating random coordinates , At the same time, you should also judge that the chess position is an empty string to play chess
int is_full(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
// Judge whether the chessboard is full , Yes, go back to 1, If not, return to 0
char is_win(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
{
return board[i][1];
}
}
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
{
return board[1][i];
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
{
return board[1][1];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
{
return board[1][1];
}
if (is_full(board, row, col) == 1)
{
return 'Q';
}
return 'C';
}
//is_win This function is also a flexible function in the whole project ( This function is not optimal , It can only be used in Sanzi chess , It can be modified according to the actual situation ). This function judges the horizontal 、 longitudinal 、 Whether there are three pieces connected into a line , If yes, it will return the string of the completed chess piece input ; If the board is full and no one wins , Then return to Q; If not, return C.
test.c:
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
printf("***************************\n");
printf("******* 1. play *******\n");
printf("******* 0. exit *******\n");
printf("***************************\n");
}
void game()
{
char board[ROW][COL] = { 0 };
Initboard(board, ROW, COL);// Initialize chessboard
Displayboard(board, ROW, COL);// Print chessboard
char ret = 0;
while (1)
{
player_move(board, ROW, COL);// Players play chess
Displayboard(board, ROW, COL);// Print chessboard
ret = is_win(board, ROW, COL);// Judgement of winning or losing
if (ret != 'C')
{
break;
}
computer_move(board, ROW, COL);// The computer plays chess
Displayboard(board, ROW, COL);// Print chessboard
ret = is_win(board, ROW, COL);// Judgement of winning or losing
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf(" Game player wins \n");
}
else if (ret == '#')
{
printf(" Computers win \n");
}
else
{
printf(" It ends in a draw \n");
}
}
void test()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf(" Please enter :");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf(" Quit the game \n");
break;
default:
printf(" Wrong choice \n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
stay game Function , First initialize and print the chessboard , Then use while Loop to repeat every time the player and the computer play chess 、 Print and determine if the chessboard has been filled , If no one wins or the board is filled ,is_win The function returns C,while It's going to go on and on . When one side wins or the board is filled , The corresponding string will be returned , Further judgment is made by the returned string .
It is worth noting that , Because computer chess uses random coordinates , Random values need to be called , So you need to write in at the beginning srand((unsigned int)time(NULL)) This code , At the same time, you need to import header files <time.h> and <stdlib.h>.
game.h:
#pragma once
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define ROW 3
#define COL 3
// Initialize chessboard
void Initboard(char board[ROW][COL], int row, int col);
// Print chessboard
void Displayboard(char board[ROW][COL], int row, int col);
// Players play chess
void player_move(char board[ROW][COL], int row, int col);
// The computer plays chess
void computer_move(char board[ROW][COL], int row, int col);
char is_win(char board[ROW][COL], int row, int col);
The complete code of the whole Sanzi chess is put here , You can have a look if you are interested : Cai Xinzhi /C - C++ project - Gitee.comhttps://gitee.com/zkcxz/c-----c---project/tree/master/game1 Extension : You can modify the number of rows and columns as required 、 Rules and so on .
summary
That's what we're going to talk about today , This program only uses a large number of function call arrays , Not much else , Other knowledge points will be summarized later . Next , Make a brief summary of what you have learned recently : In the past few months , To study the C The basic grammar of language 、 Data storage 、 Pointer related knowledge points 、 String and character functions 、 Custom type 、 Dynamic memory management , The most difficult part is the pointer , There will be an address book program to consolidate , In the future, I plan to sort out the remaining blogs , Learn the next C Language file operation 、 Program environment and pretreatment , Recently, I still read a book I bought before 《C Modern methods of language programming ( The second edition )》, Try to consolidate C Basics , For later learning Java To prepare for .
边栏推荐
猜你喜欢
Cisco, Huawei network equipment
Neo4j - CQL使用
Mttr/mttf/mtbf diagram
202012 CCF test questions
transforms. ColorJitter(0.3, 0, 0, 0)
What exactly is Huawei cloud desktop saying when it says "smooth"?
【安全】零基础如何从0到1逆袭成为安全工程师
20220606 Young's inequality for Matrices
Qvector shallow copy performance test
教程篇(5.0) 01. 产品简介及安装 * FortiEDR * Fortinet 网络安全专家 NSE 5
随机推荐
图数据库Neo4j介绍
QML(06)——qml. Add a new folder under QRC
Tutorial (5.0) 01 Product introduction and installation * fortiedr * Fortinet network security expert NSE 5
Onnx crop intermediate node
教程篇(5.0) 02. 管理 * FortiEDR * Fortinet 网络安全专家 NSE 5
20211108 observable, controllable, stable and measurable
Exporting MySQL data table documents using Navicat
What are the bank financial products? How long is the liquidation period?
【网络安全渗透】如果你还不懂CSRF?这一篇让你彻底掌握
C/S模型与P2P模型
CAS NO lock
20211104 why are the traces of similar matrices the same
20220606 Young's inequality for Matrices
Number of parameters of pytorch statistical model
20211020 academician all drive system
QObject::connect: Cannot queue arguments of type ‘QTextCursor‘ (Make sure ‘QTextCursor‘ is registere
20211006 线性变换
output. Interpretation of topk() function
Tutorial (5.0) 04 Fortint cloud services and scripts * fortiedr * Fortinet network security expert NSE 5
20211104 为什么矩阵的迹等于特征值之和,为什么矩阵的行列式等于特征值之积