当前位置:网站首页>C language to achieve the three chess game
C language to achieve the three chess game
2022-07-29 02:33:00 【7 ang 7】
List of articles
Sanzi chess implementation
1、 Realization principle
Use a two bit array to save chessboard information , Any position on the chessboard , Inside
You can put three messages :
1、 empty
2、 User placement
3、 Computer move later
Playing three pieces of chess is to find the corresponding empty position in a two-dimensional array , Fall , Stand after falling
That is, it is necessary to judge whether there are three character beads at the position of the falling seed , So as to judge who loses and who wins . Every time
There are four situations when you play chess once :
* user 1 win
* user 2 win
* It ends in a draw ( The chessboard is covered with )
** No result Keep playing chess
2、 Implementation module
file name effect
three_chress.h The function declaration of three piece chess , Header file declaration, etc
three_chress.c The implementation of the function interface of three chess
main.c Triwizard function test function
3、 Implementation logic
One 、 We need to main.c This file , It's the same to build the framework , And then realize their functions separately . The implementation menu and the beginning and end of the game are main In the function , Then take out the game start function from inside to concretely realize the progress of the game . And include the header file used every time three_chress.h Inside .
main.c
Insert a code chip here #include"three_chress.h"
void menu()
{
printf("###### 1、 Start the game #######\n");
printf("###### 2、 sign out #######\n");
}
void game()
{
char ret = 0;
srand((unsigned int)time(NULL));
char a[ROW][COL] = {
0 };
init_a(a,ROW,COL);// initialization
show(a, ROW, COL);// visualization
while (1)
{
play_move(a, ROW, COL);// User walk
show(a, ROW, COL);
ret = Is_over(a,ROW,COL);// Judge whether it is over
if (ret != 'N')//N This character means to continue
{
break;
}
computer_move(a, ROW, COL);// Computer go
show(a, ROW, COL);
ret = Is_over(a,ROW, COL);// Judge whether it is over
if (ret != 'N')//N This character means to continue
{
break;
}
}
if (ret == '*')
{
printf(" User win \n");
}
else if (ret =='#')
{
printf(" Computers win \n");
}
else if (ret ='D')//D It means a draw
{
printf(" It ends in a draw \n");
}
show(a, ROW, COL);
}
int main()
{
int input = 0;
do{
menu();
printf(" Please select -> ");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 2:
printf(" sign out ");
break;
default:
printf(" non-existent , Please re-enter ");
break;
}
} while (input);
return 0;
}
three_chress.h
Insert a code chip here #pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 3
#define COL 3
void init_a(char a[][COL], int row, int col);// initialization
void show(char a[][COL], int row , int col);// visualization
void play_move(char a[][COL],int row,int col);// Under the user
void computer_move(char a[][COL], int row, int col);// Under the computer
char Is_over(char a[][COL],int row,int col);// Judge whether it is over
int is_full(char a[][COL],int row, int col);
Two 、 So how to play the game ? We need to implement such logic first : Build a two-dimensional array and initialize ——> Display chessboard information to visualize ——> Let the user walk and display the checkerboard information, and immediately judge whether the user wins , When judging, we use characters N To get means that the game continues , If not N It means the game is over .——> If you don't win, let the computer go and display the chessboard information to judge whether the computer wins . Finally, we use the return character method to judge who wins or draws .
3、 ... and 、 Use functions to realize the logic used above step by step , Focus on a few functions :
1、 stay play_move What we need to pay attention to in the function is , When playing chess, you should first judge its legitimacy and multiplicity, that is, repetition . Then give the chess pieces .
2、 stay computer_move The implementation is relatively simple , Let the computer go automatically 、 Random walk , Think of rand function , and rand Function and srand Function has , Then let their nature be X,Y Random generation 0 To 2 Value , Go and give the chess pieces .
3、 and Is_over Function to judge whether to win or lose is four cases : User win ; Computers win ; It ends in a draw ; continue .
Directly compare whether each position on the upper and lower diagonals in four directions is equal , It's not empty . Then write a function to judge whether it is full to judge whether it is a draw , Finally, return to continue . All the characters returned here .
The following code :
three_chree.c
Insert a code chip here #include"three_chress.h"
static int is_full(char a[][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (a[i][j]== ' ')
{
return 0;
}
}
}
return 1;
}
void init_a(char a[][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
a[i][j] = ' ';
}
}
}
void show(char a[][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
printf(" %c ", a[i][j]);
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
if (i < row - 1)
{
for (int j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
}
}
}
void play_move(char a[][COL], int row, int col)
{
int x = 0;
int y = 0;
printf(" Under the user \n");
while (1)
{
printf(" Please enter :\n");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (a[x - 1][y - 1] == ' ')
{
a[x - 1][y - 1] = '*';
break;
}
else
{
printf(" Occupied , Please re-enter ");
}
}
else
{
printf(" illegal input ");
}
}
}
void computer_move(char a[][COL], int row, int col)
{
printf(" Computer go \n");
int x = 0;
int y = 0;
x = rand() % row;
y = rand() % col;
while (1)
{
if (a[x][y] == ' ')
{
a[x][y] = '#';
break;
}
}
}
char Is_over(char a[][COL], int row, int col)// Judge whether it is over
{
for (int i = 0; i < row; i++)
{
if (a[i][0] == a[i][1] && a[i][1] == a[i][2] && a[i][0] != ' ')
{
return a[i][0];
}
}
for (int j = 0; j < col; j++)
{
if (a[0][j] == a[1][j] && a[1][j] == a[2][j] && a[0][j] != ' ')
{
return a[0][j];
}
}
if (a[0][0] == a[1][1] && a[1][1] == a[2][2] && a[0][0] != ' ')
{
return a[0][0];
}
if (a[0][2] == a[1][1] && a[1][1] == a[2][0] && a[0][2] != ' ')
{
return a[0][2];
}
if ( is_full(a, ROW, COL) == 1)
{
return 'D';
}
return 'N';// Indicates that the game continues
}
4、 Achieve results

边栏推荐
- 代码随想录笔记_哈希_349两个数的交集
- Remember error scheduler once Asynceventqueue: dropping event from queue shared causes OOM
- 线上3d数字展厅制作方案及优点
- Explain the four asynchronous solutions of JS in detail: callback function, promise, generator, async/await
- 进程间通信---对管道的详细讲解(图文案例讲解)
- 代码实现 —— 多项式的最大公因式(线性代数)
- The first of the five tips for using browsers efficiently is the most practical
- 详解异步任务:任务的状态及生命周期管理
- Altium designer outputs Gerber and other production documents
- Time pit in MySQL driver
猜你喜欢

Prometheus + alertmanager message alert

当我看源码的时候,我在想什么?

进程间通信---对管道的详细讲解(图文案例讲解)

防止勒索软件攻击数据的十种方法

Explain asynchronous tasks in detail: task status and lifecycle management

HTTP缓存

Data security and privacy computing summit - development and application of security intersection in privacy Computing: Learning

How to use RPA to achieve automatic customer acquisition?

响应式织梦模板化妆美妆类网站

3D intelligent factory process flow visualization interactive display application advantages
随机推荐
What should I do if excel opens a CSV file containing Chinese characters and there is garbled code?
代码随想录笔记_哈希_349两个数的交集
How much is the report development cost in the application system?
代码实现 —— 多项式的最大公因式(线性代数)
Work queue_ queue
聊聊 Feign 的实现原理
where、having、group by、order by,is null,not in,子查询,delete,日期函数
Explanation of engineering economics terms
Altium designer outputs Gerber and other production documents
响应式织梦模板家装建材类网站
Talk about 11 tips for interface performance optimization
线上3d数字展厅制作方案及优点
Eight practical new functions of es2022
0728~ sorting out interview questions
“两个披萨”团队的分支管理实践
响应式织梦模板户外露营类网站
The financing demand of 129 million yuan was released, and the roadshow of the Dake city project continued to irrigate the "good seedlings" of scientific innovation
Polygon point test
How to use RPA to achieve automatic customer acquisition?
响应式织梦模板酒店客房类网站