How to develop and realize tic-tac-toe chess in C language
This article mainly shows you "how to develop and realize tic-tac-toe chess in C language". The content is simple and easy to understand, and the organization is clear. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn this article "how to develop and realize tic-tac-toe chess in C language".
general idea
Tic tac toe board we can generally operate as a two-dimensional array, we need to initialize the two-dimensional array, print the board, players play chess, computer chess, judge the winning or losing code, etc.
the creation of project
We created header files for function declarations, game.c files for function implementations, and test.c files for tests.
test results
Computer wins:
Player wins:
Draw:
Implementation of each function code
We implement the code for function functionality in the game.c file
void InitBoard(char Board[ROW][COL], int row, int col){ for (int i = 0; i
< row; i++) { for (int j = 0; j < col; j++) { Board[i][j] = ' '; } }}打印棋盘void DisplayBoard(char Board[ROW][COL], int row, int col){ for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { printf(" %c ", Board[i][j]); if (j < col - 1) printf("|"); } printf("\n"); if (i < row - 1) { for (int j = 0; j < col; j++) { printf("---"); if (j < col - 1) printf("|"); } } printf("\n"); }} 这里的棋盘我们只打印了井字的形状,如果为了美观,还可以进行封边,这里就留给朋友们自行实现啦。 玩家下棋void PlayMove(char Board[ROW][COL], int row, int col){ while (1) { int x = 0, y = 0; printf("请输入坐标:\n"); scanf("%d%d", &x, &y); if (x >= 1 && x = 1 && y