To realize the three chess game
In the initial work, an identifier is defined by #define to represent a constant (the horizontal and vertical coordinates of the chessboard)
(because there are many coordinates that need to be used in the writing of the code. When reading the code, the reader may be tired of understanding what the current value represents, which will cause trouble for the interpretation of the code. Therefore, this problem is solved by using macro definition in the code, and it is also convenient to change the size of the chessboard later.)
#define MAX_ROW 3 #define MAX_COL 3
Game creation process
1. Create a chessboard
Create a chessboard (two-dimensional array) and initialize it (blank, no sub state).
//1. Chessboard initialization void init(char chessBoard[MAX_ROW][MAX_COL]) { for (int row = 0; row < MAX_ROW; row++) { for (int col = 0; col < MAX_COL; col++) { //Initialize each position of the chessboard (two-dimensional array) to null chessBoard[row][col] = ' '; } } }
2. Print chessboard
Print the chessboard so that players can see the state of the chessboard at this time.
//2. Print chessboard void printChessBoard(char chessBoard[MAX_ROW][MAX_COL]) { printf("+---+---+---+\n"); for(int row = 0; row < MAX_ROW; row++) { printf("|"); for (int col = 0; col < MAX_COL; col++) { printf(" %c |", chessBoard[row][col]); } printf("\n+---+---+---+\n"); } }
3. Players fall
Players will enter the coordinates (row, col) to drop the seeds.
Note: after the player selects the coordinates, the legitimacy test needs to be done, that is, to judge whether the player's position has children. If so, the player needs to re-enter the coordinates.
//3. Players fall void playMove(char chessBoard[MAX_ROW][MAX_COL]) { while (1) { int row = 0, col = 0; printf("Players are requested to enter the coordinates:"); scanf("%d %d", &row, &col); //Check whether the player has input errors if (row < 0 || col < 0 || row >= MAX_ROW || col >= MAX_COL) { printf("Input error! Please re-enter"); continue; } // Check whether the position entered by the player has children (judge whether the position is empty. If it is not empty, there will be children) else if (chessBoard[row][col] != ' ') { printf("This position already has child, please re-enter"); continue; } else { //No problem, drop it, mark it as' x ', and exit the current cycle chessBoard[row][col] = 'x'; break; } } }
4. Determine the outcome at this time
4.1 there are four ways to judge the current situation:
1)Three characters are the same across the line; 2)Three characters are the same; 3)1,3 The three positions of the quadrant diagonal are the same characters; 4)2,4 The three positions of the quadrant diagonal are the same characters.
//4. Determine the victory and defeat, and check all positions of the chessboard to see if they meet the four conditions of victory //Player wins and returns x; o win, return to the computer; Draw returns p; char chessWin(char chessBoard[MAX_ROW][MAX_COL]) { //Judge the three positions across for (int row = 0; row < MAX_ROW; row++) { if (chessBoard[row][0] != ' ' && chessBoard[row][0] == chessBoard[row][1] && chessBoard[row][0] == chessBoard[row][2]) { return chessBoard[row][0]; } } //Judge the three vertical positions for (int col = 0; col < MAX_COL; col++) { if(chessBoard[0][col] != ' ' && chessBoard[0][col] == chessBoard[1][col] && chessBoard[0][col] == chessBoard[2][col] ){ return chessBoard[0][col]; } } //Judge the three positions of the diagonals of quadrants 2 and 4 if (chessBoard[0][0] != ' ' && chessBoard[0][0] == chessBoard[1][1] && chessBoard[0][0] == chessBoard[2][2]) { return chessBoard[0][0]; } //Judge the three positions of the diagonals of quadrants 1 and 3 if (chessBoard[2][0] != ' ' && chessBoard[2][0] == chessBoard[1][1] && chessBoard[2][0] == chessBoard[0][2]) { return chessBoard[2][0]; } if (isFull(chessBoard)) { return 'p'; } return ' '; }
4.2 there are four situations after judging the victory and defeat, namely:
1)Player wins, return 'x' ; 2)Computer wins, return 'o' ; 3)Draw. The board is full, but there is no winner. Go back 'p' ; 4)The chessboard is still empty. Continue playing chess and return ' ' ;
We define a character variable in the main function to judge what is returned at the end of the program, and then judge whether the player wins, the computer wins, and the game is not over.
The result of the game is known according to the character variable (the specific process is shown in the content of the later main function).
4.3 the function to judge whether to play chess is:
//4.3 judge whether to play chess (the chessboard is full, but there is no victory or defeat). Returning 1 means the chessboard is full, and returning 0 means the chessboard is not full int isFull(char chessBoard[MAX_ROW][MAX_COL]) { for (int row = 0; row < MAX_ROW; row++) { for (int col = 0; col < MAX_COL; col++) { if (chessBoard[row][col] == ' ') { return 0; } } } return 1; }
5. Computer drop (computer random drop)
The random drop of computer is realized by rand() function.
(remainder operation is a good truncation method. Here, we control the value of abscissa and ordinate between [0,2] through remainder operation.)
//5. Computer drop (random) void computerPlayMove(char chessBoard[MAX_ROW][MAX_COL]) { while (1) { //The coordinates x and y of the control drop are between [0,2] int row = rand() % MAX_ROW; int col = rand() % MAX_COL; //Judge whether the position where the computer wants to drop is empty (i.e. whether there is already a child). If there is already a child, continue the cycle if (chessBoard[row][col] != ' ') { continue; } //If the position where the computer wants to drop is empty, assign 'o' to the position and end the cycle chessBoard[row][col] = 'o'; break; } }
6. Determine the outcome at this time
The same process 4.
7. Main function
Note that we write the character two-dimensional array chessboard in the main function, not at the beginning of the whole program.
The reason is: if it is written at the beginning, then this variable is a global variable.
The life cycle of the global variable is the whole program, which will occupy more memory. Moreover, if this variable has been used in the middle of the program, the memory cannot be released.
In addition, the scope of global variables is the whole project. If it is changed anywhere, the whole will be affected, and it will be very troublesome to find out where the problem is.
Therefore, based on the principle of not using global variables as much as possible, this variable is written in the main function. When other functions need this parameter, it can be used by transferring parameters.
int main() { char chessBoard[MAX_ROW][MAX_COL]; char winner = ' '; //1. Chessboard initialization init(chessBoard); while (1) { //2. Print the chessboard and make the player observe the state of the chessboard at this time printChessBoard(chessBoard); //3. Players fall playMove(chessBoard); //4. Judge the outcome winner = chessWin(chessBoard); if (winner != ' ') { //There is already a non empty return value. The game is over break; } //5. Computer storage computerPlayMove(chessBoard); //4. Judge the outcome winner = chessWin(chessBoard); if (winner != ' ') { //There is already a non empty return value. The game is over break; } } if (winner == 'x') { printChessBoard(chessBoard); printf("Congratulations on winning!"); } else if (winner == 'o') { printChessBoard(chessBoard); printf("Unfortunately, the computer won~"); } else { printChessBoard(chessBoard); printf("Oh, you're tied with the computer~"); } system("pause"); return 0; }
The above is my thinking, code and some points that need to be paid attention to in writing Sanzi game.
I am weak in talent and learning. If there is anything wrong, please give me more advice~~
(attached is a recently found super cute picture ~ ~)