/**Gobang.c*/#include#include
#define N 15
int chessboard[N+1][N+1] = {0};int whoseTurn = 0;voidinitGame();voidprintChessboard();voidplayChess();int judge(int x, inty);intmain()
{
initGame();while(1)
{
whoseTurn++;
playChess();
}return 0;
}voidinitGame()
{charc;
printf("Input Y to enter the game:");
c=getchar();if (c != 'y' && c != 'Y')
{
exit(0);
}
system("clear");
printChessboard();
}voidprintChessboard()
{inti,j;for (i = 0; i <= N; i++)
{for (j = 0; j <= N; j++)
{if (0 ==i)
{
printf("%3d", j);
}else if(0 ==j)
{
printf("%3d", i);
}else if (1 ==chessboard[i][j])
{
printf("X");
}else if (2 ==chessboard[i][j])
{
printf("O");
}else{
printf("*");
}
}
printf("\n");
}
}voidplayChess()
{inti, j, winner;if (1 == whoseTurn % 2)
{
printf("Turn to player 1, please input the position:");
scanf("%d %d", &i, &j);while(chessboard[i][j] != 0 || i > N || i < 0 || j > N || j < 0)
{
printf("your position is taken, choose another:");
scanf("%d %d", &i, &j);
}
chessboard[i][j]= 1;
}else{
printf("Turn to player 2, please input the position:");
scanf("%d %d", &i, &j);while(chessboard[i][j] != 0 || i > N || i < 0 || j > N || j < 0)
{
printf("your position is taken, choose another:");
scanf("%d %d", &i, &j);
}
chessboard[i][j]= 2;
}
system("clear");
printChessboard();if(judge(i, j))
{if (1 == whoseTurn % 2)
{
printf("player1 win\n");
exit(0);
}else{
printf("player2 win\n");
exit(0);
}
}
}int judge(int x, inty)
{inti, j, k;int t = 2 - whoseTurn % 2;const int step[4][2] = {{-1, 0}, {0, -1}, {1, 1}, {1, 0}};for (i = 0; i < 4; i++)
{const int d[2] = {-1, 1};int count = 1;for (j = 0; j < 2; ++j)
{for (k = 1; k <= 4; k++)
{int row = x + k*d[j]*step[i][0];int col = y + k*d[j]*step[i][1];if (row > 1 && row <= N && col >= 1 && col <= N &&chessboard[x][y]==chessboard[row][col])
{
count++;
}else{break;
}
}
}if (count >= 5)
{return 1;
}
}return 0;
}