当前位置:网站首页>Gobang optimized version
Gobang optimized version
2022-07-28 07:07:00 【c7473168】
Small projects : gobang
Data required :
1, Define the chessboard of the character array 15*15
2, Define variables to record the position of the drop
3, Define a variable to record the chess sub role Black chess '@' White chess '$'
Business logic :
Define whether the data needs to be initialized
for(;;)
{
1, Clean the screen , Show chessboard characters
2, Move later
Coordinates should be legal , There should be no chessmen in this position, otherwise continue to relocate
3, Check whether the five sons are linked
yes , End the game
4, Exchange roles
#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(); // Initialize chessboard
for(;;)
{
t_board(); // Show chessboard
p_chess(); // Move later
if(color_b())
{
t_board();
printf("%c Win. ",role);
return 0;
}
role = 'O'==role ? 'X':'O'; // Exchange roles
}
}
// Clean the screen Initialize chessboard
void c_board(void)
{
for(int i=0;i<15;i++)
{
for(int j=0;j<15;j++)
{
arr[i][j]='*';
}
}
}
// Show chessboard
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");
}
}
// Move later
void p_chess(void)
{
printf("------------ please %c Move later ------------",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;
}
边栏推荐
- RAID磁盘阵列
- MOOC翁恺C语言 第四周:进一步的判断与循环:3.多路分支4.循环的例子5.判断和循环常见的错误
- Operation document tree
- MOOC Weng Kai C language week 3: judgment and circulation: 2. circulation
- Remotely access the local website of services such as neo4j on the ECS
- About gcc:multiple definition of
- Custom component -- pure data field & component life cycle
- Uniapp monitor whether the app has a network connection
- MOOC Weng Kai C language week 3: judgment and cycle: 1. Judgment
- Media set up live broadcast server
猜你喜欢

DHCP principle and configuration

Blue Bridge Cup square filling number

NAT network address translation

JSON notes

Remotely access the local website of services such as neo4j on the ECS

Esxi community nvme driver update v1.1

The.Joernindex database has no content after Joern runs

metasploit渗透ms7_010练习

Joern's code uses -devign

joern运行后.joernindex数据库无内容
随机推荐
JSON notes
Servlet
MOOC翁恺 C语言 第三周:判断与循环:2.循环
MOOC翁恺C语言第五周:1.循环控制2.多重循环3.循环应用
New, let, VaR, const and temporary deadband. The difference between XML and JSON
Monotonic queue, Luogu p1886 sliding window
Implementation method of converting ast into word vector before converting word vector
shell---函数
MOOC翁恺C语言第八周:指针与字符串:1.指针2.字符类型3.字符串4.字符串计算
JS string method Encyclopedia
Shell script -- program conditional statements (conditional tests, if statements, case branch statements, echo usage, for loops, while loops)
shell脚本——sort、uniq、tr、数组排序、cut、eval命令配置
Implementation method of Bert
DOM Foundation
Reptile learning summary
PXE unattended installation management
win下安装nessus
metasploit渗透ms7_010练习
Shell script - sort, uniq, TR, array sort, cut, Eval command configuration
Bond mode configuration