Add files via upload

This commit is contained in:
2025-06-30 18:19:53 +08:00
committed by GitHub
parent b05d03c614
commit 0970c3fd82
5 changed files with 300 additions and 171 deletions
+106 -15
View File
@@ -9,6 +9,42 @@
#include <conio.h> #include <conio.h>
#endif #endif
/**
* @brief 从用户获取整数输入
*
* @param prompt 提示信息
* @param min 最小值
* @param max 最大值
* @return int 用户输入的整数
*/
int get_integer_input(const char *prompt, int min, int max)
{
int value;
int result;
char ch;
while (1)
{
printf("%s", prompt);
result = scanf("%d", &value);
if (result == 1 && value >= min && value <= max)
{
// 清除输入缓冲区中剩余的字符
while ((ch = getchar()) != '\n' && ch != EOF)
;
return value;
}
else
{
// 清除无效输入
while ((ch = getchar()) != '\n' && ch != EOF)
;
printf("输入无效,请输入一个介于 %d 和 %d 之间的整数。\n", min, max);
}
}
}
/** /**
* @brief 处理玩家回合 * @brief 处理玩家回合
* *
@@ -190,11 +226,9 @@ void run_ai_game()
review_choice = get_integer_input("是否要复盘本局比赛? (1-是, 0-否): ", 0, 1); review_choice = get_integer_input("是否要复盘本局比赛? (1-是, 0-否): ", 0, 1);
if (review_choice == 1) if (review_choice == 1)
{ {
review_process(); review_process(1); // 1 for AI mode
} }
end_game: handle_save_record(1); // 1 for AI mode
end_pvp_game:
handle_save_record();
} }
/** /**
@@ -235,9 +269,9 @@ void run_pvp_game()
review_choice = get_integer_input("是否要复盘本局比赛? (1-是, 0-否): ", 0, 1); review_choice = get_integer_input("是否要复盘本局比赛? (1-是, 0-否): ", 0, 1);
if (review_choice == 1) if (review_choice == 1)
{ {
review_process(); review_process(2); // 2 for PvP mode
} }
handle_save_record(); handle_save_record(2); // 2 for PvP mode
} }
/** /**
@@ -246,18 +280,75 @@ void run_pvp_game()
*/ */
void run_review_mode() void run_review_mode()
{ {
// 复盘模式 char filename[100];
char filename[256]; char record_files[100][100];
printf("请输入复盘文件地址: "); int file_count = 0;
scanf("%s", filename);
if (load_game_from_file(filename)) #ifdef _WIN32
WIN32_FIND_DATA ffd;
HANDLE hFind = FindFirstFile("records\\*", &ffd);
if (hFind != INVALID_HANDLE_VALUE)
{ {
printf("成功加载历史记录: %s\n", filename); do
review_process(); {
if (!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
strcpy(record_files[file_count++], ffd.cFileName);
}
} while (FindNextFile(hFind, &ffd) != 0);
FindClose(hFind);
}
#endif
if (file_count > 0)
{
printf("发现以下复盘文件:\n");
for (int i = 0; i < file_count; i++)
{
printf("%d. %s\n", i + 1, record_files[i]);
}
char prompt[150];
sprintf(prompt, "请输入复盘文件编号(1-%d),或输入0以手动输入文件名: ", file_count);
int choice = get_integer_input(prompt, 0, file_count);
if (choice > 0)
{
strcpy(filename, record_files[choice - 1]);
} }
else else
{ {
printf("加载历史记录失败: %s\n", filename); printf("请输入完整文件名: ");
exit(1); scanf("%s", filename);
int c;
while ((c = getchar()) != '\n' && c != EOF);
int possible_choice = atoi(filename);
if (possible_choice > 0 && possible_choice <= file_count)
{
strcpy(filename, record_files[possible_choice - 1]);
}
}
}
else
{
printf("未找到任何复盘文件,请输入复盘文件地址: ");
scanf("%s", filename);
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
int game_mode = load_game_from_file(filename);
if (game_mode != 0)
{
if (game_mode == 1)
{
printf("加载AI对战模式复盘文件成功!\n");
}
else if (game_mode == 2)
{
printf("加载双人对战模式复盘文件成功!\n");
}
review_process(game_mode);
} }
} }
+10
View File
@@ -18,6 +18,16 @@
#include "gobang.h" #include "gobang.h"
/**
* @brief 从用户获取整数输入
*
* @param prompt 提示信息
* @param min 最小值
* @param max 最大值
* @return int 输入的整数
*/
int get_integer_input(const char *prompt, int min, int max);
/** /**
* @brief 处理玩家回合 * @brief 处理玩家回合
* *
+169 -141
View File
@@ -1,4 +1,5 @@
#include "gobang.h" #include "gobang.h"
#include "game_mode.h"
#include <stdio.h> #include <stdio.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <time.h> #include <time.h>
@@ -78,11 +79,10 @@ bool have_space(int x, int y)
} }
/** /**
* @brief 玩家落子操作 * @brief 配置棋盘大小
* *
* @param player1 * @param player1 玩家1
* @param player2 * @param player2 玩家2
* @return int player1 or player2
*/ */
void setup_board_size() void setup_board_size()
{ {
@@ -103,7 +103,7 @@ void setup_game_options()
use_timer = get_integer_input("是否启用计时器 (1-是, 0-否): ", 0, 1); use_timer = get_integer_input("是否启用计时器 (1-是, 0-否): ", 0, 1);
if (use_timer) if (use_timer)
{ {
time_limit = get_integer_input("请输入每回合的时间限制 (分钟): ", 1, 60) * 60; time_limit = get_integer_input("请输入每回合的时间限制 (1~60分钟): ", 1, 60) * 60;
} }
} }
@@ -462,32 +462,6 @@ int dfs(int x, int y, int player, int depth, int alpha, int beta, bool is_maximi
* - 步数>10时缩小搜索范围到已有棋子附近2格 * - 步数>10时缩小搜索范围到已有棋子附近2格
* - 使用中心位置优先策略 * - 使用中心位置优先策略
*/ */
int get_integer_input(const char *prompt, int min, int max)
{
int value;
int result;
char ch;
while (1)
{
printf("%s", prompt);
result = scanf("%d", &value);
if (result == 1 && value >= min && value <= max)
{
// 清除输入缓冲区中剩余的字符
while ((ch = getchar()) != '\n' && ch != EOF);
return value;
}
else
{
// 清除无效输入
while ((ch = getchar()) != '\n' && ch != EOF);
printf("输入无效,请输入一个介于 %d 和 %d 之间的整数。\n", min, max);
}
}
}
void ai_move(int depth) void ai_move(int depth)
{ {
// 1. 首先检查是否需要阻止玩家的四子连棋或三子活棋 // 1. 首先检查是否需要阻止玩家的四子连棋或三子活棋
@@ -616,7 +590,7 @@ void ai_move(int depth)
* - 包含输入缓冲区清理防止意外输入 * - 包含输入缓冲区清理防止意外输入
* - 评分环节调用evaluate_performance()函数 * - 评分环节调用evaluate_performance()函数
*/ */
void review_process() void review_process(int game_mode)
{ {
printf("\n===== 复盘记录(总步数:%d) =====\n", step_count); printf("\n===== 复盘记录(总步数:%d) =====\n", step_count);
// 清空输入缓冲区 // 清空输入缓冲区
@@ -635,11 +609,25 @@ void review_process()
temp_board[s.x][s.y] = s.player; // 在临时棋盘上落子 temp_board[s.x][s.y] = s.player; // 在临时棋盘上落子
// 打印当前步骤信息 // 打印当前步骤信息
// 根据游戏模式显示不同的标题和玩家信息
if (game_mode == 1)
{
// 人机对战
printf("\n===== 五子棋人机对战(%dX%d棋盘) =====", BOARD_SIZE, BOARD_SIZE); printf("\n===== 五子棋人机对战(%dX%d棋盘) =====", BOARD_SIZE, BOARD_SIZE);
printf("\n 第%d步/%d步: %s 落子于(%d, %d)\n", printf("\n 第%d步/%d步: %s 落子于(%d, %d)\n",
i + 1, step_count, i + 1, step_count,
(s.player == PLAYER) ? "玩家" : "AI", // 三目运算符选择显示文本 (s.player == PLAYER) ? "玩家" : "AI",
s.x + 1, s.y + 1); // 显示1-base坐标 s.x + 1, s.y + 1);
}
else
{
// 双人对战
printf("\n===== 五子棋双人对战(%dX%d棋盘) =====", BOARD_SIZE, BOARD_SIZE);
printf("\n 第%d步/%d步: %s 落子于(%d, %d)\n",
i + 1, step_count,
(s.player == PLAYER3) ? "玩家1(黑棋)" : "玩家2(白棋)",
s.x + 1, s.y + 1);
}
// 打印当前复盘棋盘 // 打印当前复盘棋盘
printf(" "); printf(" ");
@@ -652,9 +640,9 @@ void review_process()
printf("%2d ", row + 1); // 行号 printf("%2d ", row + 1); // 行号
for (int col = 0; col < BOARD_SIZE; col++) for (int col = 0; col < BOARD_SIZE; col++)
{ {
if (temp_board[row][col] == PLAYER) if (temp_board[row][col] == PLAYER || temp_board[row][col] == PLAYER3)
printf("x "); printf("x ");
else if (temp_board[row][col] == AI) else if (temp_board[row][col] == AI || temp_board[row][col] == PLAYER4)
printf(""); printf("");
else else
printf("· "); printf("· ");
@@ -675,33 +663,63 @@ void review_process()
// 评估双方表现 // 评估双方表现
printf("\n===== 对局评分 =====\n"); printf("\n===== 对局评分 =====\n");
int player_score = evaluate_performance(PLAYER); int player1_score = 0, player2_score = 0;
int ai_score = evaluate_performance(AI);
double sum_score = (long double)player_score + (long double)ai_score; // 遍历所有步数,累积每一步的得分
for (int i = 0; i < step_count; i++)
if (sum_score > 0)
{ {
printf("玩家得分: %d, 占比: %.2f%%\n", if (steps[i].player == PLAYER || steps[i].player == PLAYER3)
player_score, (double)player_score * 100.0 / sum_score); {
printf("AI得分: %d, 占比: %.2f%%\n", player1_score += calculate_step_score(steps[i].x, steps[i].y, steps[i].player);
ai_score, (double)ai_score * 100.0 / sum_score);
} }
else else
{ {
printf("玩家得分: %d\n", player_score); player2_score += calculate_step_score(steps[i].x, steps[i].y, steps[i].player);
printf("AI得分: %d\n", ai_score); }
}
double sum_score = (long double)player1_score + (long double)player2_score;
if (sum_score > 0)
{
if (game_mode == 1)
{
printf("玩家得分: %d, 占比: %.2f%%\n",
player1_score, (double)player1_score * 100.0 / sum_score);
printf("AI得分: %d, 占比: %.2f%%\n",
player2_score, (double)player2_score * 100.0 / sum_score);
}
else
{
printf("玩家1(黑棋)得分: %d, 占比: %.2f%%\n",
player1_score, (double)player1_score * 100.0 / sum_score);
printf("玩家2(白棋)得分: %d, 占比: %.2f%%\n",
player2_score, (double)player2_score * 100.0 / sum_score);
}
}
else
{
if (game_mode == 1)
{
printf("玩家得分: %d\n", player1_score);
printf("AI得分: %d\n", player2_score);
}
else
{
printf("玩家1(黑棋)得分: %d\n", player1_score);
printf("玩家2(白棋)得分: %d\n", player2_score);
}
printf("注: 双方得分均为0,无法计算占比\n"); printf("注: 双方得分均为0,无法计算占比\n");
} }
// 评选MVP // 评选MVP
if (player_score > ai_score) if (player1_score > player2_score)
{ {
printf("\nMVP: 玩家 (领先 %d 分)\n", player_score - ai_score); printf("\nMVP: %s (领先 %d 分)\n", (game_mode == 1) ? "玩家" : "玩家1(黑棋)", player1_score - player2_score);
} }
else if (ai_score > player_score) else if (player2_score > player1_score)
{ {
printf("\nMVP: AI (领先 %d 分)\n", ai_score - player_score); printf("\nMVP: %s (领先 %d 分)\n", (game_mode == 1) ? "AI" : "玩家2(白棋)", player2_score - player1_score);
} }
else else
{ {
@@ -711,16 +729,11 @@ void review_process()
getchar(); getchar();
} }
/**
* @brief 悔棋功能实现
* @param steps_to_undo 要撤销的步数
* @return true 悔棋成功
* @return false 悔棋失败(步数不足)
*/
/** /**
* @brief 处理游戏结束后的记录保存 * @brief 处理游戏结束后的记录保存
* @return int 保存状态码(0-成功, 1-目录创建失败, 2-文件打开失败, 3-文件写入失败)
*/ */
void handle_save_record() void handle_save_record(int game_mode)
{ {
int save_choice = 0; int save_choice = 0;
printf("===== 游戏结束 =====\n"); printf("===== 游戏结束 =====\n");
@@ -734,7 +747,7 @@ void handle_save_record()
char filename[256]; char filename[256];
strftime(filename, sizeof(filename), "%Y%m%d_%H%M%S.txt", t); strftime(filename, sizeof(filename), "%Y%m%d_%H%M%S.txt", t);
int save_status = save_game_to_file(filename); int save_status = save_game_to_file(filename, game_mode);
switch (save_status) switch (save_status)
{ {
case 0: // 成功 case 0: // 成功
@@ -760,6 +773,13 @@ void handle_save_record()
} }
} }
/**
* @brief 悔棋功能实现
*
* @param steps_to_undo 要悔棋的步数
* @return true 悔棋成功
* @return false 悔棋失败(步数不足)
*/
bool return_move(int steps_to_undo) bool return_move(int steps_to_undo)
{ {
if (step_count < steps_to_undo) if (step_count < steps_to_undo)
@@ -776,25 +796,83 @@ bool return_move(int steps_to_undo)
return true; return true;
} }
/**
* @brief 复盘游戏过程,逐步重现所有落子步骤
* @note 实现逻辑:
* 1. 创建临时棋盘用于复盘展示
* 2. 按步数顺序逐步重现每个落子
* 3. 每步显示当前棋盘状态和落子信息
* 4. 通过用户按Enter键控制步骤前进
* 5. 显示1-based坐标方便用户查看
*/
/** /**
* @brief 评估玩家在整盘棋局中的表现 * @brief 评估玩家在整盘棋局中的表现
* @param player 要评估的玩家(PLAYER/AI) * @param player 要评估的玩家(PLAYER/AI)
* @return int 总分(已考虑方向重复计算) * @return int 总分(已考虑方向重复计算)
* @note 评分标准: * @note 评分标准:
* - 五连:1000000 * - 五连:2500
* - 活四:100000 冲四:10000 死四:500 * - 活四:1000 冲四:500 死四:250
* - 活三:5000 眠三:1000 死三:50 * - 活三:250 眠三:100 死三:50
* - 活二:500 眠二:100 死二:10 * - 活二:50 眠二:20 死二:10
* - 开放单子:50 半开放单子:10 封闭单子:1 * - 开放单子:10 半开放单子:5 封闭单子:1
* @note 实现细节:
* 1. 遍历棋盘所有位置
* 2. 对每个棋子检查四个方向
* 3. 统计所有连子情况并评分
* 4. 最终分数除以4(消除方向重复计算影响)
*/
int calculate_step_score(int x, int y, int player)
{
int step_score = 0;
// 检查四个方向
for (int k = 0; k < 4; k++)
{
DirInfo info = count_specific_direction(x, y, direction[k][0], direction[k][1], player);
// 根据连子数评分
switch (info.continuous_chess)
{
case 5:
step_score += 2500;
break; // 五连
case 4:
if (info.check_start && info.check_end)
step_score += 1000; // 活四
else if (info.check_start || info.check_end)
step_score += 500; // 冲四
else
step_score += 250; // 死四
break;
case 3:
if (info.check_start && info.check_end)
step_score += 250; // 活三
else if (info.check_start || info.check_end)
step_score += 100; // 眠三
else
step_score += 50; // 死三
break;
case 2:
if (info.check_start && info.check_end)
step_score += 50; // 活二
else if (info.check_start || info.check_end)
step_score += 20; // 眠二
else
step_score += 10; // 死二
break;
case 1:
if (info.check_start && info.check_end)
step_score += 10; // 开放单子
else if (info.check_start || info.check_end)
step_score += 5; // 半开放单子
else
step_score += 1; // 封闭单子
break;
}
}
return step_score;
}
/**
* @brief 评估玩家在整盘棋局中的表现
* @param player 要评估的玩家(PLAYER/AI)
* @return int 总分(已考虑方向重复计算)
* @note 评分标准:
* - 五连:2500
* - 活四:1000 冲四:500 死四:250
* - 活三:250 眠三:100 死三:50
* - 活二:50 眠二:20 死二:10
* - 开放单子:10 半开放单子:5 封闭单子:1
* @note 实现细节: * @note 实现细节:
* 1. 遍历棋盘所有位置 * 1. 遍历棋盘所有位置
* 2. 对每个棋子检查四个方向 * 2. 对每个棋子检查四个方向
@@ -810,53 +888,9 @@ int evaluate_performance(int player)
{ {
for (int j = 0; j < BOARD_SIZE; j++) for (int j = 0; j < BOARD_SIZE; j++)
{ {
if (board[i][j] != player) if (board[i][j] == player)
continue;
// 检查四个方向
for (int k = 0; k < 4; k++)
{ {
DirInfo info = count_specific_direction(i, j, direction[k][0], direction[k][1], player); total_score += calculate_step_score(i, j, player);
// 根据连子数评分
switch (info.continuous_chess)
{
case 5:
total_score += 1000;
break; // 五连
case 4:
if (info.check_start && info.check_end)
total_score += 1000; // 活四
else if (info.check_start || info.check_end)
total_score += 500; // 冲四
else
total_score += 200; // 死四
break;
case 3:
if (info.check_start && info.check_end)
total_score += 200; // 活三
else if (info.check_start || info.check_end)
total_score += 100; // 眠三
else
total_score += 20; // 死三
break;
case 2:
if (info.check_start && info.check_end)
total_score += 100; // 活二
else if (info.check_start || info.check_end)
total_score += 50; // 眠二
else
total_score += 10; // 死二
break;
case 1:
if (info.check_start && info.check_end)
total_score += 20; // 开放单子
else if (info.check_start || info.check_end)
total_score += 10; // 半开放单子
else
total_score += 1; // 封闭单子
break;
}
} }
} }
} }
@@ -872,7 +906,7 @@ int evaluate_performance(int player)
* 2: 文件打开失败 * 2: 文件打开失败
* 3: 文件写入失败 * 3: 文件写入失败
*/ */
int save_game_to_file(const char *filename) int save_game_to_file(const char *filename, int game_mode)
{ {
// 创建records目录(如果不存在) // 创建records目录(如果不存在)
struct stat st = {0}; struct stat st = {0};
@@ -906,8 +940,8 @@ int save_game_to_file(const char *filename)
return 2; // 文件打开失败 return 2; // 文件打开失败
} }
// 写入棋盘大小 // 写入游戏模式和棋盘大小
if (fprintf(file, "%d\n", BOARD_SIZE) < 0) if (fprintf(file, "%d\n%d\n", game_mode, BOARD_SIZE) < 0)
{ {
fclose(file); fclose(file);
return 3; // 文件写入失败 return 3; // 文件写入失败
@@ -937,7 +971,7 @@ int save_game_to_file(const char *filename)
* @return true 加载成功 * @return true 加载成功
* @return false 加载失败 * @return false 加载失败
*/ */
bool load_game_from_file(const char *filename) int load_game_from_file(const char *filename)
{ {
// 打开文件 // 打开文件
char fullpath[256]; char fullpath[256];
@@ -948,8 +982,13 @@ bool load_game_from_file(const char *filename)
return false; return false;
} }
// 读取棋盘大小 // 读取游戏模式和棋盘大小
int size; int game_mode, size;
if (fscanf(file, "%d", &game_mode) != 1 || (game_mode != 1 && game_mode != 2))
{
fclose(file);
return 0; // 无效的游戏模式
}
if (fscanf(file, "%d", &size) != 1 || size < 5 || size > MAX_BOARD_SIZE) if (fscanf(file, "%d", &size) != 1 || size < 5 || size > MAX_BOARD_SIZE)
{ {
fclose(file); fclose(file);
@@ -960,24 +999,13 @@ bool load_game_from_file(const char *filename)
BOARD_SIZE = size; BOARD_SIZE = size;
empty_board(); empty_board();
// 读取并重放所有落子步骤 // 读取所有落子步骤
int player, x, y; step_count = 0;
while (fscanf(file, "%d %d %d", &player, &x, &y) == 3) while (fscanf(file, "%d %d %d", &steps[step_count].player, &steps[step_count].x, &steps[step_count].y) == 3)
{ {
if (player != PLAYER && player != AI) step_count++;
{
fclose(file);
return false;
}
if (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE)
{
fclose(file);
return false;
}
board[x][y] = player;
steps[step_count++] = (Step){player, x, y};
} }
fclose(file); fclose(file);
return true; return game_mode;
} }
BIN
View File
Binary file not shown.
+14 -14
View File
@@ -203,16 +203,6 @@ int dfs(int x, int y, int player, int depth, int alpha, int beta, bool is_maximi
*/ */
void ai_move(int depth); void ai_move(int depth);
/**
* @brief Get the integer input object
*
* @param prompt 提示信息
* @param min 最小值
* @param max 最大值
* @return int 输入的整数
*/
int get_integer_input(const char *prompt, int min, int max);
/** /**
* @brief 悔棋功能实现 * @brief 悔棋功能实现
* @return true 悔棋成功 * @return true 悔棋成功
@@ -225,7 +215,7 @@ bool return_move(int steps_to_undo);
* @brief 复盘游戏过程 * @brief 复盘游戏过程
* 逐步重现游戏中的所有落子步骤 * 逐步重现游戏中的所有落子步骤
*/ */
void review_process(); void review_process(int game_mode);
/** /**
* @brief 评估玩家在整盘棋局中的表现 * @brief 评估玩家在整盘棋局中的表现
@@ -234,6 +224,16 @@ void review_process();
*/ */
int evaluate_performance(int player); int evaluate_performance(int player);
/**
* @brief 计算一步棋的得分
* @param x 行坐标
* @param y 列坐标
* @param player 玩家标识
* @return int 一步棋的得分
*/
int calculate_step_score(int x, int y, int player);
/** /**
* @brief 将当前游戏记录保存到文件 * @brief 将当前游戏记录保存到文件
* @param filename 要保存的文件名 * @param filename 要保存的文件名
@@ -243,12 +243,12 @@ int evaluate_performance(int player);
* 2: 文件打开失败 * 2: 文件打开失败
* 3: 文件写入失败 * 3: 文件写入失败
*/ */
int save_game_to_file(const char *filename); int save_game_to_file(const char *filename, int game_mode);
/** /**
* @brief 处理游戏结束后的记录保存 * @brief 处理游戏结束后的记录保存
*/ */
void handle_save_record(); void handle_save_record(int game_mode);
/** /**
* @brief 从文件加载游戏记录 * @brief 从文件加载游戏记录
@@ -256,6 +256,6 @@ void handle_save_record();
* @return true 加载成功 * @return true 加载成功
* @return false 加载失败 * @return false 加载失败
*/ */
bool load_game_from_file(const char *filename); int load_game_from_file(const char *filename);
#endif // GO_BANG_H #endif // GO_BANG_H