/*
五子棋
*/
#include <stdio.h>
#define N 8
#define M 5
char FiveChess[N + 1][N + 1] = {'0'};
int judge()
{
int i,j;
//行方向
for(i = 1; i <= N; i++)
{
for(j = 1; j <= N - M + 1;)
{
char buf = FiveChess[i][j];
if(buf == '0')
{
j++;
}
else
{
int num = 1;
do{
j++;
if(num == M)
return 1;
if(j <= N && buf == FiveChess[i][j])
{
num++;
}
else
break;
}while(1);
}
}
}
//列方向
for(i = 1; i <= N; i++)
{
for(j = 1; j <= N - M + 1;)
{
char buf = FiveChess[j][i];
if(buf == '0')
{
j++;
}
else
{
int num = 1;
do{
j++;
if(num == M)
return 1;
if(j <= N && buf == FiveChess[j][i])
{
num++;
}
else
break;
}while(1);
}
}
}
//左斜线方向
for(i = 1; i <= N - M + 1; i++)
{
for(j = 1; j <= N - M + 1;)
{
int ii = i;
int jj = j;
char buf = FiveChess[ii][jj];
if(buf == '0')
{
j++;
}
else
{
int num = 1;
do{
ii++;
jj++;
if(num == M)
return 1;
if(ii <= N && jj <= N && buf == FiveChess[ii][jj])
{
num++;
}
else
{
j++;
break;
}
}while(1);
}
}
}
//右斜线方向
for(i = 1; i <= N - M + 1; i++)
{
for(j = M; j <= N;)
{
int ii = i;
int jj = j;
char buf = FiveChess[ii][jj];
if(buf == '0')
{
j++;
}
else
{
int num = 1;
do{
ii++;
jj--;
if(num == M)
return 1;
if(ii <= N && jj >= 1 && buf == FiveChess[ii][jj])
{
num++;
}
else
{
j++;
break;
}
}while(1);
}
}
}
return 0;
}
void show()
{
int i,j;
for(i = 0; i <= N; i++)
{
for(j = 0; j <= N; j++)
{
if(FiveChess[i][j] == '0')
printf( .);
else
printf( %c,FiveChess[i][j]);
}
printf(\n);
}
}
int update(int x,int y,int T)
{
if(0 == ((x > 0 && x <= N && y > 0 && y <= N) && FiveChess[x][y] == '0'))
return -1;
if(T == 0)
FiveChess[x][y] = '#';
else
FiveChess[x][y] = '*';
show();
int buf = judge();
if(buf == 1)
{
if(T == 0)
printf(甲获胜\n);
else
printf(乙获胜\n);
}
return buf;
}
void MainLoop()
{
int T = 0;
do{
if(T == 0)
{
int buf;
printf(甲输入坐标:);
int x,y;
scanf(%d%d,&x,&y);
buf = update(x , y , T);
if(buf == 1)
break;
if(buf == -1)
{
do{
printf(输入有错,请重新输入:);
scanf(%d%d,&x,&y);
buf = update(x , y , T);
}while(buf == -1);
if(buf == 1)
break;
}
T = 1;
}
else
{
int buf;
printf(乙输入坐标:);
int x,y;
scanf(%d%d,&x,&y);
buf = update(x , y , T);
if(buf == 1)
break;
if(buf == -1)
{
do{
printf(输入有错,请重新输入:);
scanf(%d%d,&x,&y);
buf = update(x , y , T);
}while(buf == -1);
if(buf == 1)
break;
}
T = 0;
}
}while(1);
}
int main(int argc, const char * argv[])
{
int i,j;
for(i = 0; i <= N; i++)
{
for(j = 0; j <= N; j++)
FiveChess[i][j] = '0';
}
for(i = 1; i <= N; i++)
{
FiveChess[0][i] = '0' + i;
FiveChess[i][0] = '0' + i;
}
MainLoop();
return 0;
}