当前位置:网站首页>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、实现结果
边栏推荐
- fopen、_ Wfopen reads Unicode encoded files
- The outsourcing company "mixed" for two years, and I only did five things seriously. Now I get byte offer smoothly.
- 密码安全如何保障?安全浏览器如何管理密码?
- Pointer - golden stage
- The financing demand of 129 million yuan was released, and the roadshow of the Dake city project continued to irrigate the "good seedlings" of scientific innovation
- Experiment 2: Arduino's tricolor lamp experiment
- 高效使用浏览器的5个小技巧,第1个技巧最实用
- C language improvement (I)
- Data query of MySQL (multi table query)
- 响应式织梦模板化妆美妆类网站
猜你喜欢
第3章业务功能开发(线索备注的删除和修改)
Character flow comprehensive exercise problem solving process
响应式织梦模板家装建材类网站
响应式织梦模板化妆美妆类网站
The outsourcing company "mixed" for two years, and I only did five things seriously. Now I get byte offer smoothly.
2022/07/28 学习笔记 (day18) 常用API
千万不要把Request传递到异步线程里面,有坑
How to quickly design a set of cross end components that support rendering rich text content
FPGA刷题——存储器(RAM和FIFO的Verilog实现)
Object based real-time spatial audio rendering - Dev for dev column
随机推荐
Internet of things development -- mqtt message server emqx
代码随想录笔记_哈希_349两个数的交集
响应式织梦模板家装建材类网站
Jmeter之BeanShell生成MD5加密数据写入数据库
进程间通信---对管道的详细讲解(图文案例讲解)
Derivation of Euler angle differential equation
工程经济学名词解释
如果非要在多线程中使用 ArrayList 会发生什么?
[cloud native and 5g] micro services support 5g core network
How much is the report development cost in the application system?
7/28 Gauss elimination to solve linear equations + Gauss elimination to solve XOR linear equations + find the combination number II
裂开了,一次连接池参数导致的雪崩问题
npm install 报错 Error: EPERM: operation not permitted, rename
Day 15 (VLAN related knowledge)
一文理解分布式开发中的服务治理
一文搞懂 Redis 架构演化之路
7/28 高斯消元解线性方程组+高斯消元解异或线性方程组 +求组合数ii
What if there is not enough time for adequate testing?
千万不要把Request传递到异步线程里面,有坑
Interprocess communication - detailed explanation of the pipeline (explanation of graphic cases)