当前位置:网站首页>三子棋的设计和代码
三子棋的设计和代码
2022-08-10 19:34:00 【str_lai】
目录
一、游戏设计思路
二、游戏设计步骤
1.菜单创建
2.棋盘的展现
3.棋盘的初始化
4.玩家下棋
5.电脑自动下棋
6.游戏胜负的判断
三、游戏运行状况
一、游戏设计思路
三子棋游戏代码多达200多条,我们在学习时,就不能一股脑地打下去,不然在后期我们极有可能面临这大量报错的局面。所以在设计游戏代码的时候,我们应该想好从哪里入手,先设计什么再设计什么。在这里我把游戏的设计分为三个层面,一是测试(text.c)、二是函数的声明(game.h)、三是函数的实现(game.c)。
二、游戏设计步骤
1.菜单创建
每个游戏开始的时候都有选择界面,让你选择退出或开始游戏,三子棋也不例外,接下来我们要在测试层实现菜单的创建。重要的是我们需要实现连续的玩,而不是只能玩一局。这样我们需要用到do......while循环。
代码如下:
void menu()
{
printf("****************************\n");
printf("******* 0·exit ********** \n");
printf("******* 1·play ********** \n");
printf("****************************\n");
}
int main()
{
int input = 0;
do
{
menu();
printf("请输入:>");
scanf("%d", &input);
switch (input)
{
case 0:
printf("退出游戏\n");
break;
case 1:
printf("三子棋\n");
break;
default:
printf("选择错误\n");
break;
}
} while (input);
return 0;
}当然,菜单界面并不是唯一的,可以根据自己喜好设计。之后程序试运。

2.棋盘的展现
3*3棋盘对应着一个char类型的二维数组,我们定义一个函数display_board(board,3,3)。在定义的时候,函数当然不知道棋盘到底有多大,所以我们就需要“告诉”他有多大。但是,不排除我们之后做出更大的棋盘,超过了3*3,所以函数也得跟着改。为了一劳永逸,我们在game.h文件定义如下,
#define ROW 3
#define COL 3之后只要在text.c用#include"game.h"包含下就可以根据之后的变动调整了。
void game()
{
char board[ROW][COL]={0};
display_board(board,ROW,COL)
}而display_board函数,我们也需要在game.c层面进行声明,声明如下。
void display_board(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
//打印数据
//printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
int j = 0;
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
printf("|");
}
printf("\n");
//打印分割信息
//printf("---|---|---\n");
if (i < row - 1)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
printf("\n");
}
}
}3.棋盘的初始化
在上述代码运行后,我们会发现棋盘是歪的,没有对齐,接下来我们对棋盘进行美化。
现在text.c写上如下代码,之后在game.c进行声明。
void game()
{
char board[ROW][COL]={0};
InitBoard(board,ROW,COL);
display_board(board,ROW,COL);
}void InitBoard(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
棋盘展示:

4玩家下棋
接下来,我们需要在game.c层面实现玩家下棋的代码。
在这段代码中,有三点最重要的需要我们注意:
1.玩家落子需要在棋盘范围内。
2.玩家要在棋盘上空的地方落子。
3.如果输入的坐标不满足要重新输入。

结果如下:

5电脑自动下棋
在实现电脑随机落子的条件下,我们就需要注意以下几点:
1.要在主函数中使用srand((unsigned int)time(0))
将时间作为随机数种子,确保得到的行列坐标是真随机。
2.判断是否在棋盘上空的地方下棋
3.调用<stdlib.h>以及<time.h>头文件
代码如下:
void ComputerMove(char board[ROW][COL], int row, int col)
{
printf("电脑下棋:>\n");
int x = 0;
int y = 0;
while (1)
{
x = rand() % row;//0~2
y = rand() % col;//0~2
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}运行结果如下:

6游戏胜负的判断
每一局游戏的结束,我们都需要让电脑判断游戏胜负。在这里我们依然在game.c层面使用Iswin()函数来实现,用Isfull函数判断是否平局。在game.h层面声明函数。
int IsFull(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
char IsWin(char board[ROW][COL], int row, int col)
{
//行
int i = 0;
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
{
return board[i][1];//
}
}
//列
int j = 0;
for (j = 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] != ' ')
{
return board[1][j];
}
}
//对角线
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
{
return board[1][1];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
{
return board[1][1];
}
//没有人赢,就要平局
if (IsFull(board, row, col))
{
return 'Q';
}
//游戏继续
return 'C';
}
三、游戏运行状况


边栏推荐
- Public Key Retrieval is not allowed(不允许公钥检索)【解决办法】
- Pt/CeO2单原子纳米酶|[email protected] NPs纳米酶|碳纳米管负载铂颗粒纳米酶|白血病拮抗多肽修饰的FeOPtPEG复合纳米酶
- Optimization is a habit The starting point is to 'stand close to the critical'
- 测试/开发程序员值这么多钱么?“我“不会愿赌服输......
- 【自然语言处理】【向量表示】PairSupCon:用于句子表示的成对监督对比学习
- opengrok搭建[通俗易懂]
- GBASE 8s 高可用RSS集群搭建
- 1D Array Dynamics and Question Answers
- 【语义分割】2017-PSPNet CVPR
- 2022杭电多校七 Black Magic (签到)
猜你喜欢

转铁蛋白(Tf)修饰去氢骆驼蓬碱磁纳米脂质体/香豆素-6脂质体/多柔比星脂质体

铱钌合金/氧化铱仿生纳米酶|钯纳米酶|GMP-Pd纳米酶|金钯复合纳米酶|三元金属Pd-M-Ir纳米酶|中空金铂合金纳米笼核-多空二氧化硅壳纳米酶

铁蛋白颗粒Tf包载多肽/凝集素/细胞色素C/超氧化物歧化酶/多柔比星(定制服务)

FEMRL: A Framework for Large-Scale Privacy-Preserving Linkage of Patients’ Electronic Health Rec Paper Summary

网站架构探测&chrome插件用于信息收集

@Autowired注解 --required a single bean, but 2 were found出现的原因以及解决方法

Tf ferritin particles contain cisplatin / oxaliplatin / doxorubicin / methotrexate MTX / paclitaxel PTX and other drugs
We used 48h to co-create a web game: Dice Crush, to participate in international competitions

Heme - gold nanoparticles (Heme - AuNP) composite nanometer enzyme | gold nanoparticles nuclear porous hollow carbon nanometer spherical shell (Au @ HCNs) nano enzyme

力扣150-逆波兰表达式求值——栈实现
随机推荐
【语义分割】2017-PSPNet CVPR
The 2021 ICPC Asia Shanghai Regional Programming Contest D、E
C语言写数据库
Optimization is a habit The starting point is to 'stand close to the critical'
[Teach you how to make a small game] Write a function with only a few lines of native JS to play sound effects, play BGM, and switch BGM
力扣18-四数之和——双指针法
30分钟使用百度EasyDL实现健康码/行程码智能识别
QoS Quality of Service Six Router Congestion Management
从 GAN 到 WGAN
导入FontForge生成字体
“蔚来杯“2022牛客暑期多校训练营7 F
测试/开发程序员值这么多钱么?“我“不会愿赌服输......
代理模式的使用总结
电脑为什么会蓝屏的原因
链表应用----约瑟夫问题
[Teach you how to do mini-games] How to lay out the hands of Dou Dizhu?See what the UP master of the 250,000 fan game area has to say
这7个自动化办公模版 教你玩转表格数据自动化
QoS Quality of Service Eight Congestion Avoidance
线性结构----链表
About npm/cnpm/npx/pnpm and yarn