catalogue

one , menu bar

two , Initialize chessboard

three , Print chessboard

four , Players play chess

five , Computer chess

six , Judge whether to win or lose

seven , Call player , Computer chess function and judgment function

eight , All codes

one , menu bar

1. Make a simple version of the menu , And attach option tips

 2. Because the program wants to run at least once , For all selections do while loop , When choosing to play or not to play games , Then use switch case The statement is more appropriate , Then you can call the corresponding function.

  two , Initialize chessboard

1. First create a chessboard

To make it easier to change the size of the chessboard , All macro definitions are used to define their rows and columns

 

2. Initialize the chessboard

When not playing chess at first , The chessboard should be blank , All initializes all its elements to spaces

  three , Print chessboard

 

 

  four , Players play chess

Because the player doesn't know that the array subscript is 0 start , All in use checkerboard array , Both rows and columns should -1

While playing chess , There may be wrong coordinates or pieces in the coordinates written , All use cycle

 

  five , Computer chess

Computer chess adopts the method of random number , You need to use the function of timestamp and random number :rand(),time() Two functions , And call rand() Function needs to be called srand() function

Due to the uncertainty of random number , All adopt the method of taking mould , Change its range to the range of row and column markers of chessboard array

Because I don't know if there are chess pieces where I play chess , The same cycle is adopted

six , Judge whether to win or lose

1. Four results to judge whether to win or lose

If the player wins —— return   '*'

If the computer wins —— return '#'

If there is a draw     —— return 'Q'

The game continues     —— return 'C'

2. Judge whether the rows are equal

 3. Determine whether the columns are equal

4. Judge whether the diagonal lines are equal

  If the chessboard is large , It needs to be judged in a circular way

5. When judging a draw or the continuation of the game, it depends on whether the chessboard is full

 

  seven , Call player , Computer chess function and judgment function

  Because playing chess is a multi-step operation , All use cycle

  The chessboard should be printed for each move , It also determines whether the game needs to continue , Don't continue , Then jump out , Judge whether to win or lose

  eight , All codes
// Test file #include"game.h" void menu() {
printf("*****************************\n"); printf("***** 1.play **********\n");
printf("***** 0.exit **********\n"); printf("*****************************\n");
} void game() { // Create a chessboard array char board[ROW][COL]; // Initialize the chessboard InitBoard(board,
ROW, COL); // Print chessboard DisplayBoard(board, ROW, COL); char ret = '\0'; while (1) {
// Players play chess PlayerMove(board, ROW, COL); DisplayBoard(board, ROW, COL); ret =
IsWin(board, ROW, COL); // The game continues if (ret != 'C') { break; } // Computer chess
ComputerMove(board, ROW, COL); DisplayBoard(board, ROW, COL); ret =
IsWin(board, ROW, COL); // The game continues if (ret != 'C') { break; } } if (ret == '*') {
printf(" Player wins \n"); } else if (ret == '#') { printf(" Computer win \n"); } else {
printf(" it ends in a draw \n"); } } int main() { int input = 0; srand((unsigned
int)time(NULL)); do { menu(); printf(" Please select :"); scanf("%d", &input); switch
(input) { case 1: game();// play a game break; case 0: printf(" Exit the game \n"); break; default:
printf(" Selection error , Please reselect \n"); break; } } while (input); return 0; } // Declaration document
#include<stdio.h> #include<stdlib.h> #include<time.h> // Define rows and columns by macro definition , Easy to modify the chessboard
#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 PlayerMove(char board[ROW][COL], int row, int col); // Computer chess void
ComputerMove(char board[ROW][COL], int row, int col); // Judge whether to win or lose char IsWin(char
board[ROW][COL], int row, int col); // Function definition file #include"game.h" // Initialize chessboard 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] = ' ';// Initialize to space }
} } // Print chessboard void DisplayBoard(char board[ROW][COL], int row, int col) { int i =
0; for (i = 0; i < row; i++) { int j = 0; // Print each line for (j = 0; j < col; j++) {
printf(" %c ",board[i][j]); // The outermost to the right does not print (|) if (j < col - 1) { printf("|"); } }
printf("\n"); // Print delimited lines , But don't print at the bottom (---|) if (i < row - 1) { for (j = 0; j < col;
j++) { printf("---"); // The outermost to the right does not print (|) if (j < col - 1) { printf("|"); } }
printf("\n"); } } } // Players play chess void PlayerMove(char board[ROW][COL], int row, int
col) { int x = 0; int y = 0; printf(" Players play chess , Enter coordinates :"); while (1) { scanf("%d %d",
&x, &y); if (x <= row && x > 0 && y <= col && y > 0 ) { if (board[x - 1][y - 1]
== ' ') { board[x - 1][y - 1] = '*'; break; } else { printf(" The coordinates are already occupied , Please re-enter \n");
} } else { printf(" Illegal coordinates , Please re-enter \n"); } } } // Computer chess void ComputerMove(char
board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf(" Computer chess :");
while (1) { x = rand() % row; y = rand() % col; if (board[x][y] == ' ') {
board[x][y] = '#'; printf("\n"); break; } } } // Judge whether to win or lose // Player wins -- return ‘*’ // Computer win --
return ‘#’ // it ends in a draw -- return ‘Q’ // The game continues -- return ‘C’ // Judge whether the chessboard is full int IsFull(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;// The chessboard is not full } } } return
1;// The chessboard is full } char IsWin(char board[ROW][COL], int row, int col) { // Row equality 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]; } } // Column equality 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 equality 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]; } // it ends in a draw if (IsFull(board, row, col) ==
1) { return 'Q'; } // The game continues else { return 'C'; } }

 

Technology