**
Gobang games
**
##Ideas for creating games:
1. Print the initialization chessboard on the display interface with corresponding coordinates
2. When players enter coordinates, it is necessary to consider whether the coordinates entered by players are within the legal range
3. Players enter the corresponding coordinates through the keyboard as the "falling" action
4. The player's opponent is the computer. The computer randomly "drops", and the random coordinates are generated by the "time stamp"
5. Finally, judge the winning or losing situation through the "falling" in the last step
##Game module implementation and its corresponding code
1. Initialize the chessboard, initialize all the elements of the initial chessboard, and print the boundary symbols on the interface. Modularization is realized through functions.
void init(char chessBoard[MAX_ROW][MAX_COL]) { // Set every element in the array as a space for (int row = 0; row < MAX_ROW; row++) { for (int col = 0; col < MAX_COL; col++) { chessBoard[row][col] = ' '; } } }
2. / / let the player fall It is realized by inputting row and column coordinates on the console
void playerMove(char chessBoard[MAX_ROW][MAX_COL]) { while (1) { printf(" Please enter the coordinates(row col): "); int row = 0; int col = 0; scanf("%d %d", &row, &col); // Be sure to check whether the coordinates entered by the player are legal if (row < 0 || row >= MAX_ROW || col < 0 || col >= MAX_COL) { // Illegal situation // At this point, the player should be asked to re - enter printf("Your coordinates are not in the legal range [0, 2]\n"); continue; } // Has the player checked the position if (chessBoard[row][col] != ' ') { printf("Your coordinate position already has a child!\n"); continue; } // It's really settled chessBoard[row][col] = 'x'; break; } }
3. The computer randomly generates a set of random coordinates
void computerMove(char chessBoard[MAX_ROW][MAX_COL]) { while (1) { int row = rand() % MAX_ROW; int col = rand() % MAX_COL; if (chessBoard[row][col] != ' ') { // It is necessary to ensure that the random number cannot be a position that already has children continue; } chessBoard[row][col] = 'o'; break; } }
4. Judge whether the chessboard is full and draw occurs
int isFull(char chessBoard[MAX_ROW][MAX_COL]) { // Find space If you can't find it, it's full for (int row = 0; row < MAX_ROW; row++) { for (int col = 0; col < MAX_COL; col++) { if (chessBoard[row][col] == ' ') { // If a space is found, it means it is not full return 0; } } } return 1; }
5. Judge the winning or losing situation. Input the coordinates for the last time to see if there are three words connected into a line to judge the winning or losing or draw.
char isWin(char chessBoard[MAX_ROW][MAX_COL]) { // Determine all rows 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]; } } // Determine all columns 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]; } } // Determine two diagonals if (chessBoard[0][0] != ' ' && chessBoard[0][0] == chessBoard[1][1] && chessBoard[0][0] == chessBoard[2][2]) { return chessBoard[0][0]; } if (chessBoard[2][0] != ' ' && chessBoard[2][0] == chessBoard[1][1] && chessBoard[2][0] == chessBoard[0][2]) { return chessBoard[2][0]; } // Determine whether to draw // See if there is any space left in the chessboard if (isFull(chessBoard)) { return 'q'; } return ' '; }
##Design experience:
The above is the main content of the game,
//The basic process of a game
// 1. Create checkerboard and initialize
// 2. Print chessboard
// 3. Player drop (players drop by entering row and column coordinates)
// 4. Determine the relationship between victory and defeat
// 5. Computer drop (random position drop)
// 6. Determine the relationship between victory and defeat
//Go back to 2 and continue
Through the creation of this small game, I have deepened the importance of function to modular implementation. The use of functions is also more skilled. At the same time, I use array to initialize and define the chessboard, which deepens my knowledge of array application.