The code enters the main function, executes the test function, then executes the menu function to print the menu, and then enters the switch according to the data entered by the player to determine whether to execute the game function or exit the game.
Entering the game function, how to initialize the chessboard, how to output the format of the chessboard, how to let the player walk the chessboard, how to let the computer walk the chessboard, how to judge the win or loss, and how to judge the draw are roughly explained in the function statement at the beginning, which is easy to understand by self reading.
#include<stdio.h> #include<time.h> #include<stdlib.h> #define ROW 3 #define COL 3 //Function declaration void Initboard(char board[ROW][COL],int row,int col);//Initialize chessboard void Displayboard(char board[ROW][COL], int row, int col);//How to output chessboard format void PlayerMove(char board[ROW][COL], int row, int col);//Player go void ComputerMove(char board[ROW][COL], int row, int col);//Computer go char Iswin(char board[ROW][COL], int row, int col);//Judgement of winning or losing int IsFull(char board[ROW][COL], int row, int col);//Is it a draw? void menu() { printf("**************************************\n"); printf("****** Enter 1 to start the game *******\n"); printf("****** Enter 0 to exit the game *******\n"); printf("****** '*'For players '#'for computer ***** \ n“); printf("**************************************\n\n"); } void game() { char board[ROW][COL] = { 0 };//Initialize chessboard char ret = 0; Initboard(board,ROW,COL);//Format chessboard //Print chessboard printf("\n"); Displayboard(board, ROW, COL); while (1) { //Player go PlayerMove(board,ROW,COL); Displayboard(board, ROW, COL);//Players print the board every step ret=Iswin(board, ROW, COL);//Determine whether to end the cycle if (ret != 'C') { break; } //Computer go ComputerMove(board,ROW,COL); Displayboard(board, ROW, COL);//Computer printing chessboard ret=Iswin(board, ROW, COL);//Judge whether to continue the cycle if (ret != 'C') { break; } } if (ret == '*') { printf(" Congratulations, you won\n\n\n"); } else if (ret == '#') { printf(" You are too dish.\n\n\n"); } else if (ret == 'Q') { printf(" It ends in a draw\n\n\n"); } } void test() { int input = 0; srand((unsigned int) time(NULL));//The time function returns the form time_t, so the unsigned int cast is required do { menu();//Print menu printf("Please choose:>"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf(" Do you really want to quit the game?\n\n"); break; default: printf(" Wrong choice, take a good look at the tips!!!\n\n"); break; } } while (input); } int main() { test();//Main function execution return 0; } void Initboard(char board[ROW][COL], int row, int col)//Initialize chessboard content { int i = 0; int j = 0; for (i = 0; i < row;i++) for (j = 0; j < col; j++) { board[i][j] = ' '; } } void Displayboard(char board[ROW][COL], int row, int col) { int i = 0; int j = 0; for (i = 0; i < row; i++)//Output chessboard layout { int j = 0; for (j = 0; j < col; j++) { printf(" %c ",board[i][j]); if (j<col-1)//Last line does not need to output '|' printf("|"); } printf("\n"); if (i < row - 1) { for (j = 0; j < col; j++) { printf("---"); if (j<col-1)//Last column does not output '|' printf("|"); } printf("\n"); } } printf("\n"); } void PlayerMove(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("Player go:>"); while (1) { printf("Please enter the coordinates to go (method: X Blank space X Enter):>"); scanf("%d%d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col)//x and y should be within a reasonable range { if (board[x - 1][y-1] == ' ') { board[x - 1][y - 1] = '*'; break; } else { printf("Coordinates occupied, re-enter!\n\n"); } } else { printf("Illegal coordinate, re input\n\n"); } } } void ComputerMove(char board[ROW][COL], int row, int col) { printf("Computer go\n"); int x = 0; int y = 0; while (1) { x = rand() % row;//The coordinates should be within a reasonable range y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } } int IsFull(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] == ' ') { //Not full return 0; } } } //Full return 1; } //Judgement of winning or losing //Players win - '*' //Computer wins //Draw -'Q' //Continue -'C' char Iswin(char board[ROW][COL], int row, int col) { int i = 0; //Row judgment for (i = 0; i < row; i++) { if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ') { return board[i][0];//If it's not, it's just for the player to win, * for the computer to win } } //Column judgement for (i = 0; i < col; i++) { if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ') { return board[0][i]; } } if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ') { return board[0][0]; } if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ') { return board[0][2]; } //Judgement draw if (IsFull(board,row,col) == 1) { return 'Q'; } else return 'C'; }
Operation result: