catalogue
4. Just run the program according to the prompts
preface:
● realize the application and understanding of array and other knowledge points through the programming of Sanzi chess.
● due to the limited level of the author, it is inevitable that there are fallacies in the article. Please correct the slang, and sincerely hope to give advice!
Text:
Description: this code is compiled in Visual studio Studio2019, in which the code statement specifically set for the compiler appears: #define _ CRT_SECURE_NO_WARNINGS
Function of this code statement: to solve the error or warning problems of some library functions in the Visual Studio compilation environment
Note: this code statement is only available in Visual Studio The Studio compilation environment is used, and other compilers need to mask this code statement
1. Create header file: game.h
The code is as follows:
#define ROW 3 // that 's ok #define COL 3 // column //Turn to test.c to modify the array definition and change [3] [3] to [ROW][COL] #include <stdio.h> #include<stdlib.h> #include<time.h> void InitBoard(char board[ROW][COL], int row,int col); //Corresponding to the Initboard (...) in test.c, row,col, etc. will be debugged here //Go to the game.c implementation void DisplayBoard(char board[ROW][COL], int row, int col); void PlayerMove(char board[ROW][COL], int row, int col); void ComputerMove(char board[ROW][COL], int row, int col); //Tell us four game states //Player wins - '*' //Computer wins - '#' //Draw -'Q ' //Continue - 'C' char IsWin(char board[ROW][COL], int row, int col);
2. Create source file: game.c
The code is as follows:
#define _CRT_SECURE_NO_WARNINGS #include "game.h" void InitBoard(char board[ROW][COL], int row, int col) //To call the custom header file, go to the above { int i = 0; int j = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { board[i][j] = ' '; } } } //At this time, each element is initialized and go to test.c void DisplayBoard(char board[ROW][COL], int row, int col) { int i = 0; for (i = 0; i < row; i++) { //1. Print one line of data //printf(" %c | %c | %c \n",board[i][0],board[i][1],board[i][2]); //Replace with the following int j = 0; for (j = 0; j < col; j++) { //1. Print one line of data printf(" %c ", board[i][j]); if (j < col - 1) //Controls whether the last column is printed printf("|"); } printf("\n"); //2. Print split lines if (i < row - 1) //Controls whether the last line is printed { //printf("---|---|---\n"); for (j = 0; j < col; j++) { printf("---"); if (j < col - 1) //Controls whether the last column is printed printf("|"); } printf("\n"); } //printf("---|---|---\n"); } } void PlayerMove(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("Ask players to place pieces:>\n"); while (1) { printf("Please enter the coordinates:>"); scanf("%d%d", &x, &y); //Judge the validity of X and Y coordinates if (x >= 1 && x <= row && y >= 1 && y <= col) //Players may not understand that the array starts from 0 and think it starts from 1 { if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '*'; break; } else { printf("This coordinate is occupied, please re-enter!\n"); } } else { printf("The coordinates entered are illegal, please re-enter!\n"); } } } void ComputerMove(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("Please put chess pieces into the computer:>\n"); while (1) { x = rand() % row; y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } } //Returning 1 means the chessboard is full //Returning 0 means the chessboard is not full 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] == ' ') { return 0; //Not full } } } return 1; //Full } char IsWin(char board[ROW][COL], int row, int col) { int i = 0; //Horizontal three lines 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]; //[i] [0] / / [i] [2] is OK } } //Vertical three columns 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]; } } //Two diagonals if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ') { return board[1][1]; } if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ') { return board[1][1]; } //Judge whether there is a draw if (1 == IsFull(board, ROW, COL)) { return 'Q'; } return 'C'; }
3. Create source file: test.c
The code is as follows:
//Test the Gobang game #Define _crt_secure_no_warnings / / this statement is specific to the compilation environment using VS2019. Other compilers can mask this statement //Step 1 test.c step / / #include < stdio. H > //void menu() //{ // printf("***********************\n"); // printf("****1.play 0.exit****\n"); // printf("***********************\n"); //} //void test() //{ // int input = 0; // Initially assign a random value // do // { // menu(); // menu // printf("please select: >"); // scanf("%d", &input); // switch (input) // { // case 1: // printf("Gobang \ n"); // break; // case 0: // printf("exit game \ n"); // break; // defult: // Neither 1 nor 0 was selected // printf("wrong selection, please reselect! \ n"); // break; // } // } // while (input); // Judge whether the selection is true, 0 is false, 1 or default is true (! 0) //} //int main() //{ // test(); // return 0; //} //Step 2: create void game() and chessboard #include <stdio.h> #include "game.h" / / at this time, the rows and columns defined in game.h can be used void menu() { printf("Gobang game\n"); printf("***********************\n"); printf("****1.Play 0.Exit****\n"); printf("***********************\n"); printf("//The coordinate input method is: row (enter number) - spacebar - column (enter number) / / \ n "); } //The whole algorithm implementation of the game void game() { char ret = 0; //Create an array to store the chessboard information char board[ROW][COL] = { 0 }; //Char Board [3] [3] = {0}; / / modify [3] [3] to [ROW][COL] //Re reference the custom header file "game.h" //Initialize chessboard InitBoard(board,ROW,COL); //Then go to the game.h definition //The chessboard is printed DisplayBoard(board,ROW,COL); //Function of repacking and printing chessboard //play chess while (1) { //Players play chess PlayerMove(board,ROW,COL); DisplayBoard(board, ROW, COL); //Judge whether players win chess ret=IsWin(board,ROW,COL); if (ret != 'C') { break; } //Computer chess ComputerMove(board, ROW, COL); DisplayBoard(board, ROW, COL); //Judge whether the computer wins chess ret=IsWin(board, ROW, COL); if (ret != 'C') { break; } } if (ret == '*') { printf("Player wins!\n Please select whether to proceed to the next game!\n"); } else if (ret == '#') { printf("Computer wins!\n Please select whether to proceed to the next game!\n"); } else { printf("it ends in a draw!\n Please select whether to proceed to the next game!\n"); } } void test() { int input = 0; srand((unsigned int)time(NULL)); do { menu(); printf("Please select<Play>(Enter 1) or<Exit>(Enter (0):>"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf("Exit the game!\n"); break; defult: printf("Selection error, please re select!\n"); break; } } while (input); } int main() { test(); return 0; }
4. Just run the program according to the prompts
Running example:
Summary:
As the first game program written by the author, it is mixed with joy and bitterness! As a novice learning programming, the author's current level is limited, and he can only draw gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gourd gour!
During the debugging process, the author thought that he would fail several times, but insisted that after many times of debugging and consultation, he finally made the game program written for the first time run successfully! This is a very happy thing!
Due to some reasons, this paper has many shortcomings, many of which still need the author to learn and understand repeatedly. I hope that in the future, the author can grow together with the readers!