A Gobang program , It can be divided into four parts , The first part is the creation of header file , It can make our functions well encapsulated , There are two details to note : First, it is used in the header file define Define rows and columns , In this way, when we want to make changes to rows and columns in the future, we only need to change define This is a place . Second, put stdio.h
time.h Equal header files are included in game.h In this header file , Avoid duplicate inclusion .

The second part is test.c Implementation of function logic , It contains the basic logic needed to implement the game , Print including menu , Initialization of chessboard , Printing of chessboard , Players play chess with computers , Judgment of game state .

The third part is the implementation of game related functions , The front is the description of each specific function of the game , The following is the total code .

The fourth part is the game effect display . 

Part V : Overall game implementation code  

Part I :

Contents of header file -- Declaration of game related functions , Declaration of symbols , Inclusion of header file
#pragma once //game.h Declaration of game related functions , Declaration of symbols , Inclusion of header file // Declaration of symbols #define ROW 5 #define COL
5 // Inclusion of header file #include<stdio.h> #include<stdlib.h> #include<time.h> // Declaration of function
// Initialize chessboard void InitBoard(char board[ROW][COL],int row,int col); // Print chessboard void
DisplayBoard(char board[ROW][COL],int row,int col); // Players play chess void PlayerMove(char
board[ROW][COL], int row, int col); // Computer chess void ComputerMove(char
board[ROW][COL], int row, int col); // Judge whether to win or lose // Game status :*: Player wins // #: Computer win // Q: it ends in a draw //
C: The game continues char IsWin(char board[ROW][COL], int row, int col);

Part II -- Mapping game logic   test.c  
#define _CRT_SECURE_NO_WARNINGS 1 //test.c Mapping game logic //game.c Implementation of game related functions //game.h
Declaration of game related functions , Declaration of symbols , Include of header file #define _CRT_SECURE_NO_WARNINGS 1 // solve VS in scanf Unsafe problem
#include"game.h" // Create your own header file in double quotation marks void menu() // Implementation of menu { printf("1.play\n");
printf("0.exit\n"); } void game() // Game implementation part { // Define array -- Store data char board[ROW][COL]
= { 0 }; // Initialize chessboard InitBoard(board, ROW, COL); // Print chessboard DisplayBoard(board, ROW,
COL); char ret; // Judge the game state while (1) { // Players play chess PlayerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL); // Judge whether to win or lose ret = IsWin(board, ROW, COL); // Judge the game if
(ret != 'C') break; // Computer chess ComputerMove(board, ROW, COL); DisplayBoard(board,
ROW, COL); // Judge whether to win or lose ret = IsWin(board, ROW, COL); if (ret != 'C') break; } if
(ret == '*') printf(" Player wins \n"); else if (ret == '#') printf(" Computer win \n"); else
printf(" it ends in a draw \n"); } int main() { int n = 0; srand((unsigned int)time(NULL)); do {
menu(); printf(" Please select :->"); scanf("%d", &n); switch (n) { case 1:game(); break;
case 0:printf(" Exit the game "); break; default:printf(" Selection error , Reselect \n"); } } while (n);
return 0; }
Part III -- Implementation of game related functions   game.c

1, Chessboard initialization ( The essence is array initialization )
// Initialize chessboard void InitBoard(char board[ROW][COL], int row, int col) { int i = 0, j
= 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) board[i][j] = ' ';
// Initialize chessboard to space } }
2, Print chessboard
// Print chessboard void DisplayBoard(char board[ROW][COL], int row, int col) { int i = 0,
j = 0; for (i = 0; i < row; i++) // Print spaces and vertical lines on each line { for (j = 0; j < col; j++) {
printf(" %c ", board[i][j]); if (j < col - 1) printf("|"); } printf("\n"); if
(i < row - 1) { for (j = 0; j < col; j++) // Print horizontal and vertical lines { printf("---"); if (j <
col - 1) printf("|"); } printf("\n"); } } }
3, Players play chess -- It is necessary to judge whether the coordinates entered by the player are legal and whether the coordinates are occupied
// Players play chess void PlayerMove(char board[ROW][COL], int row, int col) { int m = 0, n
= 0; printf(" Players go \n"); while (1) // If the coordinates are illegal, re-enter them { printf(" Please enter coordinates ->:");
scanf("%d%d", &m, &n); // Judge the legitimacy of coordinates if (m <= ROW && m >= 1 && n <= COL && n >= 1)
{ // Judge whether the coordinates are occupied if (board[m - 1][n - 1] == ' ') // The player does not know that the array subscript starts from zero , Input is from (1
1) Started , So we need to subtract one { board[m - 1][n - 1] = '*'; break; } else printf(" Coordinates occupied , Please re-enter \n");
} else printf(" Illegal coordinates , Please re-enter \n"); } }
4, Computer chess
-- Let the computer generate a random coordinate within the range of the array , The random function value is used to generate the function rand(), At the same time, in order to ensure the randomness of the value , You need to set a seed for it , At the same time, it is necessary to judge whether the coordinates are occupied .
// Computer chess void ComputerMove(char board[ROW][COL], int row, int col) { int m = 0,
n = 0; printf(" Computer walk \n"); while (1) { m = rand() % row; n = rand() % col;
// Judge whether the coordinates are occupied if (board[m][n] == ' ') { board[m][n] = '#'; break; } } }
5, Judge the state of the game --:*: Player wins               

                                     #: Computer win
                                     Q: it ends in a draw
                                     C: The game continues
// Judge whether to win or lose char IsWin(char board[ROW][COL], int row, int col) { int i = 0, j = 0;
for (i = 0; i < row; i++) { if (board[i][0] == board[i][1] && board[i][1] ==
board[i][2] && board[i][2] == board[i][3] && board[i][3] == board[i][4] &&
board[i][4] != ' ') return board[i][1]; } for (i = 0; i < col; i++) { if
(board[0][i] == board[1][i] && board[0][i] == board[1][i] && board[1][i] ==
board[2][i] && board[2][i] == board[3][i] && board[3][i] == board[4][i] &&
board[4][i] != ' ') return board[1][i]; } if (board[0][0] == board[1][1] &&
board[1][1] == board[2][2] && board[2][2] == board[3][3] && board[3][3] ==
board[4][4] && board[4][4] != ' ') return board[0][0]; if (board[0][4] ==
board[1][1] && board[1][1] == board[2][2] && board[2][2] == board[3][3] &&
board[3][3] == board[4][4] && board[4][4] != ' ') return board[0][0]; int ret =
IsFull(board, row, col); if (ret) return 'Q'; return 'C'; }
6, Judge the game state -- Judge whether the chessboard is full
in// Judge whether the chessboard is full int IsFull(char board[ROW][COL], int row, int col) { int i = 0, j
= 0, flag = 1; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) if
(board[i][j] == ' ') flag = 0; } return flag; }
7,game.c Overall implementation of
#define _CRT_SECURE_NO_WARNINGS 1 //game.c Implementation of game related functions #include"game.h" // Initialize chessboard
void InitBoard(char board[ROW][COL], int row, int col) { int i = 0, j = 0; for
(i = 0; i < row; i++) { for (j = 0; j < col; j++) board[i][j] = ' ';
// Initialize chessboard to space } } // Print chessboard void DisplayBoard(char board[ROW][COL], int row, int
col) { int i = 0, j = 0; for (i = 0; i < row; i++) // Print spaces and vertical lines on each line { for (j = 0;
j < col; j++) { printf(" %c ", board[i][j]); if (j < col - 1) printf("|"); }
printf("\n"); if (i < row - 1) { for (j = 0; j < col; j++) // Print horizontal and vertical lines {
printf("---"); if (j < col - 1) printf("|"); } printf("\n"); } } } // Players play chess void
PlayerMove(char board[ROW][COL], int row, int col) { int m = 0, n = 0;
printf(" Players go \n"); while (1) // If the coordinates are illegal, re-enter them { printf(" Please enter coordinates ->:"); scanf("%d%d",
&m, &n); // Judge the legitimacy of coordinates if (m <= ROW && m >= 1 && n <= COL && n >= 1) { // Judge whether the coordinates are occupied
if (board[m - 1][n - 1] == ' ') // The player does not know that the array subscript starts from zero , Input is from (1 1) Started , So we need to subtract one { board[m -
1][n - 1] = '*'; break; } else printf(" Coordinates occupied , Please re-enter \n"); } else
printf(" Illegal coordinates , Please re-enter \n"); } } // Computer chess void ComputerMove(char board[ROW][COL], int
row, int col) { int m = 0, n = 0; printf(" Computer walk \n"); while (1) { m = rand() %
row; n = rand() % col; // Judge whether the coordinates are occupied if (board[m][n] == ' ') { board[m][n] = '#';
break; } } } // Judge whether the chessboard is full int IsFull(char board[ROW][COL], int row, int col) {
int i = 0, j = 0, flag = 1; for (i = 0; i < row; i++) { for (j = 0; j < col;
j++) if (board[i][j] == ' ') flag = 0; } return flag; } // Judge whether to win or lose char IsWin(char
board[ROW][COL], int row, int col) { int i = 0, j = 0; for (i = 0; i < row;
i++) { if (board[i][0] == board[i][1] && board[i][1] == board[i][2] &&
board[i][2] == board[i][3] && board[i][3] == board[i][4] && board[i][4] != ' ')
return board[i][1]; } for (i = 0; i < col; i++) { if (board[0][i] ==
board[1][i] && board[0][i] == board[1][i] && board[1][i] == board[2][i] &&
board[2][i] == board[3][i] && board[3][i] == board[4][i] && board[4][i] != ' ')
return board[1][i]; } if (board[0][0] == board[1][1] && board[1][1] ==
board[2][2] && board[2][2] == board[3][3] && board[3][3] == board[4][4] &&
board[4][4] != ' ') return board[0][0]; if (board[0][4] == board[1][1] &&
board[1][1] == board[2][2] && board[2][2] == board[3][3] && board[3][3] ==
board[4][4] && board[4][4] != ' ') return board[0][0]; int ret = IsFull(board,
row, col); if (ret) return 'Q'; return 'C'; }
Part IV -- Game effect

1, Menu effects

2, Initialize the effect of the chessboard

 3, Effect of the game

4, Effect of the end of the game

Part V : Overall game implementation code .

game.h
#pragma once //game.h Declaration of game related functions , Declaration of symbols , Inclusion of header file // Declaration of symbols #define ROW 5 #define COL
5 // Inclusion of header file #include<stdio.h> #include<stdlib.h> #include<time.h> // Declaration of function
// Initialize chessboard void InitBoard(char board[ROW][COL],int row,int col); // Print chessboard void
DisplayBoard(char board[ROW][COL],int row,int col); // Players play chess void PlayerMove(char
board[ROW][COL], int row, int col); // Computer chess void ComputerMove(char
board[ROW][COL], int row, int col); // Judge whether to win or lose // Game status :*: Player wins // #: Computer win // Q: it ends in a draw //
C: The game continues char IsWin(char board[ROW][COL], int row, int col);
 test.c
#define _CRT_SECURE_NO_WARNINGS 1 //test.c Mapping game logic //game.c Implementation of game related functions //game.h
Declaration of game related functions , Declaration of symbols , Inclusion of header file #define _CRT_SECURE_NO_WARNINGS 1 // solve VS in scanf Unsafe problem
#include"game.h" // Create your own header file in double quotation marks void menu() // Implementation of menu { printf("1.play\n");
printf("0.exit\n"); } void game() // Game implementation part { // Define array -- Store data char board[ROW][COL]
= { 0 }; // Initialize chessboard InitBoard(board, ROW, COL); // Print chessboard DisplayBoard(board, ROW,
COL); char ret; // Judge the game state while (1) { // Players play chess PlayerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL); // Judge whether to win or lose ret = IsWin(board, ROW, COL); // Judge the game if
(ret != 'C') break; // Computer chess ComputerMove(board, ROW, COL); DisplayBoard(board,
ROW, COL); // Judge whether to win or lose ret = IsWin(board, ROW, COL); if (ret != 'C') break; } if
(ret == '*') printf(" Player wins \n"); else if (ret == '#') printf(" Computer win \n"); else
printf(" it ends in a draw \n"); } int main() { int n = 0; srand((unsigned int)time(NULL)); do {
menu(); printf(" Please select :->"); scanf("%d", &n); switch (n) { case 1:game(); break;
case 0:printf(" Exit the game "); break; default:printf(" Selection error , Reselect \n"); } } while (n);
return 0; }
 game.c
#define _CRT_SECURE_NO_WARNINGS 1 //game.c Implementation of game related functions #include"game.h" // Initialize chessboard
void InitBoard(char board[ROW][COL], int row, int col) { int i = 0, j = 0; for
(i = 0; i < row; i++) { for (j = 0; j < col; j++) board[i][j] = ' ';
// Initialize chessboard to space } } // Print chessboard void DisplayBoard(char board[ROW][COL], int row, int
col) { int i = 0, j = 0; for (i = 0; i < row; i++) // Print spaces and vertical lines on each line { for (j = 0;
j < col; j++) { printf(" %c ", board[i][j]); if (j < col - 1) printf("|"); }
printf("\n"); if (i < row - 1) { for (j = 0; j < col; j++) // Print horizontal and vertical lines {
printf("---"); if (j < col - 1) printf("|"); } printf("\n"); } } } // Players play chess void
PlayerMove(char board[ROW][COL], int row, int col) { int m = 0, n = 0;
printf(" Players go \n"); while (1) // If the coordinates are illegal, re-enter them { printf(" Please enter coordinates ->:"); scanf("%d%d",
&m, &n); // Judge the legitimacy of coordinates if (m <= ROW && m >= 1 && n <= COL && n >= 1) { // Judge whether the coordinates are occupied
if (board[m - 1][n - 1] == ' ') // The player does not know that the array subscript starts from zero , Input is from (1 1) Started , So we need to reduce one { board[m -
1][n - 1] = '*'; break; } else printf(" Coordinates occupied , Please re-enter \n"); } else
printf(" Illegal coordinates , Please re-enter \n"); } } // Computer chess void ComputerMove(char board[ROW][COL], int
row, int col) { int m = 0, n = 0; printf(" Computer walk \n"); while (1) { m = rand() %
row; n = rand() % col; // Judge whether the coordinates are occupied if (board[m][n] == ' ') { board[m][n] = '#';
break; } } } // Judge whether the chessboard is full int IsFull(char board[ROW][COL], int row, int col) {
int i = 0, j = 0, flag = 1; for (i = 0; i < row; i++) { for (j = 0; j < col;
j++) if (board[i][j] == ' ') flag = 0; } return flag; } // Judge whether to win or lose char IsWin(char
board[ROW][COL], int row, int col) { int i = 0, j = 0; for (i = 0; i < row;
i++) { if (board[i][0] == board[i][1] && board[i][1] == board[i][2] &&
board[i][2] == board[i][3] && board[i][3] == board[i][4] && board[i][4] != ' ')
return board[i][1]; } for (i = 0; i < col; i++) { if (board[0][i] ==
board[1][i] && board[0][i] == board[1][i] && board[1][i] == board[2][i] &&
board[2][i] == board[3][i] && board[3][i] == board[4][i] && board[4][i] != ' ')
return board[1][i]; } if (board[0][0] == board[1][1] && board[1][1] ==
board[2][2] && board[2][2] == board[3][3] && board[3][3] == board[4][4] &&
board[4][4] != ' ') return board[0][0]; if (board[0][4] == board[1][1] &&
board[1][1] == board[2][2] && board[2][2] == board[3][3] && board[3][3] ==
board[4][4] && board[4][4] != ' ') return board[0][0]; int ret = IsFull(board,
row, col); if (ret) return 'Q'; return 'C'; }

Technology
©2019-2020 Toolsou All rights reserved,
【C++ Must see for entry 】C++ from 0 reach 1 Introductory programming axios Interceptor packaging and use Spring Boot Interview must ask : Automatic configuration principle VMware 16 install centos 7 Detailed tutorial C Language data structure - Sequence table delete duplicates V2.0.0 The 12th Blue Bridge Cup c++b Group personal problem solving On sending data from serial port single chip microcomputer to upper computer centos7 install RabbitMqjava Polymorphic array of opencv-python Fourier transform and inverse transform