#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <direct.h>
#include <windows.h>
#define TOTAL_WORDS 300
#define SCORE_FILE "./high_score.txt"
#define WORD_FILE "./word_library.txt"
typedef struct
{
char name[20];
char difficulty[20];
int score;
} FishingRod;
typedef struct
{
char english[50];
char chinese[100];
int level;
} Word;
void initRods(FishingRod rods[])
{
strcpy(rods[0].name, "木制鱼竿");
strcpy(rods[0].difficulty, "初级鱼");
rods[0].score = 2;
strcpy(rods[1].name, "铁制鱼竿");
strcpy(rods[1].difficulty, "中级鱼");
rods[1].score = 4;
strcpy(rods[2].name, "金制鱼竿");
strcpy(rods[2].difficulty, "高级鱼");
rods[2].score = 6;
}
void showRodMenu(FishingRod rods[])
{
printf("\n=== 鱼竿选择 ===\n");
printf("1. %s - %s (%d分/题)\n", rods[0].name, rods[0].difficulty, rods[0].score);
printf("2. %s - %s (%d分/题)\n", rods[1].name, rods[1].difficulty, rods[1].score);
printf("3. %s - %s (%d分/题)\n", rods[2].name, rods[2].difficulty, rods[2].score);
printf("请选择鱼竿类型 (1-3): ");
}
int loadWordsFromFile(Word words[])
{
FILE *fp = fopen(WORD_FILE, "r");
if (!fp)
{
printf("无法打开单词库文件:%s\n", WORD_FILE);
return -1;
}
int count = 0;
while (count < TOTAL_WORDS)
{
int ret = fscanf(fp, "%49s %99s %d",
words[count].english,
words[count].chinese,
&words[count].level
);
if (ret == EOF || ret != 3)
break;
count++;
}
fclose(fp);
if (count == 0)
{
printf("单词文件为空或格式不正确!\n");
return -1;
}
printf("单词库加载成功,共 %d 个\n", count);
return count;
}
int loadHighScore()
{
FILE *fp = fopen(SCORE_FILE, "r");
int high = 0;
if (fp)
{
fscanf(fp, "%d", &high);
fclose(fp);
}
return high;
}
void saveHighScore(int score, int old)
{
if (score <= old) return;
FILE *fp = fopen(SCORE_FILE, "w");
if (!fp) return;
fprintf(fp, "%d", score);
fclose(fp);
printf("🎉刷新最高分:%d → %d\n", old, score);
}
void clearBuffer()
{
while (getchar() != '\n');
}
Word getRandomWord(Word words[], int level, int total)
{
Word pool[300];
int pc = 0;
for (int i = 0; i < total; i++)
if (words[i].level == level)
pool[pc++] = words[i];
if (pc == 0)
{
printf("此等级无单词,自动降级!\n");
return words[rand() % total];
}
return pool[rand() % pc];
}
// ========== 新增功能:添加单词到单词库 ==========
void addWordToLibrary()
{
FILE *fp = fopen(WORD_FILE, "a"); // 以追加模式打开文件
if (!fp)
{
printf("无法打开单词库文件:%s\n", WORD_FILE);
return;
}
printf("\n=== 添加单词到单词库 ===\n");
printf("格式:English Chinese \n难度等级(1-3)\n");
printf("示例:apple 苹果 1\n");
printf("输入'exit'返回主菜单\n\n");
char english[50], chinese[100];
int level;
while (1)
{
printf("请输入单词 \n(格式: English Chinese 等级): ");
// 读取英文单词
if (scanf("%49s", english) != 1)
{
clearBuffer();
continue;
}
// 检查退出命令
if (strcmp(english, "exit") == 0)
{
break;
}
// 读取中文和等级
if (scanf("%99s %d", chinese, &level) != 2)
{
clearBuffer();
printf("输入格式错误!请重试。\n");
continue;
}
clearBuffer(); // 清除输入缓冲区
// 验证等级范围
if (level < 1 || level > 3)
{
printf("难度等级必须在1-3之间!请重试。\n");
continue;
}
// 写入文件
fprintf(fp, "%s %s %d\n", english, chinese, level);
printf("✅ 添加成功: %s - %s (等级%d)\n", english, chinese, level);
// 询问是否继续
printf("是否继续添加?(y/n): ");
char choice = getchar();
clearBuffer();
if (choice != 'y' && choice != 'Y')
{
break;
}
}
fclose(fp);
printf("单词添加完成,返回主菜单。\n");
}
int main()
{
SetConsoleOutputCP(CP_UTF8);
SetConsoleCP(CP_UTF8);
srand(time(NULL));
FishingRod rods[3];
Word words[TOTAL_WORDS];
initRods(rods);
int totalLoaded = loadWordsFromFile(words);
if (totalLoaded < 0)
{
system("pause");
return 0;
}
int highScore = loadHighScore();
int totalScore = 0;
while (1)
{
printf("\n===== 单词钓鱼游戏 =====\n");
printf("当前得分: %d | 历史最高分: %d\n", totalScore, highScore);
printf("游戏模式:\n");
printf("1. 汉译英\n");
printf("2. 英译汉\n");
printf("3. 添加单词到单词库\n");
printf("4. 退出游戏\n");
printf("请选择模式 (1-4): ");
int mode;
if (scanf("%d", &mode) != 1)
{
clearBuffer();
printf("无效输入,请重新选择。\n");
continue;
}
clearBuffer();
if (mode == 4)
{
printf("游戏结束。最终得分: %d\n", totalScore);
saveHighScore(totalScore, highScore);
break;
}
if (mode == 3)
{
addWordToLibrary();
// 重新加载单词库以包含新单词
totalLoaded = loadWordsFromFile(words);
continue;
}
if (mode != 1 && mode != 2)
{
printf("无效选择,请重新输入。\n");
continue;
}
showRodMenu(rods);
int choice;
if (scanf("%d", &choice) != 1 || choice < 1 || choice > 3)
{
clearBuffer();
printf("无效选择,使用默认木制鱼竿。\n");
choice = 1;
}
clearBuffer();
Word w = getRandomWord(words, choice, totalLoaded);
if (mode == 1)
{
printf("\n【中文提示】:%s\n", w.chinese);
printf("请输入英文单词:");
}
else
{
printf("\n【英文提示】:%s\n", w.english);
printf("请输入中文翻译:");
}
char input[200];
int wrong = 0;
while (wrong < 3)
{
fgets(input, 200, stdin);
input[strcspn(input, "\n")] = 0;
if ((mode == 1 && strcmp(input, w.english) == 0) ||
(mode == 2 && strcmp(input, w.chinese) == 0))
{
totalScore += rods[choice - 1].score;
printf("正确!得分 +%d, 当前总分 %d\n",
rods[choice - 1].score, totalScore);
break;
}
else
{
wrong++;
if (wrong < 3)
printf("错误,还剩 %d 次机会:", 3 - wrong);
else
{
printf("连续三次错误,本轮结束!\n");
saveHighScore(totalScore, highScore);
highScore = loadHighScore(); // 更新最高分
totalScore = 0; // 重置当前分数
}
}
}
}
system("pause");
return 0;
}
如何给这个游戏添加图形运行界面,使他更逼真。用C语言
最新发布