catalog:
The following is the main body
1, Explain
With the gradual deepening of our learning of C language, we can realize some simple games. This paper will introduce the C language to realize the three piece chess game. Through this paper, we can understand the usage of array and the use of loop and selection structure in C language
2, Multi file creation
When we are writing some large programs, it will be very confusing and poor readability to write directly in a file. At this time, we can usually divide a large program into several small parts, such as header file, function file and main function file. We can also add files as needed. In this article, we include three files, which are the main function test C file, which contains the menu and main function; Game C file, which contains some functions of the game; game.h header file, which contains the header file referenced by the game. Next, we will interpret each part of the file in detail. If you need to copy the code, you can directly turn to the last to see the overall code.
3, Design ideas
First of all, let me introduce the playing methods and rules of Sanzi chess. Here we quote the following Baidu Encyclopedia introduction to Sanzi chess.
Design idea:
First of all, when we want to play a three piece chess game, we first print out the menu and let the player choose whether to play the game. If the player chooses 1, the player enters the game. If the player chooses zero, the player exits the game. When the player enters an error, we prompt the player to enter an error and re-enter. Here, we use the do while loop so that the player can continue to play if he doesn't have fun. When the player chooses to enter the game, we initialize the chessboard and print it out. Then the player selects the coordinates and then plays chess. Then the computer selects the coordinates and then the computer plays chess. In order to achieve the effect of computer chess after players play chess, we use the while loop. At the end of each chess game, judge whether to win or lose. When one party wins or draws, the game ends. The above is the design idea of sanziqi game.
When we want to play a three piece chess game, we first print out the menu and let the player choose whether to play the game. If the player chooses 1, the player enters the game. If the player chooses zero, the player exits the game. When the player enters an error, we prompt the player to enter an error and re-enter. Here, we use the do while loop to make the player continue to play if he has no fun. When the player chooses to enter the game, we initialize the chessboard and print it out. Then the player selects the coordinates and then plays chess. Then the computer selects the coordinates and then the computer plays chess. In order to achieve the effect of computer chess after players play chess, we use the while loop. At the end of each chess game, judge whether to win or lose. When one party wins or draws, the game ends. The above is the design idea of sanziqi game.
4, Code implementation
1.test.c
The code is as follows:
#include"game.h" void menu() { printf("**********************************************\n"); printf("**************** 1.play ********************\n"); printf("**************** 0.exit ********************\n"); printf("**********************************************\n"); } void game() { char ret = 0; //Store chess data char board[ROW][COL] = { 0 }; //Initialize the chessboard to full space init_board(board, ROW, COL); //Print chessboard display_board(board, ROW, COL); while (1) { //Players play chess player_move(board, ROW, COL); display_board(board, ROW, COL); //Judge whether to win or lose ret = is_win(board, ROW, COL); if (ret != 'C') { break; } //Computer chess computer_move(board, ROW, COL); display_board(board, ROW, COL); //Judge whether to win or lose ret = is_win(board, ROW, COL); if (ret != 'C') { break; } } if (ret == '*') { printf("The player won\n"); } else if (ret == '#') { printf("The computer won\n"); } else if (ret == 'Q') { printf("it ends in a draw\n"); } } //Player wins - '*' //Computer wins - '#' //Draw -- 'Q' //Continue -- 'C' int main() { int input = 0; srand((unsigned int)time(NULL));//Generate random values do { menu(); printf("Please enter:"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf("Exit the game\n"); break; default: printf("Input error, please re-enter\n"); break; } } while (input); return 0; }
First of all, we want players to choose whether to play or not, so we use scanf to let players make input choices. In order to let players play the game many times, we use the do while loop. We first print out the menu, write a simple menu in the menu function and use it in the main function. Then use the switch statement to enter the game when selecting 1, exit the game when selecting 0, and let the player choose again when entering other characters.
2.game.h
In the header file, we can put the library functions we need to reference in it, so that there is no need to repeat references in the main function. In addition, we can declare custom functions in the header file, so there is no need to declare them in the main function. Moreover, we can use macro definition to replace the length and width of the chessboard with ROW and COL, which is easy to use. We put init in the header file_ Board, display_board, player_move, computer_move, is_win.
#include<stdio.h> #include<stdlib.h> #include<time.h> #include<Windows.h> #define ROW 3 #define COL 3 void menu(); void game(); //Initialize chessboard void init_board(char board[ROW][COL], int row, int col); //Print chessboard void display_board(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 whether to win or lose char is_win(char board[ROW][COL], int row,int col);
3.game.c
(1)init_board()
Initialize chessboard
{ int i = 0; for (i = 0; i < row; i++) { for (int j = 0; j < col; j++) { board[i][j] = ' '; } } }
Each element of the board array is initialized as a space by nesting two for loops
(2)display_board()
Print chessboard
{ for (int i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { //print data printf(" %c ", board[i][j]); if(j<col-1) printf("|"); } printf("\n"); //Print split lines if (i < row - 1) { for (j = 0; j < col; j++) { printf("---", board[i][j]); if (j < col - 1) printf("|"); } printf("\n"); } } }
The effect we want in printing the chessboard is shown in the figure below. Write code according to the ideal diagram and spend a little time thinking about it.
(3)player_move()
Player movement
{ int x = 0; int y = 0; printf("Players play chess\n"); while (1) { printf("Please enter coordinates:\n"); 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("Illegal coordinates, please re-enter\n"); } }
First of all, we use scanf to let players input coordinates. When the coordinates are legal, that is, when x is greater than 1 and less than the width of the chessboard and Y is greater than 1 and less than the length of the chessboard, if there is a space on this coordinate, the player will play chess in this coordinate, that is, replace the character at (x,y) with *. If it is occupied or input incorrectly, the player will be prompted to re-enter.
(4)computer_move()
Computer mobile
{ int x = 0; int y = 0; printf("Computer chess\n"); while (1) { x = rand() % row; y = rand() % col; if (board[x][x] == ' ') { board[x][y] = '#'; break; } } }
The difficulty of computer chess is to generate random coordinates. Here we use time stamps to generate random values. This part is described in detail in the number guessing game written before. The part of playing chess is the same as that of players.
(5)if_full()
Judge the draw
{ int i = 0; for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { if (board[i][j] == ' ') { return 0; } } } return 1; }
Returns 1 when all positions of the chessboard are occupied, and returns zero when there are positions not occupied
(6)is_win()
Judge the outcome
{ 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]; } } for (i = 0; i < col; i++) { if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ') { return board[1][i]; } } 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 the draw if (if_full(board, row, col) == 1) { return 'Q'; } return 'C'; }
The method of judging the victory or defeat of a function must first know the conditions of victory.
Conditions: three horizontal rows are the same 𞓜 three vertical rows are the same | three oblique rows are the same. Knowing the winning and losing conditions will lead to ideas. We use the for loop to traverse the characters in a row first. When the characters in a row are the same, we return the characters in this row. When the draw is a draw, the return value is Q. in addition to these cases, we return C, that is, continue -- continue.
Then let's look at the method of judging who wins and who loses in the game function
void game() { char ret = 0; //Store chess data char board[ROW][COL] = { 0 }; //Initialize the chessboard to full space init_board(board, ROW, COL); //Print chessboard display_board(board, ROW, COL); while (1) { //Players play chess player_move(board, ROW, COL); display_board(board, ROW, COL); //Judge whether to win or lose ret = is_win(board, ROW, COL); if (ret != 'C') { break; } //Computer chess computer_move(board, ROW, COL); display_board(board, ROW, COL); //Judge whether to win or lose ret = is_win(board, ROW, COL); if (ret != 'C') { break; } } if (ret == '*') { printf("The player won\n"); } else if (ret == '#') { printf("The computer won\n"); } else if (ret == 'Q') { printf("it ends in a draw\n"); } } //Player wins - '*' //Computer wins - '#' //Draw -- 'Q' //Continue -- 'C'
Here we write a character variable ret to receive is_ The return value of win function and judge it in the code. See function for details.
5, Overall code
//game.h #include<stdio.h> #include<stdlib.h> #include<time.h> #include<Windows.h> #define ROW 3 #define COL 3 void menu(); void game(); //Initialize chessboard void init_board(char board[ROW][COL], int row, int col); //Print chessboard void display_board(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 whether to win or lose char is_win(char board[ROW][COL], int row,int col); //test.c #include"game.h" void menu() { printf("**********************************************\n"); printf("**************** 1.play ********************\n"); printf("**************** 0.exit ********************\n"); printf("**********************************************\n"); } void game() { char ret = 0; //Store chess data char board[ROW][COL] = { 0 }; //Initialize the chessboard to full space init_board(board, ROW, COL); //Print chessboard display_board(board, ROW, COL); while (1) { //Players play chess player_move(board, ROW, COL); display_board(board, ROW, COL); //Judge whether to win or lose ret = is_win(board, ROW, COL); if (ret != 'C') { break; } //Computer chess computer_move(board, ROW, COL); display_board(board, ROW, COL); //Judge whether to win or lose ret = is_win(board, ROW, COL); if (ret != 'C') { break; } } if (ret == '*') { printf("The player won\n"); } else if (ret == '#') { printf("The computer won\n"); } else if (ret == 'Q') { printf("it ends in a draw\n"); } } //Player wins - '*' //Computer wins - '#' //Draw -- 'Q' //Continue -- 'C' int main() { int input = 0; srand((unsigned int)time(NULL)); do { menu(); printf("Please enter:"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf("Exit the game\n"); break; default: printf("Input error, please re-enter\n"); break; } } while (input); return 0; } //game.c #include"game.h" void init_board(char board[ROW][COL], int row, int col) { int i = 0; for (i = 0; i < row; i++) { for (int j = 0; j < col; j++) { board[i][j] = ' '; } } } void display_board(char board[ROW][COL], int row, int col) { for (int i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { //print data printf(" %c ", board[i][j]); if(j<col-1) printf("|"); } printf("\n"); //Print split lines if (i < row - 1) { for (j = 0; j < col; j++) { printf("---", board[i][j]); 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:\n"); 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("Illegal coordinates, 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; y = rand() % col; if (board[x][x] == ' ') { 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; } } } return 1; } char is_win(char board[ROW][COL], int row, int col) { 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]; } } for (i = 0; i < col; i++) { if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ') { return board[1][i]; } } 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 the draw if (if_full(board, row, col) == 1) { return 'Q'; } return 'C'; }
6, Summary
This article mainly introduces the implementation of sanziqi game, which introduces the use of loops, selection statements and how to structurally complete a project, and the debugging process in the design process will improve our technology. I think it is worth spending time on it. Above is the whole content of this article. Thank you for watching.
Look forward to your attention