自己写的一个五子棋程序,欢迎大家拍砖,自己感觉写的也很烂。。。
#include <stdio.h>#include <stdlib.h>
#include <string.h>
char a[5][5]={NULL};//棋盘的大小
int row=0;//数组所代表的行
int column=0;//数组所代表的列
int cnt1=0;//玩家1落子个数计数器
int cnt2=0;//玩家2落子个数计数器
int black_num=0;//如果值为5,说明玩家1获胜
int black_row_num0=0;//玩家1第一行落子个数计数器
int black_row_num1=0;//玩家1第二行落子个数计数器
int black_row_num2=0;//玩家1第三行落子个数计数器
int black_row_num3=0;//玩家1第四行落子个数计数器
int black_row_num4=0;//玩家1第五行落子个数计数器
int black_column_num0=0;//玩家1第一列落子个数计数器
int black_column_num1=0;//玩家1第二列落子个数计数器
int black_column_num2=0;//玩家1第三列落子个数计数器
int black_column_num3=0;//玩家1第四列落子个数计数器
int black_column_num4=0;//玩家1第五列落子个数计数器
int black_slash_num0=0;//玩家1正斜对角线落子个数计数器
int black_slash_num1=0;//玩家1反斜对角线落子个数计数器
int white_num=0;//如果值为5,说明玩家2获胜
int white_row_num0=0;//玩家2第一行落子个数计数器
int white_row_num1=0;//玩家2第二行落子个数计数器
int white_row_num2=0;//玩家2第三行落子个数计数器
int white_row_num3=0;//玩家2第四行落子个数计数器
int white_row_num4=0;//玩家2第五行落子个数计数器
int white_column_num0=0;//玩家2第一列落子个数计数器
int white_column_num1=0;//玩家2第二列落子个数计数器
int white_column_num2=0;//玩家2第三列落子个数计数器
int white_column_num3=0;//玩家2第四列落子个数计数器
int white_column_num4=0;//玩家2第五列落子个数计数器
int white_slash_num0=0;//玩家2正斜对角线落子个数计数器
int white_slash_num1=0;//玩家2正斜对角线落子个数计数器
void Five_Chess();
void player_first();
void player_second();
void compare_black_white_num(int *row,int *column,char*player);
int main()
{
int number=0;
int cnt3=0;//统计二维数组是否每一个格子都下了子
int cnt4=0;//0代表重新开局,1代表退出
int time_dalay_num;//延时个数
int count0=5;//密码错误次数计数器
char str0[10]={NULL};//用于密码的比较
char str1[10]={NULL};//用于密码的比较
system("color 30");
/*
system("color 背景色/字体色")
0 = 黑色 8 = 灰色
1 = 蓝色 9 = 淡蓝色
2 = 绿色 A = 淡绿色
3 = 浅绿 B = 淡浅绿色
4 = 红色 C = 淡红色
5 = 紫色 D = 淡紫色
6 = 黄色 E = 淡黄色
7 = 白色 F = 亮白色
*/
printf("这是周正的五子棋软件!\n");
printf("请输入长度不超过10的密码!\n");
scanf("%s",&str0);
while(1)
{
printf("请再次输入密码!\n");
scanf("%s",&str1);
if (strcmp(str0,str1)==0)
{
printf("密码正确!\n");
printf("正在进入五子棋软件系统,请您耐心等候");
break;
}
else
{
count0--;
if(count0<1)
{
printf("5次密码均输入错误,今日已不能五子棋软件系统,请明日再进行登录!\n");
exit(0);
}
else printf("密码输入错误,你还有%d次机会!\n",count0);
}
}
for (time_dalay_num=0;time_dalay_num<5;time_dalay_num++)
{
_sleep(1000);
printf(".");
}
printf("\n\n");
LABEL0:
printf("输入数字1,显示五子棋棋盘图形!\n");
printf("输入数字2,选手1进行落子!\n");
printf("输入数字3,选手2进行落子!\n");
printf("输入数字4,退出五子棋软件系统!\n");
scanf("%d",&number);
switch(number)
{
case 1:
{
system("cls");
Five_Chess();
goto LABEL0;
}
LABEL3:
case 2:
{
player_first();
system("cls");
Five_Chess();
goto LABEL1;
}
LABEL4:
case 3:
{
player_second();
system("cls");
Five_Chess();
goto LABEL2;
}
case 4:
{
printf("感谢您使用我的五子棋软件系统!\n");
exit(0);
}
default:
{
printf("你输入的数字有误,请重新输入!\n");
goto LABEL0;
}
}
LABEL1:
if (cnt1)
{
if (black_num==5)
{
//当玩家1赢了,将棋手1的落子个数清0,将棋手2的落子个数清0,
//整个棋盘的落子个数清0,另外将之前玩家1和玩家2的行、列、斜计数器也清0
cnt1=0;
cnt2=0;
cnt3=0;
black_num=0;
black_row_num0=0;
black_row_num1=0;
black_row_num2=0;
black_row_num3=0;
black_row_num4=0;
black_column_num0=0;
black_column_num1=0;
black_column_num2=0;
black_column_num3=0;
black_column_num4=0;
black_slash_num0=0;
black_slash_num1=0;
white_num=0;
white_row_num0=0;
white_row_num1=0;
white_row_num2=0;
white_row_num3=0;
white_row_num4=0;
white_column_num0=0;
white_column_num1=0;
white_column_num2=0;
white_column_num3=0;
white_column_num4=0;
white_slash_num0=0;
white_slash_num1=0;
printf("玩家1获胜!\n");
printf("如果想再来一局,请输入1,如果不想玩了,请输入0!\n");
scanf("%d",&cnt4);
if (cnt4==1)
{
for (row=0;row<5;row++)
{
for (column=0;column<5;column++)
{
a[row][column]=NULL;
}
}
goto LABEL0;
}
else exit(0);
}
else
{
if ((a[row][column]=='X')|(a[row][column]=='O'))
{
++cnt3;
if (cnt3==25)
{
//当棋盘布满时,双方都未获胜,将棋手1的落子个数清0,将棋手2的落子个数清0,
//整个棋盘的落子个数清0,另外将之前玩家1和玩家2的行、列、斜计数器也清0
cnt1=0;
cnt2=0;
cnt3=0;
black_num=0;
black_row_num0=0;
black_row_num1=0;
black_row_num2=0;
black_row_num3=0;
black_row_num4=0;
black_column_num0=0;
black_column_num1=0;
black_column_num2=0;
black_column_num3=0;
black_column_num4=0;
black_slash_num0=0;
black_slash_num1=0;
white_num=0;
white_row_num0=0;
white_row_num1=0;
white_row_num2=0;
white_row_num3=0;
white_row_num4=0;
white_column_num0=0;
white_column_num1=0;
white_column_num2=0;
white_column_num3=0;
white_column_num4=0;
white_slash_num0=0;
white_slash_num1=0;
printf("双方都未获胜,结果为和棋!\n");
printf("如果想再来一局,请输入1,如果不想玩了,请输入0!\n");
scanf("%d",&cnt4);
if (cnt4==1)
{
for (row=0;row<5;row++)
{
for (column=0;column<5;column++)
{
a[row][column]=NULL;
}
}
goto LABEL0;
}
else exit(0);
}
else goto LABEL4;
}
}
}
LABEL2:
if (cnt2)
{
if (white_num==5)
{
//当玩家2赢了,将棋手1的落子个数清0,将棋手2的落子个数清0,
//整个棋盘的落子个数清0,另外将之前玩家1和玩家2的行、列、斜计数器也清0
cnt1=0;
cnt2=0;
cnt3=0;
black_num=0;
black_row_num0=0;
black_row_num1=0;
black_row_num2=0;
black_row_num3=0;
black_row_num4=0;
black_column_num0=0;
black_column_num1=0;
black_column_num2=0;
black_column_num3=0;
black_column_num4=0;
black_slash_num0=0;
black_slash_num1=0;
white_num=0;
white_row_num0=0;
white_row_num1=0;
white_row_num2=0;
white_row_num3=0;
white_row_num4=0;
white_column_num0=0;
white_column_num1=0;
white_column_num2=0;
white_column_num3=0;
white_column_num4=0;
white_slash_num0=0;
white_slash_num1=0;
printf("玩家2获胜!\n");
printf("如果想再来一局,请输入1,如果不想玩了,请输入0!\n");
scanf("%d",&cnt4);
if (cnt4==1)
{
for (row=0;row<5;row++)
{
for (column=0;column<5;column++)
{
a[row][column]=NULL;
}
}
goto LABEL0;
}
else exit(0);
}
else
{
if ((a[row][column]=='X')|(a[row][column]=='O'))
{
++cnt3;
if (cnt3==25)
{
//当棋盘布满时,将棋手1的落子个数清0,将棋手2的落子个数清0,
//整个棋盘的落子个数清0,另外将之前玩家1和玩家2的行、列、斜计数器也清0
cnt1=0;
cnt2=0;
cnt3=0;
black_num=0;
black_row_num0=0;
black_row_num1=0;
black_row_num2=0;
black_row_num3=0;
black_row_num4=0;
black_column_num0=0;
black_column_num1=0;
black_column_num2=0;
black_column_num3=0;
black_column_num4=0;
black_slash_num0=0;
black_slash_num1=0;
white_num=0;
white_row_num0=0;
white_row_num1=0;
white_row_num2=0;
white_row_num3=0;
white_row_num4=0;
white_column_num0=0;
white_column_num1=0;
white_column_num2=0;
white_column_num3=0;
white_column_num4=0;
white_slash_num0=0;
white_slash_num1=0;
printf("双方都未获胜,结果为和棋!\n");
printf("如果想再来一局,请输入1,如果不想玩了,请输入0!\n");
scanf("%d",&cnt4);
if (cnt4==1)
{
for (row=0;row<5;row++)
{
for (column=0;column<5;column++)
{
a[row][column]=NULL;
}
}
goto LABEL0;
}
else exit(0);
}
else goto LABEL3;
}
}
}
return 0;
}
void Five_Chess()
{
printf("*********^@欢迎使用我的五子棋软件^@*********^@\n");
printf("\t ━━━━━━━━\n");
printf("\t ┃%c┃%c┃%c┃%c┃%c┃\n",a[0][0],a[0][1],a[0][2],a[0][3],a[0]
[4]);
printf("\t ━━━━━━━━\n");
printf("\t ┃%c┃%c┃%c┃%c┃%c┃\n",a[1][0],a[1][1],a[1][2],a[1][3],a[1]
[4]);
printf("\t ━━━━━━━━\n");
printf("\t ┃%c┃%c┃%c┃%c┃%c┃\n",a[2][0],a[2][1],a[2][2],a[2][3],a[2]
[4]);
printf("\t ━━━━━━━━\n");
printf("\t ┃%c┃%c┃%c┃%c┃%c┃\n",a[3][0],a[3][1],a[3][2],a[3][3],a[3]
[4]);
printf("\t ━━━━━━━━\n");
printf("\t ┃%c┃%c┃%c┃%c┃%c┃\n",a[4][0],a[4][1],a[4][2],a[4][3],a[4]
[4]);
printf("\t ━━━━━━━━\n");
printf("*********^@欢迎使用我的五子棋软件^@*********^@\n\n");
return ;
}
void player_first()
{
char p1=NULL;//玩家1落子'X'
char player1_huiqi_num=NULL;//Y代表悔棋,N代表不悔棋
char player2_agree_num=NULL;//Y代表玩家2同意悔棋,N代表不同意
printf("现在请第一名选手落子!\n");
while(1)
{
while(1)
{
while (1)
{
while (1)
{
printf("请输入你想落子的位置!\n");
scanf("%d %d",&row,&column);
getchar();
if ((row>=5)||(column>=5))
{
printf("玩家1输入的坐标超出了棋盘的范围,请重新输入!\n");
}
else break;
}
while (1)
{
printf("输入X进行落子!\n");
scanf("%c",&p1);
getchar();
if (p1!='X')
{
printf("您输入的字符不正确,请重新输入!\n");
}
else break;
}
if (a[row][column]=='O')
{
printf("这个地方玩家2已经下了棋子,您不能再在这个地方落子,请重新输入你想落子的位置!\n");
}
else break;
}
if (a[row][column]=='X')
{
printf("这个地方您已经下了棋子,您不能重复在这个地方落子,请重新输入你想落子的位置!\n");
}
else break;
}
if (p1!='\0')
{
a[row][column]=p1;
++cnt1;
}
Five_Chess();
printf("如果你想悔棋请输入字母Y,不悔棋则输入N!\n");
scanf("%c",&player1_huiqi_num);
getchar();
printf("如果玩家2同意悔棋则输入Y;不同意则输入N!\n");
scanf("%c",&player2_agree_num);
getchar();
if (player1_huiqi_num=='Y')
{
if (player2_agree_num=='Y')
{
a[row][column]=NULL;
}
else break;
}
else break;
}
compare_black_white_num(&row,&column,&p1);
return ;
}
void player_second()
{
char p2=NULL;//玩家2落子'O'
char player2_huiqi_num=NULL;//Y代表悔棋,N代表不悔棋
char player1_agree_num=NULL;//Y代表玩家1同意悔棋,N代表不同意
printf("现在请第二名选手落子!\n");
while(1)
{
while(1)
{
while (1)
{
while (1)
{
printf("请输入你想落子的位置!\n");
scanf("%d %d",&row,&column);
getchar();
if ((row>=5)||(column>=5))
{
printf("玩家2输入的坐标超出了棋盘的范围,请重新输入!\n");
}
else break;
}
while (1)
{
printf("输入O进行落子!\n");
scanf("%c",&p2);
getchar();
if (p2!='O')
{
printf("您输入的字符不正确,请重新输入!\n");
}
else break;
}
if (a[row][column]=='X')
{
printf("这个地方玩家1已经下了棋子,您不能再在这个地方落子,请重新输入你想落子的位置!\n");
}
else break;
}
if (a[row][column]=='O')
{
printf("这个地方您已经下了棋子,您不能重复在这个地方落子,请重新输入你想落子的位置!\n");
}
else break;
}
if (p2!='\0')
{
a[row][column]=p2;
++cnt2;
}
Five_Chess();
printf("如果你想悔棋请输入字母Y,不悔棋则输入N!\n");
scanf("%c",&player2_huiqi_num);
getchar();
printf("如果玩家1同意悔棋则输入Y;不同意则输入N!\n");
scanf("%c",&player1_agree_num);
getchar();
if (player2_huiqi_num=='Y')
{
if (player1_agree_num=='Y')
{
a[row][column]=NULL;
}
else break;
}
else break;
}
compare_black_white_num(&row,&column,&p2);
return ;
}
void compare_black_white_num(int*row,int*column,char*player)
{
if ((*row==0)&&(*column==0))
{
{
if (*player=='X')
{
++black_row_num0;
++black_column_num0;
++black_slash_num0;
}
else if (*player=='O')
{
++white_row_num0;
++white_column_num0;
++white_slash_num0;
}
}
}
else if ((*row==0)&&(*column==1))
{
{
if (*player=='X')
{
++black_row_num0;
++black_column_num1;
}
else if (*player=='O')
{
++white_row_num0;
++white_column_num1;
}
}
}
else if ((*row==0)&&(*column==2))
{
{
if (*player=='X')
{
++black_row_num0;
++black_column_num2;
}
else if (*player=='O')
{
++white_row_num0;
++white_column_num2;
}
}
}
else if ((*row==0)&&(*column==3))
{
{
if (*player=='X')
{
++black_row_num0;
++black_column_num3;
}
else if (*player=='O')
{
++white_row_num0;
++white_column_num3;
}
}
}
else if ((*row==0)&&(*column==4))
{
{
if (*player=='X')
{
++black_row_num0;
++black_column_num4;
++black_slash_num1;
}
else if (*player=='O')
{
++white_row_num0;
++white_column_num4;
++white_slash_num1;
}
}
}
else if ((*row==1)&&(*column==0))
{
{
if (*player=='X')
{
++black_row_num1;
++black_column_num0;
}
else if (*player=='O')
{
++white_row_num1;
++white_column_num0;
}
}
}
else if ((*row==1)&&(*column==1))
{
{
if (*player=='X')
{
++black_row_num1;
++black_column_num1;
++black_slash_num0;
}
else if (*player=='O')
{
++white_row_num1;
++white_column_num1;
++white_slash_num0;
}
}
}
else if ((*row==1)&&(*column==2))
{
{
if (*player=='X')
{
++black_row_num1;
++black_column_num2;
}
else if (*player=='O')
{
++white_row_num1;
++white_column_num2;
}
}
}
else if ((*row==1)&&(*column==3))
{
{
if (*player=='X')
{
++black_row_num1;
++black_column_num3;
++black_slash_num1;
}
else if (*player=='O')
{
++white_row_num1;
++white_column_num3;
++white_slash_num1;
}
}
}
else if ((*row==1)&&(*column==4))
{
{
if (*player=='X')
{
++black_row_num1;
++black_column_num4;
}
else if (*player=='O')
{
++white_row_num1;
++white_column_num4;
}
}
}
else if ((*row==2)&&(*column==0))
{
{
if (*player=='X')
{
++black_row_num2;
++black_column_num0;
}
else if (*player=='O')
{
++white_row_num2;
++white_column_num0;
}
}
}
else if ((*row==2)&&(*column==1))
{
{
if (*player=='X')
{
++black_row_num2;
++black_column_num1;
}
else if (*player=='O')
{
++white_row_num2;
++white_column_num1;
}
}
}
else if ((*row==2)&&(*column==2))
{
{
if (*player=='X')
{
++black_row_num2;
++black_column_num2;
++black_slash_num0;
++black_slash_num1;
}
else if (*player=='O')
{
++white_row_num2;
++white_column_num2;
++white_slash_num0;
++white_slash_num1;
}
}
}
else if ((*row==2)&&(*column==3))
{
{
if (*player=='X')
{
++black_row_num2;
++black_column_num3;
}
else if (*player=='O')
{
++white_row_num2;
++white_column_num3;
}
}
}
else if ((*row==2)&&(*column==4))
{
{
if (*player=='X')
{
++black_row_num2;
++black_column_num4;
}
else if (*player=='O')
{
++white_row_num2;
++white_column_num4;
}
}
}
else if ((*row==3)&&(*column==0))
{
{
if (*player=='X')
{
++black_row_num3;
++black_column_num0;
}
else if (*player=='O')
{
++white_row_num3;
++white_column_num0;
}
}
}
else if ((*row==3)&&(*column==1))
{
{
if (*player=='X')
{
++black_row_num3;
++black_column_num1;
++black_slash_num1;
}
else if (*player=='O')
{
++white_row_num3;
++white_column_num1;
++white_slash_num1;
}
}
}
else if ((*row==3)&&(*column==2))
{
{
if (*player=='X')
{
++black_row_num3;
++black_column_num2;
}
else if (*player=='O')
{
++white_row_num3;
++white_column_num2;
}
}
}
else if ((*row==3)&&(*column==3))
{
{
if (*player=='X')
{
++black_row_num3;
++black_column_num3;
++black_slash_num0;
}
else if (*player=='O')
{
++white_row_num3;
++white_column_num3;
++white_slash_num0;
}
}
}
else if ((*row==3)&&(*column==4))
{
{
if (*player=='X')
{
++black_row_num3;
++black_column_num4;
}
else if (*player=='O')
{
++white_row_num3;
++white_column_num4;
}
}
}
else if ((*row==4)&&(*column==0))
{
{
if (*player=='X')
{
++black_row_num4;
++black_column_num0;
++black_slash_num1;
}
else if (*player=='O')
{
++white_row_num4;
++white_column_num0;
++white_slash_num1;
}
}
}
else if ((*row==4)&&(*column==1))
{
{
if (*player=='X')
{
++black_row_num4;
++black_column_num1;
}
else if (*player=='O')
{
++white_row_num4;
++white_column_num1;
}
}
}
else if ((*row==4)&&(*column==2))
{
{
if (*player=='X')
{
++black_row_num4;
++black_column_num2;
}
else if (*player=='O')
{
++white_row_num4;
++white_column_num2;
}
}
}
else if ((*row==4)&&(*column==3))
{
{
if (*player=='X')
{
++black_row_num4;
++black_column_num3;
}
else if (*player=='O')
{
++white_row_num4;
++white_column_num3;
}
}
}
else if ((*row==4)&&(*column==4))
{
{
if (*player=='X')
{
++black_row_num4;
++black_column_num4;
++black_slash_num0;
}
else if (*player=='O')
{
++white_row_num4;
++white_column_num4;
++white_slash_num0;
}
}
}
if ((black_row_num0==5)|(black_row_num1==5)|(black_row_num2==5)|(black_row_num3==5)|(black_row_num4==5)|
(black_column_num0==5)|(black_column_num1==5)|(black_column_num2==5)|(black_column_num3==5)|
(black_column_num4==5)|(black_slash_num0==5)|(black_slash_num1==5))
{
black_num=5;
}
else if ((white_row_num0==5)|(white_row_num1==5)|(white_row_num2==5)|(white_row_num3==5)|(white_row_num4==5)|
(white_column_num0==5)|(white_column_num1==5)|(white_column_num2==5)|(white_column_num3==5)|
(white_column_num4==5)|(white_slash_num0==5)|(white_slash_num1==5))
{
white_num=5;
}
return ;
}