重构输入处理:统一使用get_integer_input函数替换scanf

This commit is contained in:
2025-07-23 09:37:54 +08:00
parent 59c816767b
commit fbe4f5273c
5 changed files with 65 additions and 150 deletions
+8 -91
View File
@@ -146,28 +146,11 @@ void display_current_config()
void config_board_size() void config_board_size()
{ {
printf("\n当前棋盘大小: %d x %d\n", BOARD_SIZE, BOARD_SIZE); printf("\n当前棋盘大小: %d x %d\n", BOARD_SIZE, BOARD_SIZE);
printf("请输入新的棋盘大小 (%d-%d): ", MIN_BOARD_SIZE, MAX_BOARD_SIZE);
int new_size; int new_size = get_integer_input("请输入新的棋盘大小: ", MIN_BOARD_SIZE, MAX_BOARD_SIZE);
if (scanf("%d", &new_size) == 1)
{
if (new_size >= MIN_BOARD_SIZE && new_size <= MAX_BOARD_SIZE)
{
BOARD_SIZE = new_size; BOARD_SIZE = new_size;
printf("棋盘大小已设置为: %d x %d\n", BOARD_SIZE, BOARD_SIZE); printf("棋盘大小已设置为: %d x %d\n", BOARD_SIZE, BOARD_SIZE);
} }
else
{
printf("无效的棋盘大小!\n");
}
}
else
{
printf("输入格式错误!\n");
// 清除输入缓冲区
while (getchar() != '\n');
}
}
/** /**
* @brief 配置禁手规则 * @brief 配置禁手规则
@@ -175,20 +158,11 @@ void config_board_size()
void config_forbidden_moves() void config_forbidden_moves()
{ {
printf("\n当前禁手规则: %s\n", use_forbidden_moves ? "开启" : "关闭"); printf("\n当前禁手规则: %s\n", use_forbidden_moves ? "开启" : "关闭");
printf("是否启用禁手规则?(1=开启, 0=关闭): ");
int choice; int choice = get_integer_input("是否启用禁手规则?(1=开启, 0=关闭): ", 0, 1);
if (scanf("%d", &choice) == 1)
{
use_forbidden_moves = (choice != 0); use_forbidden_moves = (choice != 0);
printf("禁手规则已%s\n", use_forbidden_moves ? "开启" : "关闭"); printf("禁手规则已%s\n", use_forbidden_moves ? "开启" : "关闭");
} }
else
{
printf("输入格式错误!\n");
while (getchar() != '\n');
}
}
/** /**
* @brief 配置计时器 * @brief 配置计时器
@@ -196,38 +170,20 @@ void config_forbidden_moves()
void config_timer() void config_timer()
{ {
printf("\n当前计时器: %s\n", use_timer ? "开启" : "关闭"); printf("\n当前计时器: %s\n", use_timer ? "开启" : "关闭");
printf("是否启用计时器?(1=开启, 0=关闭): ");
int choice; int choice = get_integer_input("是否启用计时器?(1=开启, 0=关闭): ", 0, 1);
if (scanf("%d", &choice) == 1)
{
use_timer = choice; use_timer = choice;
if (use_timer) if (use_timer)
{ {
printf("请输入时间限制(分钟): "); int new_limit = get_integer_input("请输入时间限制(分钟): ", 1, 999);
int new_limit;
if (scanf("%d", &new_limit) == 1 && new_limit > 0)
{
time_limit = new_limit * 60; // 转换为秒数存储 time_limit = new_limit * 60; // 转换为秒数存储
printf("计时器已开启,时间限制: %d 分钟\n", time_limit / 60); printf("计时器已开启,时间限制: %d 分钟\n", time_limit / 60);
} }
else else
{
printf("无效的时间限制!\n");
while (getchar() != '\n');
}
}
else
{ {
printf("计时器已关闭\n"); printf("计时器已关闭\n");
} }
} }
else
{
printf("输入格式错误!\n");
while (getchar() != '\n');
}
}
/** /**
* @brief 配置网络参数 * @brief 配置网络参数
@@ -238,46 +194,14 @@ void config_network()
printf("当前网络端口: %d\n", network_port); printf("当前网络端口: %d\n", network_port);
printf("当前网络超时: %d 毫秒\n", network_timeout); printf("当前网络超时: %d 毫秒\n", network_timeout);
printf("\n请输入新的网络端口 (%d-%d): ", MIN_NETWORK_PORT, MAX_NETWORK_PORT); int new_port = get_integer_input("请输入新的网络端口: ", MIN_NETWORK_PORT, MAX_NETWORK_PORT);
int new_port;
if (scanf("%d", &new_port) == 1)
{
if (new_port >= MIN_NETWORK_PORT && new_port <= MAX_NETWORK_PORT)
{
network_port = new_port; network_port = new_port;
printf("网络端口已设置为: %d\n", network_port); printf("网络端口已设置为: %d\n", network_port);
}
else
{
printf("无效的端口号!端口范围: %d-%d\n", MIN_NETWORK_PORT, MAX_NETWORK_PORT);
}
}
else
{
printf("输入格式错误!\n");
while (getchar() != '\n');
}
printf("\n请输入网络超时时间(毫秒, 建议1000-10000): "); int new_timeout = get_integer_input("请输入网络超时时间(毫秒, 建议1000-10000): ", 1000, 60000);
int new_timeout;
if (scanf("%d", &new_timeout) == 1)
{
if (new_timeout > 0)
{
network_timeout = new_timeout; network_timeout = new_timeout;
printf("网络超时已设置为: %d 毫秒\n", network_timeout); printf("网络超时已设置为: %d 毫秒\n", network_timeout);
} }
else
{
printf("无效的超时时间!\n");
}
}
else
{
printf("输入格式错误!\n");
while (getchar() != '\n');
}
}
/** /**
* @brief 配置管理主菜单 * @brief 配置管理主菜单
@@ -292,14 +216,7 @@ void config_management_menu()
display_settings_menu(); display_settings_menu();
display_current_config(); display_current_config();
printf("请选择操作: "); choice = get_integer_input("请选择操作(0-5): ", 0, 5);
if (scanf("%d", &choice) != 1)
{
printf("输入格式错误!\n");
while (getchar() != '\n');
pause_for_input("按任意键继续...");
continue;
}
switch (choice) switch (choice)
{ {
@@ -318,7 +235,7 @@ void config_management_menu()
case 5: case 5:
printf("AI难度设置功能开发中...\n"); printf("AI难度设置功能开发中...\n");
break; break;
case 6: case 0:
save_game_config(); save_game_config();
return; return;
default: default:
+1 -3
View File
@@ -289,10 +289,8 @@ void display_game_scores(int game_mode)
*/ */
void handle_save_record(int game_mode) void handle_save_record(int game_mode)
{ {
int save_choice = 0;
printf("===== 游戏结束 =====\n"); printf("===== 游戏结束 =====\n");
printf("是否保存游戏记录? (1-是, 0-否): "); int save_choice = get_integer_input("是否保存游戏记录? (1-是, 0-否): ", 0, 1);
scanf("%d", &save_choice);
if (save_choice == 1) if (save_choice == 1)
{ {
+1 -1
View File
@@ -123,7 +123,7 @@ void display_settings_menu()
printf("3. 计时器设置\n"); printf("3. 计时器设置\n");
printf("4. 网络配置设置\n"); printf("4. 网络配置设置\n");
printf("5. AI难度设置\n"); printf("5. AI难度设置\n");
printf("6. 返回主菜单\n"); printf("0. 返回主菜单\n");
printf("==================\n"); printf("==================\n");
} }