当前位置:网站首页>【详解如何一步步实现三子棋】
【详解如何一步步实现三子棋】
2022-07-28 05:26:00 【JuLiJuLi.】
相信大家都玩过五子棋,三子棋也是一样的道理,行列三子、对角线三子获得胜利。
想要实现三子棋小游戏需要那几步.
1.三子棋首先我们要创建棋盘,创建一个二维数组三行三列,然后将棋盘初始化为全空格。
2.如何将棋盘转换为网格状棋盘,如下图

3.玩家下棋。
4.电脑下棋。
5.判断输赢以及平局。
具体代码如下,详解在代码中。
(1).Li.h头文件中用来声明函数的作用.
#include<stdio.h>
#include<stdlib.h> //srand函数头文件
#include<time.h> //time函数头文件
#define ROW 3 //定义棋盘的行,用ROW定义行有利于后期修改棋盘的大小 或者做五子棋等,可直接修改,代码内部无修修改
#define COL 3 //定义棋盘的列,用COL定义列也是同样道理,只需修改数值,无须修改代码内部
//初始化棋盘
void InitBoard(char board[ROW][COL], int row, int col);
//打印棋盘
void DisPlayBoard(char board[ROW][COL], int row, int col);
//玩家下棋
void Player_move(char board[ROW][COL], int row, int col);
//电脑下棋
void Computer_move(char board[ROW][COL], int row, int col);
//判断输赢
char is_win(char board[ROW][COL], int row, int col);
2.Li.c源文件用来实现游戏的逻辑.
#include"Li.h" //引用头文件中自己定义的行和列 ROW及COL
#include<stdio.h>
#include<stdlib.h> //srand函数头文件
#include<time.h> //time函数头文件
void menu() //游戏的菜单及名称
{
printf("*******************************\n");
printf("**********三子棋游戏***********\n");
printf("**********1.p l a y ***********\n");
printf("**********0.e x i t ***********\n");
printf("*******************************\n");
}
void game() //game函数来实现游戏
{
int ret = 0;
char board[ROW][COL] = { 0 }; //存放数据
InitBoard(board, ROW, COL); //初始化棋盘
DisPlayBoard(board, ROW, COL); //打印棋盘
while (1)
{
Player_move(board, ROW, COL); //玩家下棋
DisPlayBoard(board, ROW, COL); //打印棋盘
ret = is_win(board, ROW, COL); //判断输赢
if (ret != 'L') //游戏继续
{
break;
}
Computer_move(board, ROW, COL); //电脑下棋
DisPlayBoard(board, ROW, COL); //打印棋盘
ret = is_win(board, ROW, COL); //判断输赢
if (ret != 'L') //游戏继续
{
break;
}
}
if (ret == '*') //如果is_win函数返回*号则玩家赢了
{
printf("恭喜你获胜了!\n");
}
else if (ret == '#') //如果is_win函数返回#号则电脑赢了
{
printf("失败了电脑获胜!\n");
}
else //否则平局
printf("游戏平局\n");
DisPlayBoard(board, ROW, COL); //不管玩家还是电脑获胜最后打印一次棋盘可以看到如何获胜的
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL)); //引用time函数来返回随机值给srand 类型强制转换为int
do
{
menu(); //调用菜单
printf("请输入数字选择-->:");
scanf("%d", &input);
switch (input)
{
case 1:
game(); //进入游戏
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}3.game.c源文件用来实现三子棋小游戏.
#include<stdio.h>
#include"Li.h" //引用头文件中自己定义的行和列 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] = ' ';
}
}
}
void DisPlayBoard(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++)
{
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 Player_move(char board[ROW][COL], int row, int col) //玩家下棋
{
int x = 0;
int y = 0;
printf("玩家下棋\n");
while (1)
{
printf("请输入坐标-->:");
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] = '*'; //未占用则打印*号为玩家棋子
break;
}
else
{
printf("该坐标被占用,请重新输入坐标\n"); //占用了则重新输入坐标
}
}
else
printf("坐标错误,请重新输入\n");
}
}
void Computer_move(char board[ROW][COL], int row, int col) //电脑下棋
{
int x = 0;
int y = 0;
printf("电脑下棋\n");
while (1)
{
x = rand() % row; //用srand接收的随机值%行获得电脑下棋的行坐标
y = rand() % col; //用srand接收的随机值%列获得电脑下棋的列坐标
if (board[x][y] == ' ') //判断是否是未占用的空格
{
board[x][y] = '#'; //未占用输出#号为电脑棋子
break;
}
}
}
char is_win(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];
}
//判断列
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];
}
//判断对角线
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][i] != ' ')
{
return board[1][1];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][i] != ' ')
{
return board[1][1];
}
//判断平局
if (if_full(board, row, col) == 1)
{
return 'Q';
}
return 'L'; //代表还未有人赢或者平局 游戏继续
}
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; //还有空格证明还没平局 返回0
}
}
return 1; //否则返回1
}
边栏推荐
- Systemmediasize startup option added in esxi 7.0 update 1C
- What's a good gift for your girlfriend on the Chinese Valentine's day in 2022? Practical and beautiful gift recommendation
- Icc2 use report_ Placement check floorplan
- MFC 使用控制台打印程序信息
- What about the insufficient memory of Clickhouse aggregation? Then improve the polymerization performance
- JSON笔记
- I heard that you are also practicing when I interviewed several junior interns.
- 新的selenium
- 2022-07-19 达梦数据库-实例创建和管理
- C语言的文件操作
猜你喜欢

做气传导耳机最好的是哪家、最好的气传导耳机盘点

MATLAB signal processing

MFC uses the console to print program information

QT custom sliding button (beautiful and easy to use)

Use and safe shutdown of qthread thread in QT

气传导耳机哪个好、性价比最高的气传导耳机推荐

使用wampserver3.2.6时切换中文时造成启动失败

Word自动目录字体修改和行间距的问题

七夕送什么礼物最实用?送人绝对不会出错的礼物值得买

Matlab simulation of radar imaging 4 - range resolution analysis
随机推荐
qt设置加载界面的几种方法
Problems of font modification and line spacing in word automatic directory
如何模拟实现strcpy库函数
2022-06-07 responsebodyadvice caused the spring frame problem in swagger
2022-06-07 六.日志实现
NPM yarn related operations
QT solves the problem of rebuilding UI files every time they are modified
Monitor the CPU temperature of raspberry pie 4B installed with esxi on ARM
Ship detection in SAR image based on yolov5
pyppeteer 下拉 selenium下拉
相对路径和绝对路径
MySQL安装与使用
Matlab simulation of radar imaging 4 - range resolution analysis
qt解决每次修改完ui文件都要重新构建的问题
QT batch operation control and set signal slot
scrapy 定时执行
Detailed explanation of word mail merge function: after merging, multiple word documents are generated and blank pages are deleted
自定义组件--样式
2021-11-10
[server usage record] log in to the remote server through the springboard machine and transfer files