当前位置:网站首页>C language implementation of the small game [sanziqi] Notes detailed logic clear, come and have a look!!
C language implementation of the small game [sanziqi] Notes detailed logic clear, come and have a look!!
2022-07-27 02:15:00 【AF sail_】
Sanzi _c Language implementation
List of articles
The detailed steps
One 、game.h( The header file )
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 3 // use define Defined checkerboard row and column size Because it is convenient to expand the chessboard later
#define COL 3
void initboard (char board[ROW][COL], int row, int col );// Initialize chessboard
void displayboard (char board[ROW][COL], int row, int col );// Print chessboard
void playermove(char board[ROW][COL], int row, int col);// Players play chess
void computermove(char board[ROW][COL], int row, int col);// The computer plays chess
char is_win(char board[ROW][COL], int row, int col);// Judge a win or a draw
Two 、game.c( Game source file )
The code is as follows ( Example ):
#include "game.h"
void initboard(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] = ' ';
}
}
}
void displayboard(char board[ROW][COL], int row , int col)
{
// Two parts Part of the print data Partial print split lines
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");
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if(j<col-1)
printf("|");
}
}
printf("\n");
}
}
void playermove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf(" Please enter the coordinates >");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';// Don't type the assignment number wrong again
}
else
{
printf(" The location is already occupied .\n");
}
break;
}
else
{
printf(" Wrong coordinate input ;\n");
}
}
}
void computermove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
x = rand() % row;// utilize rand Generate random numbers The reason %row Because the remaining number must be 0~row Between
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
static int if_full(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++)
{
if (board[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
char is_win(char board[ROW][COL], int row, int col)
{
// Judgment line
int i = 0;
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][0] ;
}
// Judgment column
i = 0;
for (i = 0; i < col;i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
return board[0][i];
}
// Diagonals
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
{
return board[0][0];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')
{
return board[0][2];
}
int m = 0;
m = if_full(board, ROW, COL);
if (m == 1)
{
return 'Q';
}
else
{
return 'c';
}
}
3、 ... and 、test.c( Test source file )
#include "game.h"
void menu()
{
printf("*****************************\n");
printf("******** 1.play *************\n");
printf("******** 0.exit *************\n");
printf("*****************************\n");
}
void game()
{
char board[ROW][COL] = {
0};
initboard(board, ROW, COL);
displayboard(board, ROW, COL);
char ret = ' ';
while (1)
{
playermove(board, ROW, COL);
displayboard(board, ROW, COL);
ret=is_win(board, ROW, COL);
if (ret != 'c')
{
break;
}
computermove(board, ROW, COL);
displayboard(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'c')
{
break;
}
}
if (ret == '*')
{
printf(" Players win \n");
}
else if (ret == '#')
{
printf(" The computer wins \n");
}
else
{
printf(" It ends in a draw \n");
}
}
void test()
{
int input = 0;
do // Specific chess playing steps
{
menu();
printf(" Please enter :");
scanf("%d", &input);
switch (input)
{
case 1:
printf(" Now let's start sanziqi >\n");
game();
break;
case 0:
printf(" sign out \n");
break;
default:
printf(" Input error , Please re-enter \n");
break;
}
} while (input);
}
int main()
{
srand((unsigned int)time(NULL));
test();
return 0;
}
Four 、 Effect display !


It's really hard to draw !!!!!! After several rounds, there was no draw The computer is too stupid
summary
This is from C Language implementation of the three chess , If you have any questions, welcome to the comment area perhaps Talk to me in private !!!
边栏推荐
- OSPF在MGRE环境下的实验
- 6.30 didi surface warp (one side + two sides)
- 2022 latest Tiktok live broadcast monitoring (II) streaming media download in live broadcast room
- Experiment of total connection and star topology of mGRE
- RIP V2 的简单应用(v2的配置、宣告、手工汇总、RIPV2的认证、沉默接口、加快收敛)
- 6.30联发科笔试
- 解决方案:炼丹师养成计划 Pytorch+DeepLearning遇见的各种报错与踩坑避坑记录(三)
- Text to image论文精读SSA-GAN:基于语义空间感知的文本图像生成 Text to Image Generation with Semantic-Spatial Aware GAN
- 6.28同花顺笔试
- 6.30 written examination of MediaTek
猜你喜欢

R分数复现 R-precision评估指标定量 文本生成图像R分数定量实验全流程复现(R-precision)定量评价实验踩坑避坑流程

Flink1.13.6详细部署方式
![[详解C语言]一文带你玩转函数](/img/44/53cdac9b9cf0d3f77e5da05956c3dc.png)
[详解C语言]一文带你玩转函数

HCIA(网络初级综合实验练习)

Experiment of total connection and star topology of mGRE

【mysql】mysql启动关闭命令以及一些报错解决问题
![[explain C language in detail] takes you to play with loop structure (for_while_do while)](/img/d9/75053297873a5b5458514e7f557cdc.png)
[explain C language in detail] takes you to play with loop structure (for_while_do while)

Introduction to network - Introduction to home networking & basic network knowledge
![[translation] explicit and implicit batch in tensorrt](/img/17/3f00697d53ff43cd881960849da5f7.png)
[translation] explicit and implicit batch in tensorrt

ACM模式输入输出练习
随机推荐
动态路由ofps协议配置
初识C语言(1)
TIM输出比较——PWM
三范式,约束,部分关键字区别,
7.13 蔚来提前批笔试
Es specify user name and password when instantiating resthighlevelclient
ViTGAN:用视觉Transformer训练生成性对抗网络 Training GANs with Vision Transformers
[explain C language in detail] takes you to play with loop structure (for_while_do while)
[reprint] GPU compute capability table
6.28同花顺笔试
GAN的训练技巧:炼丹师养成计划 ——生成式对抗网络训练、调参和改进
FID index reproduction step on the pit to avoid the pit text generation image FID quantitative experiment whole process reproduction (FR é Chet inception distance) quantitative evaluation experiment s
预分频值和自动重装值对中断频率的影响
JS逻辑运算符
7.16 written examination of Duoyi network
静态路由基础配置(IP地址的规划、静态路由的配置),实现全网可达。
【mysql】mysql启动关闭命令以及一些报错解决问题
6.30滴滴面经(一面+二面)
第三讲--GPIO输入输出库函数使用以及相关例程
TCP的三次握手与四次断开