用C++代码写一个五子棋游戏
时间: 2023-04-07 21:02:39 浏览: 120
好的,我可以回答这个问题。以下是一个简单的五子棋游戏的 C 代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define BOARD_SIZE 15
char board[BOARD_SIZE][BOARD_SIZE];
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("%c ", 'A' + i);
}
printf("\n");
for (i = 0; i < BOARD_SIZE; i++) {
printf("%2d", i + 1);
for (j = 0; j < BOARD_SIZE; j++) {
printf("%c ", board[i][j]);
}
printf("\n");
}
}
int is_valid_move(int x, int y) {
if (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE) {
return 0;
}
if (board[x][y] != '+') {
return 0;
}
return 1;
}
int is_win(int x, int y) {
int i, j, k;
char c = board[x][y];
for (i = -4; i <= 4; i++) {
if (i == 0) {
continue;
}
j = x + i;
k = y;
if (j >= 0 && j < BOARD_SIZE && k >= 0 && k < BOARD_SIZE &&
board[j][k] == c) {
j += i;
if (j >= 0 && j < BOARD_SIZE && board[j][k] == c) {
j += i;
if (j >= 0 && j < BOARD_SIZE && board[j][k] == c) {
j += i;
if (j >= 0 && j < BOARD_SIZE && board[j][k] == c) {
j += i;
if (j >= 0 && j < BOARD_SIZE && board[j][k] == c) {
return 1;
}
}
}
}
}
j = x;
k = y + i;
if (j >= 0 && j < BOARD_SIZE && k >= 0 && k < BOARD_SIZE &&
board[j][k] == c) {
k += i;
if (j >= 0 && j < BOARD_SIZE && board[j][k] == c) {
k += i;
if (j >= 0 && j < BOARD_SIZE && board[j][k] == c) {
k += i;
if (j >= 0 && j < BOARD_SIZE && board[j][k] == c) {
k += i;
if (j >= 0 && j < BOARD_SIZE && board[j][k] == c) {
return 1;
}
}
}
}
}
j = x + i;
k = y + i;
if (j >= 0 && j < BOARD_SIZE && k >= 0 && k < BOARD_SIZE &&
board[j][k] == c) {
j += i;
k += i;
if (j >= 0 && j < BOARD_SIZE && k >= 0 && k < BOARD_SIZE &&
board[j][k] == c) {
j += i;
k += i;
if (j >= 0 && j < BOARD_SIZE && k >= 0 && k < BOARD_SIZE &&
board[j][k] == c) {
j += i;
k += i;
if (j >= 0 && j < BOARD_SIZE && k >= 0 && k < BOARD_SIZE &&
board[j][k] == c) {
j += i;
k += i;
if (j >= 0 && j < BOARD_SIZE && k >= 0 && k < BOARD_SIZE &&
board[j][k] == c) {
return 1;
}
}
}
}
}
j = x + i;
k = y - i;
if (j >= 0 && j < BOARD_SIZE && k >= 0 && k < BOARD_SIZE &&
board[j][k] == c) {
j += i;
k -= i;
if (j >= 0 && j < BOARD_SIZE && k >= 0 && k < BOARD_SIZE &&
board[j][k] == c) {
j += i;
k -= i;
if (j >= 0 && j < BOARD_SIZE && k >= 0 && k < BOARD_SIZE &&
board[j][k] == c) {
j += i;
k -= i;
if (j >= 0 && j < BOARD_SIZE && k >= 0 && k < BOARD_SIZE &&
board[j][k] == c) {
j += i;
k -= i;
if (j >= 0 && j < BOARD_SIZE && k >= 0 && k < BOARD_SIZE &&
board[j][k] == c) {
return 1;
}
}
}
}
}
}
return 0;
}
int main() {
int x, y, turn = 0;
init_board();
while (1) {
print_board();
if (turn % 2 == 0) {
printf("Black's turn (x y): ");
} else {
printf("White's turn (x y): ");
}
scanf("%d%d", &x, &y);
if (!is_valid_move(x, y)) {
printf("Invalid move!\n");
continue;
}
if (turn % 2 == 0) {
board[x][y] = 'X';
} else {
board[x][y] = 'O';
}
if (is_win(x, y)) {
print_board();
if (turn % 2 == 0) {
printf("Black wins!\n");
} else {
printf("White wins!\n");
}
break;
}
turn++;
}
return 0;
}
```
这个程序实现了一个简单的五子棋游戏,可以在命令行中运行。玩家可以输入坐标来下棋,程序会检查是否合法,并判断胜负。
阅读全文