catalogue
Recommended reading order (skipping is not recommended)
Interface after implementation
Steps to be implemented by the analyzer
Recommended reading order (skipping is not recommended)
First look at the interface after implementation -- then look at the steps to be implemented by the analyzer -- and then turn to test Part C - in test Find the main () function in C -- from the first step of the main function -- if you encounter a custom function, please go to game H find the details of the corresponding function in the source file
Auxiliary reading:
game.h file contains the function declaration (reference header file)
game.c file contains the details of the function (how to implement the corresponding steps in code)
test.c the general idea of the whole is put in the document
The purpose of sub packaging code with different documents is to make your code more organized, think more clearly, and make it easier for others to understand
If you think it's too troublesome to turn around: please log in to your current compiler - copy the code first (package it according to different files) - encounter a custom function (put the mouse over the function name, right-click and go to the definition) (so that the compiler can directly go to the function details in the game.c file)
Interface after implementation
Steps to be implemented by the analyzer
1. Print the menu on the screen (prompting you to select)
2. Define the array and initialize the array (replace all the elements in the array with spaces)
3. Print out the array
4. Players play chess
5. Judge whether to win or lose (four situations: player wins, player loses, draw, game is not over, continue)
6. Computer chess
7. Judge whether to win or lose (four situations: player wins, player loses, draw, game is not over, continue)
8. You can play this game many times
test.c (source file)
For the general idea of putting the whole, the user-defined function is placed in game H file
#define _CRT_SECURE_NO_WARNINGS 1 #include "game.h" void menu() { printf("+-------------------------------------+\n"); printf("| 1.play a game |\n"); printf("| 0.Exit the game |\n"); printf("+-------------------------------------+\n"); } void game() { //Define checkerboard array //Where Hang and Lie are identifier constants defined by define -- in game H can be found in the file //Hang indicates that there are several rows -- Lie indicates that there are several columns char board[Hang][Lie]; char ret = ' '; //Initialize checkerboard array //Pass in three variables, the first element of the array, address, row and column //In game H to declare this function in game C to implement this function, complete the above two steps, and use it directly here init_board(board, Hang, Lie); //Enter this function -- jump to game C file, with a comment of 1 above the code //Print the chessboard interface - display the chessboard - enter this function - jump to game C file, with 2 comments above the code checker_board(board, Hang, Lie); while (1) { //Players go - enter this function - jump to game C file, with 3 comments above the code player_move(board, Hang, Lie); checker_board(board, Hang, Lie); //Judge the winner or loser - enter this function - jump to game C file, with 5 comments above the code ret = win_or_lose(board, Hang, Lie); if (ret != 'C') { break; } //The computer goes into this function and jumps to game C file, with 4 comments above the code computer_move(board, Hang, Lie); checker_board(board, Hang, Lie); //Judge whether to win or lose ret = win_or_lose(board, Hang, Lie); if (ret != 'C') { break; } } if (ret == 'Q') { printf("it ends in a draw\n"); } if (ret == '*') { printf("Congratulations: win\n"); } if (ret == '#') { printf("I'm sorry: you lost\n"); } } int main() { int input = 0; srand((unsigned int)time(NULL)); //rand() is used to generate random numbers //srand() is called with rand() //srand(), where () needs to use constantly changing quantities -- time stamps are used to do constantly changing quantities -- time stamps can be implemented by the time() function //(unsigned int) -- is a cast type //The srand() function doesn't need to be referenced multiple times, so just put it in front of the loop //This generated random number was also mentioned in the previous blog post. If you don't understand it, you can also go to the previous blog post do { menu(); //Print a simple menu interface (this function doesn't need to go to the game.c file, just above this file) printf("Please enter the sequence number before the option>:"); scanf("%d", &input); switch (input) //The switch selection statement is implemented: enter 1 to play the game, enter 0 to exit the game, and enter other numbers to prompt input errors. { case 1: //game() user defined function - the main design idea of implementing the three piece chess game in this function is in this function //This function is also at the top of this file. You don't have to go to game C found in file game(); //Entry function break; case 0: printf("Exit the game\n"); break; default: printf("Input error, please re-enter\n"); break; } } while (input); return 0; }
game.h (header file)
Declaration of the playback function (reference header file)
#pragma once #include<stdio.h> #include<time.h> #include<stdlib.h> #define Hang 3 #define Lie 3 //Initialize the chessboard and change the number in the chessboard into a space; void init_board(char board[Hang][Lie], int hang, int lie); //Print checkerboard and display the checkerboard status; void checker_board(char board[Hang][Lie], int hang, int lie); //Player go void player_move(char board[Hang][Lie], int hang, int lie); //Computer walk void computer_move(char board[Hang][Lie], int hang, int lie); //Judge whether to win or lose char win_or_lose(char board[Hang][Lie], int hang, int lie);
game.c (source file)
Details of the amplification function (how to implement the corresponding steps in code)
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" //1111111111 //Initializes the number in the array to a space void init_board(char board[Hang][Lie], int hang, int lie) { int i = 0; for (i = 0; i < hang; i++) { int j = 0; for (j = 0; j < lie; j++) { board[i][j] = ' '; } } } //2222222222 //Print chessboard void checker_board(char board[Hang][Lie], int hang, int lie) { int i = 0; for (i = 0; i < hang; i++) { int j = 0; for (j = 0; j < lie; j++) //Print the first line of chessboard { printf(" %c ", board[i][j]); if (j < lie - 1) { printf("|"); } } printf("\n"); if (i < hang - 1) { for (j = 0; j < lie; j++) { printf("---"); if (j < lie - 1) { printf("|"); } } } printf("\n"); } } //3333333333 //Player input - judge the coordinates entered by the player //If it exceeds the array range, an input error is displayed. Please re-enter //If the position of playing chess has been occupied, the coordinate error is displayed void player_move(char board[Hang][Lie], int hang, int lie) { int x = 0; int y = 0; printf("Please enter the location coordinates you want to:"); while (1) //The condition for the end of this cycle is that the player enters the correct { scanf("%d %d", &x, &y); //Players enter coordinates if (x > 0 && x <= hang && y > 0 && y <= lie) { if (board[x - 1][y - 1] == ' ') //The input number is correct { board[x - 1][y - 1] = '*'; break; } else //The coordinates corresponding to the number of inputs have been occupied { printf("Coordinate error:"); } } else //The number of inputs exceeds the array range { printf("Input error, please re-enter:"); } } } //4444444444 //Computer walk void computer_move(char board[Hang][Lie], int hang, int lie) { int hangs = 0; int lies = 0; printf("Computer walk:\n"); while(1) //The condition for the end of this cycle is: until the execution random coordinates are correct (not occupied) { //Production random coordinates int hangs = rand() % hang; int lies = rand() % lie; if (board[hangs][lies] == ' ') //Determine whether the coordinates are occupied { board[hangs][lies] = '#'; break; } } } //5555555555 //Judge 4 states; //1. Player wins, return* //2. Computer wins, return# //3. Draw return Q //4. Continue to return to C char win_or_lose(char board[Hang][Lie], int hang, int lie) { int i = 0; int j = 0; int count = 0; //The following judgment is whether the computer wins or the player wins //Conditions for winning (case 8): three in a row are the same, or three in a column are the same, or the diagonal is the same for (i = 0; i < hang; i++) { if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ') { return board[i][0]; } if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ') { return board[0][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]; } //The following is to judge whether there is a draw //Draw condition: there are no spaces in the array (all occupied) and it has been determined that the player's computer did not win for (i = 0; i < hang; i++) { for (j = 0; j < lie; j++) { if (board[i][j] == ' ') { count++; } } } if (count == 0) { return 'Q'; } //Not a draw, not a computer win, not a player win, the rest is to continue playing return 'C'; }