It is divided into three parts , Easier to learn .

test.c Test the logic of the game
// Test the logic of sanziqi #include "game.h" void menu() { printf("********************\n");
printf("**** 1.play ****\n"); printf("**** 0.exit ****\n");
printf("********************\n"); } void game() { char ret = 0; // Store chess playing data char
board[ROW][COL] = { 0 }; // Initialize chessboard to all spaces InitBoard(board, ROW, COL); // Print chessboard
DisplayBoard(board, ROW, COL); while (1) { // Players play chess player_move(board, ROW, COL);
DisplayBoard(board, ROW, COL); // Judge the winner or loser ret = is_win(board, ROW, COL); if (ret !=
'C') { break; } // Computer chess computer_move(board, ROW, COL);// Play chess at random
DisplayBoard(board, ROW, COL); ret = is_win(board, ROW, COL); if (ret != 'C') {
break; } } if (ret == '*') { printf(" Player wins \n"); } else if (ret == '#') {
printf(" Computer wins \n"); } else if(ret=='Q') { printf(" it ends in a draw \n"); } //DisplayBoard(board,
ROW, COL); } // When does the game end // Player wins -* // Computer wins -# // it ends in a draw -Q // continue -C void test() { int input
= 0; srand((unsigned int)time(NULL)); do { menu(); printf(" Please select ："); scanf("%d",
&input); switch (input) { case 1: game();// game break; case 0: printf(" Exit the game \n");
break; default: printf(" Selection error \n"); break; } } while (input); } int main() {
test(); return 0; }

game.c Game implementation
#include "game.h" void InitBoard(char board[ROW][COL], int row, int col) { int
i = 0; for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) {
board[i][j] = ' '; } } } //void DisplayBoard(char board[ROW][COL], int row, int
col) //{ // int i = 0; // for (i = 0; i < row;i++) // { // int j = 0; // for (j
= 0; j < col;j++) // { // printf("%c", board[i][j]); // } // printf("\n"); // }
//} void DisplayBoard(char board[ROW][COL], int row, int col) { int i = 0; for
(i = 0; i < row; i++) { // print data //printf(" %c | %c | %c
\n",board[i][0],board[i][1],board[i][2]); int j = 0; for (j = 0; j < col; j++)
{ printf(" %c ", board[i][j]); if (j<col-1) printf("|"); } printf("\n");
// Print split lines if (i < row - 1) //printf("---|---|---\n"); { for (j = 0; j < col; j++)
{ printf("---"); if (j < col - 1) printf("|"); } printf("\n"); } } } void
player_move(char board[ROW][COL], int row, int col) { int x = 0; int y = 0;
printf(" Players play chess \n"); while (1) { printf(" Please enter coordinates ："); scanf("%d %d", &x, &y); if (x
>= 1 && x <= row && y >= 1 && y <= col) { // play chess if (board[x - 1][y - 1] == ' ')
{ board[x - 1][y - 1] = '*'; break; } else { printf(" This coordinate is occupied , Please re-enter \n"); } }
else { printf(" This coordinate is occupied , Please re-enter \n"); } } } void computer_move(char
board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf(" Computer chess \n");
while (1) { x = rand() % row;//0~2 y = rand() % col;//0~2 if (board[x][y] == '
') { board[x][y] = '#'; break; } } } static int if_full(char board[ROW][COL],
int row, int col) { int i = 0; for (i = 0; i < row; i++) { int j = 0; for (j =
0; j < col; j++) { if (board[i][j] == ' ') { return 0;// Not full } } } return 1;// Full
} char is_win(char board[ROW][COL], int row, int col) { int i = 0; // Judgment line 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]; } } // Judgment column for (i = 0;
i < row; i++) { if (board[0][i] == board[1][i] && board[1][i] == board[2][i] &&
board[1][i] != ' ') { return board[1][i]; } } // diagonal 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]; } // Judge a draw if (if_full(board, row, col)
== 1) { return 'Q'; } // continue return 'C'; }

game.h Function declaration , Definition of symbols
#pragma once #include <stdio.h> #include <stdlib.h> #include <time.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); // Computer chess void
computer_move(char board[ROW][COL], int row, int col); // Judge the winner or loser char is_win(char
board[ROW][COL], int row, int col);

Technology
Daily Recommendation