五子棋
1.基本思路下一个棋子,先横向判断,向左判断,直到不为该颜色棋子,向右判断直到不为该颜色棋子,(也就是判断最多五次)
其他方向类似
2.基本操作
输入两个a-z的字符回车即可
3.异常处理
输入错误,会重新录入
输入棋子存在,会重新录入
输入大小写均可
好久之前写的,今天用了下,有点bug调了下。
程序代码:#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<windows.h>
static char* chess[15][15];
bool q=true;
bool Q=true;
int count=0;
//绘制棋盘
void Full()
{
for(int i=0;i<15;i++)
{
for(int j=0;j<15;j++)
{
if(i==0)
{
chess[i][j] = "╦";
}
if(i==14)
{
chess[i][j] = "╩";
}
if(j==0)
{
chess[i][j] = "╠";
}
if(j==14)
{
chess[i][j] = "╣";
}
if(i==0 && j==0 )
{
chess[i][j] = "╔";
}
if(i==0 && j==14)
{
chess[i][j] = "╗";
}
if(i==14 && j==0)
{
chess[i][j] = "╚";
}
if(i==14 && j==14)
{
chess[i][j] = "╝";
}
if(i>0 && i<14 && j>0 && j<14)
{
chess[i][j] = "╬";
}
}
}
}
//显示棋盘
void Show()
{
char c='A';
for(int m=0;m<15;m++)
{
printf("%2c",c+m);
}
printf("\n");
for(int k=0;k<15;k++)
{
if(k==0) printf("%c",c);
for(int l=0;l<15;l++)
{
printf("%s",chess[k][l]);
}
printf("\n");
if(k<14) printf("%c",c+k+1);
}
}
//下棋合法性检测
bool Check(char* c)
{
for(int i=0;i<2;i++)
{
c[i]=(((c[i]>='A')&&(c[i]<='Z'))?c[i]+32:c[i]);
if(c[i]<'a'||c[i]>'o')
return false;
}
int x=c[0]-'a';
int y=c[1]-'a';
if((strcmp(chess[x][y],"●")==0)||(strcmp(chess[x][y],"○")==0))
return false;
else
return true;
}
//输赢判断
bool win(int x,int y,char* who)
{
int i = 0;
int j = 0;
int nCount = 0;
//横向检测
i = x;
while(1)
{
if(strcmp(chess[i][y],who)==0)
{
nCount++;
if(nCount >= 5)
return true;
}else {
break;
}
i++;
if(i > 14)
break;
}
i = x;
while(1)
{
if(strcmp(chess[i][y],who)==0)
{
nCount++;
if(nCount > 5)
return true;
}else {
break;
}
i--;
if(i < 0)
break;
}
//纵向检测
j = y;
nCount = 0;
while(1)
{
if(strcmp(chess[x][j],who)==0)
{
nCount++;
if(nCount >= 5)
return true;
}else {
break;
}
j++;
if(j > 14)
break;
}
j = y;
while(1)
{
if(strcmp(chess[x][j],who)==0)
{
nCount++;
if(nCount > 5)
return true;
}
else
{
break;
}
j--;
if(j < 0)
break;
}
//左下至右上
i = x;
j = y;
nCount = 0;
while(1)
{
if(strcmp(chess[i][j],who)==0)
{
nCount++;
if(nCount >= 5)
return true;
}else {
break;
}
i++;
j--;
if(i > 14 || j < 0)
break;
}
i = x;
j = y;
while(1)
{
if(strcmp(chess[i][j],who)==0)
{
nCount++;
if(nCount > 5)
return true;
}
else
{
break;
}
i--;
j++;
if(j > 14 || i < 0)
break;
}
//左上右下
i = x;
j = y;
nCount = 0;
while(1)
{
if(strcmp(chess[i][j],who)==0)
{
nCount++;
if(nCount >= 5)
return true;
}else {
break;
}
i++;
j++;
if(i > 14 || j > 14)
break;
}
i = x;
j = y;
while(1)
{
if(strcmp(chess[i][j],who)==0)
{
nCount++;
if(nCount > 5)
return true;
}
else
{
break;
}
i--;
j--;
if(i < 0 || j < 0)
break;
}
return false;
}
//下棋
void Chess()
{
q=!q;
char c[100];
int x,y;
char* who=(q?"●":"○");
printf("请%s下棋:",who);
scanf("%s",c);
while(!Check(c))
{
printf("请%s重新下棋:",who);
scanf("%s",c);
}
x=c[0]-'a';
y=c[1]-'a';
chess[x][y] = who;
count++;
system("cls");
Show();
if((win(x,y,who))&&(q==true))
{
printf("黑方胜利!!\n");
Q=false;
}
if((win(x,y,who))&&(q==false))
{
printf("白方胜利!!\n");
Q=false;
}
if(count==225)
{
printf("平局\n");
Q=false;
}
}
int main()
{
//绘制棋盘
Full();
//显示棋盘
Show();
while(Q)
{
Chess();
}
return 0;
}[此贴子已经被作者于2017-5-17 00:44编辑过]









