用c语言写一个具有对局回放功能的五子棋小游戏
时间: 2024-03-24 18:36:32 浏览: 55
游戏,欢乐五子棋,C#实现完整源代码,界面大气美观,跟电脑对战,电脑反应神速,人机对战,赢一局很难,F1可以重新开始对战
好的,我可以为您提供一个五子棋小游戏的基本代码,包括对局回放功能,代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define BOARD_SIZE 15 // 棋盘大小
#define EMPTY 0 // 空格
#define BLACK 1 // 黑棋
#define WHITE 2 // 白棋
// 棋盘
int board[BOARD_SIZE][BOARD_SIZE];
// 保存每一步的信息
typedef struct {
int x;
int y;
int player;
} Step;
// 保存所有的步骤
Step steps[BOARD_SIZE * BOARD_SIZE];
int stepCount = 0;
// 初始化棋盘
void initBoard() {
memset(board, EMPTY, sizeof(board));
}
// 打印棋盘
void printBoard() {
printf(" ");
for (int i = 0; i < BOARD_SIZE; i++) {
printf("%2d ", i);
}
printf("\n");
for (int i = 0; i < BOARD_SIZE; i++) {
printf("%2d ", i);
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == EMPTY) {
printf(". ");
} else if (board[i][j] == BLACK) {
printf("X ");
} else {
printf("O ");
}
}
printf("\n");
}
}
// 判断胜负
int checkWin(int player) {
int count = 0;
// 判断横向
for (int i = 0; i < BOARD_SIZE; i++) {
count = 0;
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == player) {
count++;
if (count == 5) {
return 1;
}
} else {
count = 0;
}
}
}
// 判断纵向
for (int i = 0; i < BOARD_SIZE; i++) {
count = 0;
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[j][i] == player) {
count++;
if (count == 5) {
return 1;
}
} else {
count = 0;
}
}
}
// 判断正对角线
for (int i = 0; i < BOARD_SIZE - 4; i++) {
for (int j = 0; j < BOARD_SIZE - 4; j++) {
count = 0;
for (int k = 0; k < 5; k++) {
if (board[i+k][j+k] == player) {
count++;
if (count == 5) {
return 1;
}
} else {
count = 0;
}
}
}
}
// 判断反对角线
for (int i = 0; i < BOARD_SIZE - 4; i++) {
for (int j = 4; j < BOARD_SIZE; j++) {
count = 0;
for (int k = 0; k < 5; k++) {
if (board[i+k][j-k] == player) {
count++;
if (count == 5) {
return 1;
}
} else {
count = 0;
}
}
}
}
return 0;
}
// 保存步骤
void saveStep(int x, int y, int player) {
Step step;
step.x = x;
step.y = y;
step.player = player;
steps[stepCount++] = step;
}
// 悔棋
void undo() {
if (stepCount == 0) {
printf("已经没有步骤可以悔了!\n");
return;
}
Step step = steps[--stepCount];
board[step.x][step.y] = EMPTY;
printf("悔棋成功!\n");
}
// 对局回放
void replay() {
for (int i = 0; i < stepCount; i++) {
Step step = steps[i];
board[step.x][step.y] = step.player;
printBoard();
printf("\n");
printf("%c 下在 (%d, %d)\n", step.player == BLACK ? 'X' : 'O', step.x, step.y);
printf("按任意键继续...\n");
getchar();
}
}
int main() {
srand(time(NULL));
initBoard();
printBoard();
int player = BLACK;
while (1) {
int x, y;
printf("%c 的回合,请输入落子位置(x, y):", player == BLACK ? 'X' : 'O');
scanf("%d%d", &x, &y);
if (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE) {
printf("位置无效,请重新输入!\n");
continue;
}
if (board[x][y] != EMPTY) {
printf("该位置已经有子,请重新输入!\n");
continue;
}
board[x][y] = player;
saveStep(x, y, player);
printBoard();
if (checkWin(player)) {
printf("%c 获胜!\n", player == BLACK ? 'X' : 'O');
break;
}
if (stepCount == BOARD_SIZE * BOARD_SIZE) {
printf("平局!\n");
break;
}
player = player == BLACK ? WHITE : BLACK;
}
printf("按 1 回放对局,按其他键退出:");
int choice;
scanf("%d", &choice);
if (choice == 1) {
replay();
}
return 0;
}
```
这个代码使用了 `Step` 结构体来保存每一步的信息,使用了 `steps` 数组来保存所有的步骤,使用了 `stepCount` 变量来保存当前步骤的数量。在每次落子时,会将当前步骤保存到 `steps` 数组中,落子结束后会检查是否有玩家获胜或者棋盘已经下满,如果有则结束游戏。在游戏结束后,会提示用户是否要回放对局,如果用户输入 `1` 则进行对局回放,否则退出程序。
阅读全文