当前位置:网站首页>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;
}
边栏推荐
- Applet custom components - data, methods, and properties
- easypoi导出表格带echars图表
- Static and floating routes
- Small turtle C (Chapter 5 loop control structure program 567) break and continue statements
- Implementation method of converting ast into word vector before converting word vector
- shell---函数
- As a result, fill in the birthday candles
- Applets: WSX scripts
- shell---循环语句练习
- 232(母)转422(公)
猜你喜欢

MOOC Weng Kai C language week 6: arrays and functions: 1. Arrays 2. Definition and use of functions 3. Parameters and variables of functions 4. Two dimensional arrays

Custom components -- styles

三层交换和VRRP

Monotonic queue, Luogu p1886 sliding window

Repair the faulty sector

根据excel生成create建表SQL语句

一、PXE概述和安装

Sysevr environment configuration: joern-0.3.1, neo4j-2.1.5, py2neo2.0

MOOC翁恺 C语言 第三周:判断与循环:2.循环

NAT-网络地址转换
随机推荐
[learning notes] tool
Esxi community network card driver updated in March 2022
Animation animation realizes the crossing (click) pause
Pictures are adaptive to the screen
Monotonic queue, Luogu p1886 sliding window
metasploit渗透ms7_010练习
bond模式配置
VNC Timed out waiting for a response from the computer
YUM仓库的搭建
[learning notes] VIM editor
JS string method Encyclopedia
RAID磁盘阵列
[learning records of erudite Valley] Super summary, attentive sharing | collection
MOOC翁恺C语言第八周:指针与字符串:1.指针2.字符类型3.字符串4.字符串计算
Esxi community network card driver updated again
静态和浮动路由
分解路径为目录名和文件名的方法
shell脚本——编程条件语句(条件测试、if语句、case分支语句、echo用法、for循环、while循环)
Shell script -- program conditional statements (conditional tests, if statements, case branch statements, echo usage, for loops, while loops)
Operation document tree