c语言五子棋视频教学视频,C语言实现五子棋

/**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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值