当前位置:网站首页>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 3Just 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
边栏推荐
- [论]【DSTG】Dynamic SpatiotemporalGraph Convolutional Neural Networks for Traffic Data Imputation
- DHU programming exercise
- Recommendations for agileplm database parameter optimization
- DHU programming exercise
- Unity3D UGUI强制刷新Layout(布局)组件
- How does payment splitting help B2B bulk commodity transactions?
- 模板参数包和函数参数包
- 2022护网行动在即,关于护网的那些事儿
- DDoS surge in mobile and data centers
- DHU programming exercise
猜你喜欢

How to use SMS to deliver service information to customers? The guide is here!

论文回顾:Playful Palette: An Interactive Parametric Color Mixer for Artists

Precautions for purchasing wildcard SSL certificate

PR second training notes

Créer des compétences exquises dans l'éducation des créateurs

Ffmpeg source code

Digicert、Sectigo、Globalsign代码签名证书的区别
![[Postgres] Postgres database migration](/img/45/7074aa766640160a3b6f00b109cb2f.png)
[Postgres] Postgres database migration

Quick sort

Steam elements hidden in science and Technology Education
随机推荐
DHU programming exercise
归并排序
[论]【DSTG】Dynamic SpatiotemporalGraph Convolutional Neural Networks for Traffic Data Imputation
DDoS "fire drill" service urges companies to prepare
Pytoch learning (II)
Several key points recorded after reviewing redis design and Implementation
什么是证书透明度CT?如何查询CT logs证书日志?
银行的理财产品一般期限是多久?
dhu编程练习
What is a self signed certificate? Advantages and disadvantages of self signed SSL certificates?
matlab代码运行教程(如何运行下载的代码)
How to prevent phishing emails? S/mime mail certificate
2.< tag-动态规划和0-1背包问题>lt.416. 分割等和子集 + lt.1049. 最后一块石头的重量 II
Créer des compétences exquises dans l'éducation des créateurs
C语言 pivot_root的Invalid argument错误解决方案
What is an X.509 certificate? 10. 509 certificate working principle and application?
Pytorch学习(二)
Shell Sort
什么是自签名证书?自签名SSL证书的优缺点?
打造創客教育中精湛技藝