当前位置:网站首页>Implement Gobang with C language
Implement Gobang with C language
2022-07-03 14:36:00 【roseisbule】
Gobang is a very classic game , Today I try to use c Language to write , You can customize how many children are connected together as a victory , Customize the size of the chessboard , Insufficient AI Only random , There is no certain ability to analyze the chessboard .
First, clear your mind , I hope the program can print the chessboard first , Then let me play chess , Then judge the legitimacy of the chess position ( The irregularity prompts me to start again ), Then judge whether the player wins , Then judge the draw . Under the machine, random numbers are used , The other steps are the same .
Clear your mind and start writing code , First write a simple print menu ;
void menu()
{
printf("-----------------------------------\n");
printf("******* 1. Start the game *********\n");
printf("******* 0. Quit the game *********\n");
printf("-----------------------------------\n");
int input = 0;
char arr[HANG][LIE] = { 0 };
printf(" Please select :");
do
{
scanf("%d", &input);
switch (input)
{
case 1:
game(arr, HANG, LIE);
break;
case 0:
printf(" sign out ");
break;
default:
printf(" Wrong value entered , Please re-enter :");
}
} while (input);
}
Then write the game function
void game(char arr[HANG][LIE],int hang,int lie)
{
chushihua(arr, hang, lie);
display(arr, hang, lie);
while (1)
{
if (put_palyer(arr,hang,lie) == 1)
{
display(arr, hang, lie);
printf("\n Game player wins \n");
break;
}
if (is_full(arr, hang, lie))
{
display(arr, hang, lie);
printf("\n It ends in a draw \n");
break;
}
display(arr, hang, lie);
if (put_computer(arr,hang,lie) == 1)
{
display(arr, hang, lie);
printf(" Computers win ");
break;
}
if (is_full(arr, hang, lie))
{
display(arr, hang, lie);
printf("\n It ends in a draw \n");
break;
}
display(arr, hang, lie);
}
menu();// I hope players can see the complete chessboard at the end of the last time , And you can choose whether to play the next game
}
Of course, it's better to initialize the array
void chushihua(char arr[HANG][LIE], int hang, int lie)
{
int i = 0;
int j = 0;
for (i = 0; i < hang; i++)
{
for (j = 0; j < lie; j++)
arr[i][j] = ' ';
}
}
Then write the print function , Can print out the chessboard
void display(char arr[HANG][LIE], int hang, int lie)
{
system("cls");
int i = 0;
int j = 0;
for (i = 0; i < hang; i++)
printf(" %d ", i + 1);
printf("\n");
for (i = 0; i < hang; i++)
{
printf("%d", i + 1);
for (j = 0; j < lie-1; j++)
{
printf(" %c |", arr[i][j]);
}
printf(" %c\n ", arr[i][j]);
if (i < hang - 1)
{
for (j = 0; j < lie - 1; j++)
printf("---|");
printf("---\n");
}
}
}
Then write The function of players and computers playing chess
int put_palyer(char arr[HANG][LIE],int hang,int lie)
{
int i = 0;
int j = 0;
printf("(# Fang ) Please enter the coordinates :");
while (1)
{
scanf("%d %d", &i, &j);
if (i < 1 || i > hang || j < 1 || j > lie)
{
printf(" Illegal coordinates , Re input :");
continue;
}
else if (arr[i-1][j-1] != ' ')
{
printf(" The coordinates occupy , Re input :");
continue;
}
else
{
arr[i-1][j-1] = '#';
break;
}
}
if (panduan(arr, hang, lie, '#') == 1)
return 1;
return 0;
}
int put_computer(char arr[HANG][LIE], int hang, int lie)
{
while (1)
{
int i = rand() % hang;
srand((unsigned int)time(NULL));
int j = rand() % lie;
if (0 <= i && i < hang && 0 <= j && j < lie && arr[i][j] == ' ')
{
arr[i][j] = '@';
break;
}
}
if (panduan(arr, hang, lie, '@') == 1)
return 1;
return 0;
}
Finally, the most difficult thing is to realize the function of judging whether to win or lose
My idea is 4 A big judgment They are vertical level From top left to bottom right From bottom left to top right 4 A big judgment
Then create a global variable flag If in a certain line perhaps A column perhaps In a slash Find players or computer chess ,flag Just +1, But if not , It means that it is disconnected ,flag It will become 0, In this column , Or in this line , Or in this slash , Find out flag=5, Then it can be said that players or computers win . It is very important to pay attention to , Line break , Transposition , Change the slash again flag=0.
Know the judgment idea , But how to write the judgment process . Vertical and horizontal are easy , The difficulty is diagonal judgment . Here we introduce local variables k, Once again, move the top left to the bottom right , It is divided into two parts from the bottom left to the top right .
Let's look at the process from top left to bottom right . In two parts . Up arrow and down arrow .
From the lower arrow . I hope to use k To indicate the downward movement of the judgment slash , But because of Gobang , therefore k The maximum is hang-5, among hang Is the number of rows . Then it's like vertical and horizontal , Judge , But every line should be marked with k Is the starting position .
From the arrow above . I hope to use k To indicate the right shift of the judgment slash . But because of Gobang , therefore k The lowest is lie-5, among lie Is the number of columns , Then it's the same as the one above , It's just ,k The maximum is lie-5, The number of columns changes from k Start .
int panduan2(char x,char y)
{
if (x == y)
flag++;
else
flag = 0;
if (flag == 5)
return 1;
return 0;
}
int panduan(char arr[HANG][LIE], int hang, int lie, char a)
{
int flag = 0;
int i = 0;
int j = 0;
int k = 0;
for (i = 0; i < hang; i++)// Lateral judgment
{
flag = 0;
for (j = 0; j < lie; j++)
if (panduan2(arr[i][j], a) == 1)
return 1;
}
for (j = 0; j < hang; j++)// Vertical judgment
{
flag = 0;
for (i = 0; i < lie; i++)
if (panduan2(arr[i][j], a) == 1)
return 1;
}
for (k = 0; k < hang - 4; k++)
{
flag = 0;
for (i = k, j = 0; i < hang; i++, j++)
if (panduan2(arr[i][j], a) == 1)
return 1;
}
for (k = 1; k < lie - 4; k++)
{
flag = 0;
for (i = 0, j = k; j < lie; i++, j++)
if (panduan2(arr[i][j], a) == 1)
return 1;
}
for (k = hang - 1; k > 3; k--)
{
flag = 0;
for (i = k, j = 0; i >= 0; i--, j++)
if (panduan2(arr[i][j], a) == 1)
return 1;
}
for (k = 1; k < lie - 4; k++)
{
flag = 0;
for (i = hang - 1, j = k; j < lie; j++, i--)
if (panduan2(arr[i][j], a) == 1)
return 1;
}
return 0;
}
From the bottom left to the top right is the same method and idea .
Finally, we need to write a code to judge the draw .
int is_full(char arr[HANG][LIE], int hang, int lie)
{
int i = 0;
int j = 0;
for (i = 0; i < hang; i++)
{
for (j = 0; j < lie; j++)
if (arr[i][j] == ' ')
return 0;
}
return 1;
}
Finally, the basic implementation logic of the whole program is completed .
gitee link :RoseIsBlue/learning_c_language - Gitee.com
11.8 Code for
边栏推荐
猜你喜欢
Code writing and playing method of tonybot humanoid robot at fixed distance
retrofit
How to query the baby category of tmall on Taobao
一文了解微分段应用场景与实现机制
Frequently asked questions: PHP LDAP_ add(): Add: Undefined attribute type in
Luogu p5018 [noip2018 popularization group] symmetric binary tree problem solution
表单文本框的使用(一) 选择文本
【北大青鸟昌平校区】互联网行业中,哪些岗位越老越吃香?
编程语言:类型系统的本质
Tiantu investment sprint Hong Kong stocks: asset management scale of 24.9 billion, invested in xiaohongshu and Naixue
随机推荐
Jiuyi cloud black free encryption free version source code
7-9 one way in, two ways out (25 points)
Add ZABBIX calculation type itemcalculated items
Sendmail无法发送邮件及发送过慢解决
Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
Selective sorting
【7.3】146. LRU缓存机制
7-22 tortoise and rabbit race (result oriented)
Address book sorting
Zzuli:1047 logarithmic table
天谋科技 Timecho 完成近亿元人民币天使轮融资,打造工业物联网原生时序数据库
Output student grades
7-3 rental (20 points)
Zzuli:1057 prime number determination
Adc128s022 ADC Verilog design and Implementation
How to query the baby category of tmall on Taobao
论文分享:Generating Playful Palettes from Images
LNMP环境mail函数不能发送邮件解决
Frequently asked questions: PHP LDAP_ add(): Add: Undefined attribute type in
Mysql多表查询 #子查询