codeblocks的C语言围棋对弈程序
时间: 2023-06-24 12:07:10 浏览: 54
以下是一个简单的 Code::Blocks C 语言围棋对弈程序示例,仅供参考:
```c
#include <stdio.h>
#include <stdlib.h>
#define BOARD_SIZE 15
char board[BOARD_SIZE][BOARD_SIZE]; // 棋盘
void init_board();
void print_board();
int check_winner(int row, int col, char chess);
int check_horizontal(int row, int col, char chess);
int check_vertical(int row, int col, char chess);
int check_left_diagonal(int row, int col, char chess);
int check_right_diagonal(int row, int col, char chess);
int main()
{
int row, col, turn = 0;
char player1[20], player2[20];
printf("请输入玩家1的名字:");
scanf("%s", player1);
printf("请输入玩家2的名字:");
scanf("%s", player2);
init_board(); // 初始化棋盘
while (1) {
print_board(); // 打印棋盘
// 玩家1下棋
if (turn % 2 == 0) {
printf("%s,请输入你的下棋位置(行 列):", player1);
scanf("%d %d", &row, &col);
if (board[row][col] == ' ') {
board[row][col] = 'X';
if (check_winner(row, col, 'X')) {
printf("%s 胜利!\n", player1);
break;
}
turn++;
} else {
printf("此处已有棋子,请重新输入!\n");
}
}
// 玩家2下棋
else {
printf("%s,请输入你的下棋位置(行 列):", player2);
scanf("%d %d", &row, &col);
if (board[row][col] == ' ') {
board[row][col] = 'O';
if (check_winner(row, col, 'O')) {
printf("%s 胜利!\n", player2);
break;
}
turn++;
} else {
printf("此处已有棋子,请重新输入!\n");
}
}
}
return 0;
}
// 初始化棋盘
void init_board()
{
int i, j;
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
board[i][j] = ' ';
}
}
}
// 打印棋盘
void print_board()
{
int i, j;
printf(" ");
for (i = 0; i < BOARD_SIZE; i++) {
printf("%d ", i);
}
printf("\n");
for (i = 0; i < BOARD_SIZE; i++) {
printf("%d ", i);
for (j = 0; j < BOARD_SIZE; j++) {
printf("%c ", board[i][j]);
}
printf("\n");
}
}
// 检查是否有玩家获胜
int check_winner(int row, int col, char chess)
{
if (check_horizontal(row, col, chess) >= 5 ||
check_vertical(row, col, chess) >= 5 ||
check_left_diagonal(row, col, chess) >= 5 ||
check_right_diagonal(row, col, chess) >= 5) {
return 1;
}
return 0;
}
// 检查水平方向是否有5个棋子相连
int check_horizontal(int row, int col, char chess)
{
int count = 1, i;
// 向左
for (i = col - 1; i >= 0 && board[row][i] == chess; i--) {
count++;
}
// 向右
for (i = col + 1; i < BOARD_SIZE && board[row][i] == chess; i++) {
count++;
}
return count;
}
// 检查垂直方向是否有5个棋子相连
int check_vertical(int row, int col, char chess)
{
int count = 1, i;
// 向上
for (i = row - 1; i >= 0 && board[i][col] == chess; i--) {
count++;
}
// 向下
for (i = row + 1; i < BOARD_SIZE && board[i][col] == chess; i++) {
count++;
}
return count;
}
// 检查左斜方向是否有5个棋子相连
int check_left_diagonal(int row, int col, char chess)
{
int count = 1, i, j;
// 向左上
for (i = row - 1, j = col - 1; i >= 0 && j >= 0 && board[i][j] == chess; i--, j--) {
count++;
}
// 向右下
for (i = row + 1, j = col + 1; i < BOARD_SIZE && j < BOARD_SIZE && board[i][j] == chess; i++, j++) {
count++;
}
return count;
}
// 检查右斜方向是否有5个棋子相连
int check_right_diagonal(int row, int col, char chess)
{
int count = 1, i, j;
// 向右上
for (i = row - 1, j = col + 1; i >= 0 && j < BOARD_SIZE && board[i][j] == chess; i--, j++) {
count++;
}
// 向左下
for (i = row + 1, j = col - 1; i < BOARD_SIZE && j >= 0 && board[i][j] == chess; i++, j--) {
count++;
}
return count;
}
```
这个程序实现了一个简单的人机对弈,只有一个玩家可以下棋,电脑随机下棋。如果需要实现人人对弈,则需要修改程序让两个玩家轮流下棋。
阅读全文