当前位置:网站首页>Implementation of Sanzi chess with C language
Implementation of Sanzi chess with C language
2022-06-30 02:31:00 【A faint branch】
Catalog
One 、 Function description
Two 、 Process analysis
3、 ... and 、 Specific steps
1、 menu
2、 Create a chessboard
3、 The player is down
4、 Computer move later
5、 Judge the outcome
Four 、 Code implementation
5、 ... and 、 Demonstration process
One 、 Function description
use c Language to achieve the game of Gobang .
Two 、 Process analysis
1. Create game menu bar , Select start or exit
2. Use an array to store chess playing data , Initially change the chessboard and print the chessboard
3. Players play chess
4. The computer plays chess
5. Judgement of winning or losing
3、 ... and 、 Specific steps
1. Create a menu
choice 1 Start the game , choice 0 Quit the game
void menu()
{
printf("**************************\n");
printf("***********1.play*********\n");
printf("***********0.exit*********\n");
printf("**************************\n");
}
2、 Create a chessboard
(1) Store chess data
char board[ROW][COL] = { 0 };
(2) Initialize chessboard ( Each position on the chessboard is a space )
Note that in order to improve the game in the future , For example 3*3 The chessboard of is changed to 5*5, The following statement can be made
#define ROW 3
#define COL 3
Just modify it here
(3) Print chessboard
void displayBoard1(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
// Print data
printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
if (i < row - 1)
{
// Print split lines
printf("---|---|---\n");
}
}
}
notes : This way of writing is flawed . If you change to 10*10 Where's your chessboard ? This kind of writing can only print 3 Column
It can be optimized as follows :
void print_Board(char board[ROW][COL], int row, int col)
{
// Print data
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
// Print separator
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
}
}
}
This makes it easy to print checkerboards of various sizes : for example 10*10:
3、 Players play chess
The subscript of an array is from 0 At the beginning , But non programmers don't realize this , Therefore, we should ensure that the number entered by the player is greater than or equal to 1 Of ; Players enter coordinates , Use to show the player's position
notes :
1. Players need to fall within the chessboard .
2. The player wants to drop his son over the chessboard .
3. If the entered coordinates do not meet the requirements, re-enter
void play_Move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf(" Players please play chess \n");
printf(" Please enter the coordinates ->:");
scanf_s("%d %d", &x, &y);
if (x >= 1 && x <= row &&y >= 1 && y <= col)
{
if (board[x - 1][y - 1] = ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf(" The location is already occupied , Please re-enter ");
}
}
else
{
printf(" Incorrect input , Please re-enter !");
}
}
}
4. The computer plays chess
The computer randomly generates row and column coordinates , use # Code computer placement
Be careful :
1. Use srand((unsigned int)time(0)), Generate random numbers
2. Play chess above the chessboard .
// The computer plays chess
void cmp_Move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] ==' ')
{
board[x][y] = '#';
break;
}
else
{
printf(" The location is already occupied , Please re-enter ");
}
}
}
5、 ... and 、 Judgement of winning or losing
analysis : When does the game end ?
Write a function to judge whether you win or lose , Returns when the player wins , The computer won back #, The chessboard returns when it is full Q, Otherwise, keep playing
char is_Win(char board[ROW][COL], int row, int col)
{
int i = 0;
// Judgment line
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
{
return board[i][1];// return ? Game player wins /# Computers win
}
}
// Judgment column
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];// return ? Game player wins /# Computers win
}
}
// Diagonals 1
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
{
return board[1][1];// return ? Game player wins /# Computers win
}
// Diagonals 2
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
{
return board[1][1];// return ? Game player wins /# Computers win
}
// Judge a draw
if (is_Full(board, row, col) == 1)
{
return 'Q';
}
// continue
return 'C';
}
notes : This function contains a static function to judge whether the chessboard is full
// Judge whether the chessboard is full
static int is_Full(char board[ROW][COL], int row, int col)// Use only in this file
{
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;
}
Four 、 Code implementation
(1) Declaration of functions
#include<stdio.h>
#define ROW 3
#define COL 3
// Initialize chessboard
void init_Board(char board[ROW][COL], int row, int col);
// Print chessboard
void print_Board(char board[ROW][COL], int row, int col);
// Players play chess
void play_Move(char board[ROW][COL], int row, int col);
// Computer move later ;
void cmp_Move(char board[ROW][COL], int row, int col);
// Judgement of winning or losing
char is_Win(char board[ROW][COL], int row, int col);
(2) The realization of the game
#include"game.h"
// Initialize chessboard
void init_Board(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++)
{
board[i][j] = ' ';
}
}
}
// Print chessboard
void print_Board(char board[ROW][COL], int row, int col)
{
// Print data
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
// Print separator
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
}
}
}
// Players play chess
void play_Move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf(" Players please play chess \n");
printf(" Please enter the coordinates ->:");
scanf_s("%d %d", &x, &y);
if (x >= 1 && x <= row &&y >= 1 && y <= col)
{
if (board[x - 1][y - 1] ==' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf(" The location is already occupied , Please re-enter ");
}
}
else
{
printf(" Incorrect input , Please re-enter !");
}
}
}
// The computer plays chess
void cmp_Move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf(" The computer plays chess \n");
x = rand() % row;
y = rand() % col;
if (board[x][y] ==' ')
{
board[x][y] = '#';
break;
}
else
{
printf(" The location is already occupied , Please re-enter ");
}
}
}
// Judge whether the chessboard is full
static int is_Full(char board[ROW][COL], int row, int col)// Use only in this file
{
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;
}
// Judgement of winning or losing
char is_Win(char board[ROW][COL], int row, int col)
{
int i = 0;
// Judgment line
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
{
return board[i][1];// return ? Game player wins /# Computers win
}
}
// Judgment column
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];// return ? Game player wins /# Computers win
}
}
// Diagonals 1
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
{
return board[1][1];// return ? Game player wins /# Computers win
}
// Diagonals 2
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
{
return board[1][1];// return ? Game player wins /# Computers win
}
// Judge a draw
if (is_Full(board, row, col) == 1)
{
return 'Q';
}
// continue
return 'C';
}
(3) Test the logic of the game
#include<stdio.h>
#include"game.h"
#include<stdlib.h>
#include<time.h>
// menu ;
void menu()
{
printf("*********************************\n");
printf("***************1.play************\n");
printf("***************0.exit************\n");
printf("*********************************\n");
}
void game()
{
char ret = 0;
// Define an array to store chessboard data
char board[ROW][COL] = { 0 };
// Initialize chessboard
init_Board(board, ROW, COL);
// Print chessboard
print_Board(board, ROW, COL);
while (1)
{
// Players play chess
play_Move(board, ROW, COL);
print_Board(board, ROW, COL);
// Judgement of winning or losing
ret = is_Win(board, ROW, COL);
if (ret != 'C')
{
break;
}
// The computer plays chess
cmp_Move(board, ROW, COL);
// Print chessboard
print_Board(board, ROW, COL);
// Judgement of winning or losing
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
{
printf(" It ends in a draw \n");
}
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do {
menu();
printf(" Please select ->:");
scanf_s("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
break;
default:
printf(" Incorrect input , Please re-enter ");
break;
}
} while (input);
return 0;
}
5、 ... and 、 Demonstration process
(1) Game player wins
(2) Computers win
(3) It ends in a draw
End and scatter
边栏推荐
- Internet Crime Complaint Center reports an increase in DDoS Attacks
- Can autojs code be encrypted? Yes, display of autojs encryption skills
- What files does a CA digital certificate contain? How to view SSL certificate information?
- Global and Chinese markets of liquid optical waveguides 2022-2028: Research Report on technology, participants, trends, market size and share
- How to display all keys through redis cli- How to show ALL keys through redis-cli?
- dhu编程练习
- What should academic presentation /ppt do?
- Global and Chinese market of relay lens 2022-2028: Research Report on technology, participants, trends, market size and share
- Weekly recommended short video: why is the theory correct but can not get the expected results?
- IBM websphere通道联通搭建和测试
猜你喜欢
归并排序
什么是证书透明度CT?如何查询CT logs证书日志?
Pytoch learning (II)
走进江苏作家诗人胭脂茉莉|世界读书日
FDA ESG规定:必须使用数字证书保证通信安全
Enlightenment from the revocation of Russian digital certificate by mainstream CA: upgrade the SSL certificate of state secret algorithm to help China's network security to be autonomous and controlla
Openlayers 3 built in interaction
Time complexity analysis
什么是X.509证书?X.509证书工作原理及应用?
Traffic, but no sales? 6 steps to increase website sales
随机推荐
1380. lucky numbers in matrices
Sitelock nine FAQs
dhu编程练习
Jupyter notebook displays a collection of K-line graphs
Heap sort
Global and Chinese market of mobile commerce solutions 2022-2028: Research Report on technology, participants, trends, market size and share
什么是证书透明度CT?如何查询CT logs证书日志?
day33
Bubble sort
DHU programming exercise
选购通配符SSL证书注意事项
dhu编程练习
Recheck on February 15, 2022
What files does a CA digital certificate contain? How to view SSL certificate information?
DHU programming exercise
【干货分享】最新WHQL徽标认证申请流程
Detailed explanation of minimum stack
What should academic presentation /ppt do?
Weekly recommended short video: why is the theory correct but can not get the expected results?
DHU programming exercise