当前位置:网站首页>五子棋优化版
五子棋优化版
2022-07-28 05:18:00 【c7473168】
小项目:五子棋
需要的数据:
1,定义字符数组的棋盘15*15
2,定义变量用于记录落子的位置
3,定义一个变量记录棋子角色 黑棋'@'白棋'$'
业务逻辑:
定义需要数据是否需要对数据进行初始化
for(;;)
{
1,清理屏幕,显示棋盘角色
2,落子
坐标要合法,该位置不能有棋子否则继续重新落子
3,检查是否五子连珠
是,结束游戏
4,交换角色
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <getch.h>
char arr[15][15];
char row=7,col=7;
char role='O';
void c_board(void);
void t_board(void);
void p_chess(void);
int count_key(int ox,int oy);
bool color_b(void);
int main(int argc,const char* argv[])
{
c_board(); //初始化棋盘
for(;;)
{
t_board(); //显示棋盘
p_chess(); //落子
if(color_b())
{
t_board();
printf("%c赢了",role);
return 0;
}
role = 'O'==role ? 'X':'O'; //交换角色
}
}
// 清理屏幕 初始化棋盘
void c_board(void)
{
for(int i=0;i<15;i++)
{
for(int j=0;j<15;j++)
{
arr[i][j]='*';
}
}
}
// 显示棋盘
void t_board(void)
{
system("clear");
for(int i=0;i<15;i++)
{
for(int j=0;j<15;j++)
{
printf(" %c",arr[i][j]);
}
printf("\n");
}
}
// 落子
void p_chess(void)
{
printf("------------请%c落子------------",role);
for(;;)
{
printf("\33[%d;%dH",row+1,(col+1)*2);
switch(getch())
{
case 183: case 119: row>0 && row--; break;
case 184: case 115: row<14 && row++; break;
case 186: case 97: col>0 && col--; break;
case 185: case 100: col<14 && col++; break;
case 10: case 32: if('*'==arr[row][col])
{
arr[row][col]=role;
return;
}
break;
}
}
}
int count_key(int ox,int oy)
{
int count = 0;
for(int x=row+ox,y=col+oy; x>=0 && x<=14 && y>=0 && y<=14;x+=ox,y+=oy)
{
if(arr[x][y] == role)
{
count++;
}
else
{
return count;
}
}
return count;
}
bool color_b(void)
{
if(count_key(-1,0)+count_key(1,0)>=4) return true;
if(count_key(0,-1)+count_key(0,1)>=4) return true;
if(count_key(-1,-1)+count_key(1,1)>=4) return true;
if(count_key(-1,1)+count_key(1,-1)>=4) return true;
return false;
}
边栏推荐
- Openjudge: patient queuing
- 环形链表问题
- URL form
- repackag failed: Unable to find main class
- Framework step by step easy-to-use process
- SSM project quick build project configuration file
- regular expression
- MySQL uses list as a parameter to query
- Openjudge: stone scissors cloth
- Review of metallurgical physical chemistry ---- gas solid reaction kinetics
猜你喜欢
随机推荐
MySQL adds sequence number to query results
Problems encountered when the registry service Eureka switches to nocas
关于swagger中的localDateTime
visio如何精确控制图形的大小和位置及角度
ByteBuffer. Position throws exception illegalargumentexception
Pytorch uses hook to get feature map
24小时内的时间段无交叉
pytorch安装----CPU版的
Mutual conversion between latex and word
repackag failed: Unable to find main class
Feignclient calls the get method and reports an error resultvo{result= unknown exception. Exception details: request method 'post' not supported
Merge two ordered arrays of order table OJ
JVM notes 3: class loading and bytecode Technology
(dark horse) MySQL beginner advanced notes (blogger lazy dog)
使用深度学习训练图像时,图像太大进行切块训练预测
Response<T>类
repackag failed: Unable to find main class
Openjudge: maximum span of string
softmax多分类 梯度推导
Invalid bound statement (not found): com.exam.mapper.UserMapper.findbyid









