五子棋C程序
#include <stdio.h>
#include <stdlib.h>
#include<windows.h>
#include<conio.h>
#define n 15
char *map[2 * n + 1][2 * n + 1];
int w = 1, x = 1, y = 1, z = 1;
int Cx, Cy;
int now;
int modle;
int I, J;
int e,f;
int p,q;
int i, j;
double k=0,max,num,t;
double distance1,distance2;
int count = 0;
int input;
void windows1()
{
printf("** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **\n");
printf("** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **\n");
printf("** **\n");
printf("** **\n");
printf("** **\n");
printf("** **\n");
printf("** **\n");
printf("** ★★★★欢迎来到趣味五子棋游戏★★★★ **\n");
printf("** **\n");
printf("** **\n");
printf("** **\n");
printf("** **\n");
printf("** **\n");
printf("** ★★★模式1 模式2★★★ **\n");
printf("** **\n");
printf("** **\n");
printf("** **\n");
printf("** **\n");
printf("** **\n");
printf("** ★★方向键 键盘输入★★ **\n");
printf("** **\n");
printf("** **\n");
printf("** **\n");
printf("** **\n");
printf("** 输入1选择方向键控制光标位置 **\n");
printf("** 输入2选择键盘输入控制光标位置 **\n");
printf("** 输入0退出程序 **\n");
printf("** **\n");
printf("** **\n");
printf("** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **\n");
printf("** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **\n");
}
char*getmap()
{
for (i = 0; i<2 * n + 1; i++)
{
for (j = 0; j < 2 * n + 1; j++)
{
if (i == 0)
{
if (j == 0)
map[i][j] = ((char*)"┌");
else if (j == 2 * n)
map[i][j] = ((char*)"┐");
else if (j % 2 == 0)
map[i][j] = ((char*)"┬");
else map[i][j] = ((char*)"─");
}
else if (i == 2 * n)
{
if (j == 0)
map[i][j] = ((char*)"└");
else if (j == 2 * n)
map[i][j] = ((char*)"┘");
else if (j % 2 == 0)
map[i][j] = ((char*)"┴");
else map[i][j] = ((char*)"─");
}
else if (i % 2 == 0)
{
if (j == 0)
map[i][j] = ((char*)"├");
else if (j == 2 * n)
map[i][j] = ((char*)"┤");
else if (j % 2 == 0)
map[i][j] = ((char*)"┼");
else map[i][j] = ((char*)"─");
}
else
{
if (j % 2 == 0)
map[i][j] = ((char*)"│");
else
map[i][j] = ((char*)" ");
}
}
}
return 0;
}
void printfmap()
{
for (i = 0; i < 2 * n + 1; i++)
{
for (j = 0; j < 2 * n + 1; j++)
{
printf("%s", map[i][j]);
}
printf("\n");
}
}
void initialize()
{
for (i = 0; i <= n; i++)
for (j = 0; j <= n; j++)
if (n % 2 == 0)
{
Cx = n;
Cy = n;
}
else
{
Cx = n + 1;
Cy = n + 1;
}
count = 0;
now = 1;
}
char* displaycursor()
{
for (i = 0; i < 2 * n + 1; i++)
{
for (j = 0; j < 2 * n + 1; j++)
{
if (i == Cx - 1 && j == Cy - 1) map[i][j] = ((char*)"┍");
else if (i == Cx + 1 && j == Cy - 1) map[i][j] = ((char*)"┕");
else if (i == Cx - 1 && j == Cy + 1) map[i][j] = ((char*)"┑");
else if (i == Cx + 1 && j == Cy + 1) map[i][j] = ((char*)"┙");
}
}
return 0;
}
char* clean1(int Cx, int Cy)
{
for (i = 0; i < 2 * n + 1; i++)
{
for (j = 0; j < 2 * n + 1; j++)
{
if (i == Cx - 1 && j == Cy + 3) map[i][j] = ((char*)" ");
if (i == Cx + 1 && j == Cy + 3) map[i][j] = ((char*)" ");
}
}
return 0;
}
char* clean2(int Cx, int Cy)
{
for (i = 0; i < 2 * n + 1; i++)
{
for (j = 0; j < 2 * n + 1; j++)
{
if (i == Cx - 3 && j == Cy - 1) map[i][j] = ((char*)" ");
if (i == Cx - 3 && j == Cy + 1) map[i][j] = ((char*)" ");
}
}
return 0;
}
char* clean3(int Cx, int Cy)
{
for (i = 0; i < 2 * n + 1; i++)
{
for (j = 0; j < 2 * n + 1; j++)
{
if (i == Cx - 1 && j == Cy - 3) map[i][j] = ((char*)" ");
if (i == Cx + 1 && j == Cy - 3) map[i][j] = ((char*)" ");
}
}
return 0;
}
char* clean5(int Cx, int Cy)
{
for (i = 0; i < 2 * n + 1; i++)
{
for (j = 0; j < 2 * n + 1; j++)
{
if (i == Cx + 3 && j == Cy - 1) map[i][j] = ((char*)" ");
if (i == Cx + 3 && j == Cy + 1) map[i][j] = ((char*)" ");
}
}
return 0;
}
int check1()
{
if (map[Cx][Cy] == map[Cx][Cy + 2] && map[Cx][Cy + 2] == map[Cx][Cy + 4] && map[Cx][Cy + 4] == map[Cx][Cy + 6] && map[Cx][Cy + 6] == map[Cx][Cy + 8]&& map[Cx][Cy] == map[Cx][Cy + 8])
return 1;
if (map[Cx][Cy] == map[Cx][Cy -2] && map[Cx][Cy-2] == map[Cx][Cy-4] && map[Cx][Cy- 4] == map[Cx][Cy-6] && map[Cx][Cy-6] == map[Cx][Cy-8]&& map[Cx][Cy] == map[Cx][Cy - 8])
return 1;
if (map[Cx][Cy] == map[Cx-2][Cy] && map[Cx-2][Cy] == map[Cx-4][Cy] && map[Cx-4][Cy] == map[Cx-6][Cy] && map[Cx-6][Cy] == map[Cx-8][Cy]&&map[Cx][Cy] == map[Cx - 8][Cy])
return 1;
if (map[Cx][Cy] == map[Cx+ 2][Cy] && map[Cx +2][Cy] == map[Cx+ 4][Cy] && map[Cx+ 4][Cy] == map[Cx+ 6][Cy] && map[Cx+6][Cy] == map[Cx +8][Cy]&&map[Cx][Cy] == map[Cx + 8][Cy])
return 1;
if (map[Cx][Cy] == map[Cx - 2][Cy-2] && map[Cx - 2][Cy-2] == map[Cx - 4][Cy-4] && map[Cx - 4][Cy-4] == map[Cx - 6][Cy-6] && map[Cx - 6][Cy-6] == map[Cx - 8][Cy-8]&&map[Cx][Cy] == map[Cx - 8][Cy - 8])
return 1;
if (map[Cx][Cy] == map[Cx+2][Cy - 2] && map[Cx+ 2][Cy - 2] == map[Cx+ 4][Cy - 4] && map[Cx+4][Cy - 4] == map[Cx+6][Cy - 6] && map[Cx+ 6][Cy - 6] == map[Cx+8][Cy - 8]&&map[Cx][Cy]== map[Cx + 8][Cy - 8])
return 1;
if (map[Cx][Cy] == map[Cx - 2][Cy+ 2] && map[Cx - 2][Cy+2] == map[Cx - 4][Cy+4] && map[Cx - 4][Cy+4] == map[Cx - 6][Cy+6] && map[Cx - 6][Cy+6] == map[Cx - 8][Cy+8]&&map[Cx - 6][Cy + 6] == map[Cx - 8][Cy + 8])
return 1;
if (map[Cx][Cy] == map[Cx + 2][Cy+2] && map[Cx + 2][Cy+2] == map[Cx + 4][Cy+4] && map[Cx + 4][Cy+4] == map[Cx + 6][Cy+6] && map[Cx + 6][Cy+6] == map[Cx + 8][Cy+8]&&map[Cx + 6][Cy + 6] == map[Cx + 8][Cy + 8])
return 1;
else
return 0;
}
int check2()
{
for (i = -4; i <= 4; i++)
{
if (map[Cx + 2 * i][Cy] == map[Cx][Cy])
w++;
else w = 1;
}
if (w >5)
return 1;
for (i = -4; i <= 4; i++)
{
if (map[Cx][Cy+2*i] == map[Cx][Cy])
x++;
else x= 1;
}
if (x>5)
return 1;
for (i = -4; i <= 4; i++)
{
if (map[Cx + 2 * i][Cy+2*i] == map[Cx][Cy])
y++;
else y = 1;
}
if (y>5)
return 1;
for (i = -4; i <= 4; i++)
{
if (map[Cx + 2 * i][Cy-2*i] == map[Cx][Cy])
z++;
else z= 1;
}
if (z>5)
return 1;
else
return 0;
}
void operate1()
{
input = _getch();
if (input == 0X20)
{
if (now == 1)
{
if(map[Cx][Cy] != ((char*)"○"))
map[Cx][Cy] = ((char*)"●");
else input = _getch();
now = 3 - now;
count++;
}
else
{
if(map[Cx][Cy] != ((char*)"●"))
map[Cx][Cy] = ((char*)"○");
else input = _getch();
now = 3 - now;
count++;
}
}
else if (input == 224)
{
input = _getch();
switch (input)
{
case 75://0X48 左
Cy = Cy - 2, clean1(Cx, Cy);
break;
case 80://0X4D 下
Cx = Cx + 2, clean2(Cx, Cy);
break;
case 77:
Cy = Cy + 2, clean3(Cx, Cy);
break;
case 72://0X4B 上
Cx = Cx - 2, clean5(Cx, Cy);
break;
}
if (Cx < 0)Cx = 2 * n + 1;
if (Cy < 0)Cy = 2 * n + 1;
if (Cx > 2 * n + 1)Cx = 0;
if (Cy > 2 * n + 1)Cy = 0;
}
}
void operate2()
{
printf("请输入坐标\n");
scanf("%d%d", &I, &J);
if (now == 1)
{
if (map[2*I][2*J] != ((char*)"○"))
map[2*I][2*J] = ((char*)"●");
now = 3 - now;
count++;
}
Cx=I,Cy=J;
e=Cx,f=Cy;
if(now==2)
{
AI();
if (map[2*e][2*f] != ((char*)"●"))
map[2*e][2*f] = ((char*)"○");
count++;
}
now = 3 - now;
}
void operate3()
{
printf("请输入横纵坐标\n");
scanf("%d%d",&p,&q);
if((p<0||p>15)||(q<0||q>15))
{
printf("请重新输入!\n");
scanf("%d%d",&p,&q);
}
if (now ==1)
{
if (map[2*p][2*q] != ((char*)"○"))
map[2*p][2*q] = ((char*)"●");
count++;
}
check1();
check2();
if (now ==2)
{
if(map[2*p][2*q] != ((char*)"●"))
map[2*p][2*q]=((char*)"○");
count++;
}
check1();
check2();
now = 3 - now;
}
void AI()
{
for (i= 0; i<=15; i++)
for (j= 0; j<=15;j++)
if ((map[2*i][2*j]!=(char*)"●")&&(map[2 *i][2 *j]!=(char*)"○"))
{
t=value();
if(t>=max)
{
e = 2*i; f= 2*j; max = k;
}
}
}
int value()
{
distance1 =(2*i-Cx)*(2*i-Cx)+(2*j-Cy)*(2*j-Cy);
if (distance1 >128) k = 0;
if (distance1<=4) k=5.0;
if (distance1>4&&distance1<=8) k =4.2;
if (distance1>8&&distance1<=16) k =4;
if (distance1>8 && distance1<= 32) k =3;
if (distance1>32 && distance1 <=64) k = 2;
if (distance1>64&& distance1 <=128) k =1;
return k;
}
int judge()
{
if ((check1()==1||check2()==1)&&map[Cx][Cy] == ((char*)"●"))
{
printf(" 恭喜黑方获得胜利\n");
return 1;
}
else if ((check1()==1||check2()==1)&&map[Cx][Cy]==((char*)"○"))
{
printf(" 恭喜白方获得胜利\n");
return 1;
}
else if (count == (n + 1)*(n + 1))
{
printf(" 平局!\n");
return 1;
}
else printf(" \n");
return 0;
}
void rungame1()
{
initialize();
displaycursor();
getmap();
while (1)
{
system("cls");
displaycursor();
printfmap();
operate1();
if (judge() == 1)
{
Sleep(1000);
rungame1();
}
}
}
void rungame2()
{
initialize();
getmap();
while (1)
{
system("cls");
printfmap();
operate2();
if (judge() == 1)
{
Sleep(1000);
rungame2();
}
}
}
void rungame3()
{
initialize();
getmap();
while (1)
{
system("cls");
printfmap();
operate3();
if (judge() == 1)
{
Sleep(2000);
rungame3();
}
}
}
void rungame()
{
windows1();
scanf("%d", &modle);
switch (modle)
{
case 1: rungame1(); break;
case 2: rungame2(); break;
case 3: rungame3(); break;
case 0: break;
default: break;
}
}
int main()
{
system("title 简易五子棋");
system("mode con cols=63lines=63");
system("color 7C");
rungame();
return 0;
}