当前位置:网站首页>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
边栏推荐
- 7-15 calculation of PI
- puzzle(016.3)千丝万缕
- Luogu p3065 [usaco12dec]first! G problem solution
- Bibit pharmaceutical rushed to the scientific innovation board: annual revenue of 970000, loss of 137million, proposed to raise 2billion
- ZABBIX saves the page blank after adding calculated items
- String sort
- Zzuli:1057 prime number determination
- Special research report on the market of lithium battery electrolyte industry in China (2022 Edition)
- Frequently asked questions: PHP LDAP_ add(): Add: Undefined attribute type in
- Jiuyi cloud black free encryption free version source code
猜你喜欢
[qingniaochangping campus of Peking University] in the Internet industry, which positions are more popular as they get older?
Use of constraintlayout
亚马逊、速卖通、Lazada、Shopee、eBay、wish、沃尔玛、阿里国际、美客多等跨境电商平台,测评自养号该如何利用产品上新期抓住流量?
Zzuli:1053 sine function
ConstraintLayout 的使用
使用并行可微模拟加速策略学习
Niuke: crossing the river
基因家族特征分析 - 染色体定位分析
npm install卡住与node-npy的各种奇怪报错
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
随机推荐
FPGA blocking assignment and non blocking assignment
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
Zzuli:1049 square sum and cubic sum
Raft agreement
Optical cat super account password and broadband account password acquisition
Output student grades
556. The next larger element III
2021年区域赛ICPC沈阳站J-Luggage Lock(代码简洁)
洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解
tonybot 人形机器人 红外遥控玩法 0630
Zzuli:1054 monkeys eat peaches
Zhonggan micro sprint technology innovation board: annual revenue of 240million, net loss of 17.82 million, proposed to raise 600million
Add ZABBIX calculation type itemcalculated items
Too many files with unapproved license
Zzuli:1040 sum of sequence 1
7-3 rental (20 points)
7-10 stack of hats (25 points) (C language solution)
The mail function of LNMP environment cannot send mail
Plane vector addition
Creation of data table of Doris' learning notes