当前位置:网站首页>C语言实现三子棋游戏
C语言实现三子棋游戏
2022-07-29 01:59:00 【7昂7.】
三子棋实现
1、实现原理
采用二位数组保存棋盘信息,棋盘上面的任何一个位置,里头
可以放三个信息:
1、空
2、用户落子
3、电脑落子
下三子棋就是在二维数组种找对应的空位置,进行落子,落完之后立
即就要判断落子位置是否有三字连珠,从而判断谁数输谁赢。每
走一次棋会有四种情况:
*用户1赢
*用户2赢
*平局(棋盘被沾满)
**没有出结果 继续下棋
2、实现模块
文件名 作用
three_chress.h 三子棋的函数声明,头文件声明等
three_chress.c 三子棋函数接口的实现
main.c 三子棋函数测试功能
3、实现逻辑
一、我们要先在main.c这个文件,还是一样把框架构建好,再去分别实现他们的功能。实现菜单以及游戏的开始结束都在main函数里面,再从里面拿出游戏开始函数去具体实现游戏的进行。并把每次用到的头文件包含再three_chress.h里面。
main.c
在这里插入代码片#include"three_chress.h"
void menu()
{
printf("###### 1、开始游戏 #######\n");
printf("###### 2、退出 #######\n");
}
void game()
{
char ret = 0;
srand((unsigned int)time(NULL));
char a[ROW][COL] = {
0 };
init_a(a,ROW,COL);//初始化
show(a, ROW, COL);//可视化
while (1)
{
play_move(a, ROW, COL);//用户走
show(a, ROW, COL);
ret = Is_over(a,ROW,COL);//判断是否结束
if (ret != 'N')//N这个字符表示继续的意思
{
break;
}
computer_move(a, ROW, COL);//电脑走
show(a, ROW, COL);
ret = Is_over(a,ROW, COL);//判断是否结束
if (ret != 'N')//N这个字符表示继续的意思
{
break;
}
}
if (ret == '*')
{
printf("用户赢\n");
}
else if (ret =='#')
{
printf("电脑赢\n");
}
else if (ret ='D')//D表示为平局
{
printf("平局\n");
}
show(a, ROW, COL);
}
int main()
{
int input = 0;
do{
menu();
printf("请选择-> ");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 2:
printf("退出");
break;
default:
printf("不存在,请重新输入");
break;
}
} while (input);
return 0;
}
three_chress.h
在这里插入代码片#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);//初始化
void show(char a[][COL], int row , int col);//可视化
void play_move(char a[][COL],int row,int col);//用户下
void computer_move(char a[][COL], int row, int col);//电脑下
char Is_over(char a[][COL],int row,int col);//判断是否结束
int is_full(char a[][COL],int row, int col);
二、那么游戏怎么进行?我们要先实现这样的逻辑:构建二维数组并初始化——>显示棋盘信息即可视化——>让用户走并显示棋盘信息而且要立即判断用户是否赢,在判断时候我们用字符N来取表示游戏继续,如果返回的不是N说明游戏结束。——>如果没赢让电脑走并显示棋盘信息且判断电脑是否赢。最后我们用返回字符方式去判断谁赢或者平局。
三、把以上用到的逻辑用函数去一步一步实现,重点说几个函数:
1、在play_move函数里面需要注意的是 ,下棋所输入的坐标要先去判断其合法性以及多重性也就是重复。之后再赋棋子。
2、在computer_move实现比较简单,让电脑自动走、随机走 ,就要想到rand函数,而rand函数又和srand函数有,再用他们的性质让是X,Y随机生成0到2的值,去赋棋子即可。
3、而Is_over函数去判断输赢就是四种情况:用户赢;电脑赢;平局;继续。
直接比较四个方向上下两个对角线上的每个位置是否相等,且不为空。再写个判断是否满的函数去判断是否是平局,最后返回继续。这里面返回的都是字符。
如下代码:
three_chree.c
在这里插入代码片#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("用户下\n");
while (1)
{
printf("用户请输入:\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("已被占用,请重新输入");
}
}
else
{
printf("非法输入");
}
}
}
void computer_move(char a[][COL], int row, int col)
{
printf("电脑走\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)//判断是否结束
{
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';//表示游戏继续
}
4、实现结果

边栏推荐
- 如何利用 RPA 实现自动化获客?
- Meeting notice of meeting OA
- Website Collection
- I was stunned by this question that I browsed 746000 times
- NPM install reports an error: eperm: operation not permitted, rename
- Excel 用countif 统计
- Cookies and sessions
- 外包公司“混”了2年,我只认真做了5件事,如今顺利拿到字节 Offer。
- ES6 event binding (v-on usage)
- Internet of things development -- mqtt message server emqx
猜你喜欢

Never pass a request to an asynchronous thread. There is a hole

2022/07/28 学习笔记 (day18) 常用API

如果非要在多线程中使用 ArrayList 会发生什么?

Chapter 3 business function development (deletion and modification of clue remarks)

6年测试经验,教大家测试~如何把控项目

实验二:Arduino的三色灯实验
[email protected] The localization rate reaches 100%"/>Quanzhi t3/a40i industrial core board, 4-core [email protected] The localization rate reaches 100%

Responsive Zhimeng template decoration design website

Servlet三种实现方式

How to customize a new tab in Duoyu security browser?
随机推荐
When synchronized encounters this thing, there is a big hole, so be careful
数据安全与隐私计算峰会-安全求交集在隐私计算中的发展和应用:学习
Keil5 open the engineering prompt not found device solution
代码实现 —— 多项式的最大公因式(线性代数)
7/28 Gauss elimination to solve linear equations + Gauss elimination to solve XOR linear equations + find the combination number II
如何快速设计一套支持渲染富文本内容的跨端组件
Prevent repeated clicks
开启TLS加密的Proftpd安全FTP服务器安装指南
How to quickly design a set of cross end components that support rendering rich text content
4年测试经验,好不容易进了阿里,两个月后我选择了裸辞...
Derivation of Euler angle differential equation
聊聊接口性能优化的11个小技巧
Servlet三种实现方式
ES6详解 快速上手!
Split, an avalanche caused by connection pool parameters
Excel 用countif 统计
ES6事件绑定(v-on用法)
3d智能工厂工艺流转可视化交互展示应用优点
Installation guide for proftpd Secure FTP server with TLS encryption enabled
Experiment 2: Arduino's tricolor lamp experiment