当前位置:网站首页>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

边栏推荐
- 手把手教你安装VSCode(附带图解步骤)
- 响应式织梦模板装修设计类网站
- FPGA skimming memory (Verilog implementation of ram and FIFO)
- 外包公司“混”了2年,我只认真做了5件事,如今顺利拿到字节 Offer。
- Workflow of wireless vibrating wire acquisition system
- 4年测试经验,好不容易进了阿里,两个月后我选择了裸辞...
- 6年测试经验,教大家测试~如何把控项目
- virsh console连接失败问题
- Awvs cannot start problem
- [quality] code quality evaluation standard
猜你喜欢

即时通讯场景下安全合规的实践和经验

How awesome is the architecture of "12306"?

What if there is not enough time for adequate testing?

当Synchronized遇到这玩意儿,有个大坑,要注意

响应式织梦模板家装建材类网站

Experiment 2: Arduino's tricolor lamp experiment

C语言实现三子棋游戏

3D intelligent factory process flow visualization interactive display application advantages

How to customize a new tab in Duoyu security browser?

What should I do if excel opens a CSV file containing Chinese characters and there is garbled code?
随机推荐
Day 15 (VLAN related knowledge)
如何在多御安全浏览器中自定义新标签页?
线上3d数字展厅制作方案及优点
4年测试经验,好不容易进了阿里,两个月后我选择了裸辞...
FPGA skimming memory (Verilog implementation of ram and FIFO)
How much is the report development cost in the application system?
详解异步任务:任务的状态及生命周期管理
多边形点测试
ES6详解 快速上手!
Explanation of engineering economics terms
Kubesphere multi node installation
快速掌握Nodejs安装以及入门
一文读懂Okaleido Tiger近期动态,挖掘背后价值与潜力
How awesome is the architecture of "12306"?
0728~ sorting out interview questions
千万不要把Request传递到异步线程里面,有坑
响应式织梦模板家装建材类网站
响应式织梦模板酒店客房类网站
Hexadecimal to string
会议OA之会议通知