C语言猜数字游戏是一个经典的编程练习项目,通过这个项目可以学习C语言的基本语法、控制结构、随机数生成和用户输入处理等核心概念。
游戏规则
- 程序随机生成一个1-100之间的数字
- 玩家有7次机会猜测这个数字
- 每次猜测后,程序会提示猜大了还是猜小了
- 在7次机会内猜中数字则获胜,否则游戏失败
基础版本猜数字游戏
#include
#include
#include
int main() {
int secretNumber, guess, attempts = 0;
const int maxAttempts = 7;
// 初始化随机数种子
srand(time(0));
// 生成1-100的随机数
secretNumber = rand() % 100 + 1;
printf("=== 猜数字游戏 ===\n");
printf("我已经想了一个1-100之间的数字,你有%d次机会猜中它!\n", maxAttempts);
while (attempts < maxAttempts) {
printf("\n请输入你的猜测 (还剩%d次机会): ", maxAttempts - attempts);
// 读取用户输入
if (scanf("%d", &guess) != 1) {
printf("输入无效,请输入一个整数!\n");
// 清空输入缓冲区
while (getchar() != '\n');
continue;
}
attempts++;
if (guess == secretNumber) {
printf("\n恭喜你!猜对了!\n");
printf("你用了%d次机会猜中了数字%d!\n", attempts, secretNumber);
break;
} else if (guess < secretNumber) {
printf("猜小了,再大一点!\n");
} else {
printf("猜大了,再小一点!\n");
}
// 提示范围
if (guess < secretNumber) {
printf("提示:数字在 %d 到 100 之间\n", guess);
} else {
printf("提示:数字在 1 到 %d 之间\n", guess);
}
}
if (attempts == maxAttempts && guess != secretNumber) {
printf("\n很遗憾,机会用完了!\n");
printf("正确答案是:%d\n", secretNumber);
}
printf("\n游戏结束,感谢游玩!\n");
return 0;
}
增强版本猜数字游戏
添加了难度选择、分数系统和游戏记录功能:
#include
#include
#include
#include
// 游戏难度枚举
typedef enum {
EASY = 1,
MEDIUM,
HARD
} Difficulty;
// 游戏结构体
typedef struct {
int secretNumber;
int maxNumber;
int maxAttempts;
int score;
Difficulty difficulty;
} Game;
// 函数声明
void initializeGame(Game *game, Difficulty diff);
void playGame(Game *game);
int getUserGuess();
void giveHint(int guess, int secret, int attempts);
int calculateScore(int attempts, int maxAttempts, Difficulty diff);
void showMenu();
int main() {
int choice;
Game game;
srand(time(0));
printf("=== 增强版猜数字游戏 ===\n");
while (1) {
showMenu();
printf("请选择操作: ");
scanf("%d", &choice);
switch (choice) {
case 1:
initializeGame(&game, EASY);
playGame(&game);
break;
case 2:
initializeGame(&game, MEDIUM);
playGame(&game);
break;
case 3:
initializeGame(&game, HARD);
playGame(&game);
break;
case 4:
printf("\n感谢游玩,再见!\n");
return 0;
default:
printf("无效选择,请重新输入!\n");
}
}
return 0;
}
// 初始化游戏设置
void initializeGame(Game *game, Difficulty diff) {
game->difficulty = diff;
switch (diff) {
case EASY:
game->maxNumber = 50;
game->maxAttempts = 10;
break;
case MEDIUM:
game->maxNumber = 100;
game->maxAttempts = 7;
break;
case HARD:
game->maxNumber = 200;
game->maxAttempts = 5;
break;
}
game->secretNumber = rand() % game->maxNumber + 1;
game->score = 0;
}
// 游戏主逻辑
void playGame(Game *game) {
int guess, attempts = 0;
char *diffName;
switch (game->difficulty) {
case EASY: diffName = "简单"; break;
case MEDIUM: diffName = "中等"; break;
case HARD: diffName = "困难"; break;
}
printf("\n%s模式游戏开始!\n", diffName);
printf("数字范围: 1 - %d\n", game->maxNumber);
printf("机会次数: %d\n", game->maxAttempts);
printf("====================\n");
while (attempts < game->maxAttempts) {
printf("\n第%d次猜测 (剩余%d次): ", attempts + 1, game->maxAttempts - attempts);
guess = getUserGuess();
if (guess == -1) continue; // 输入无效,重新输入
attempts++;
if (guess == game->secretNumber) {
printf("\n恭喜你!猜对了!\n");
game->score = calculateScore(attempts, game->maxAttempts, game->difficulty);
printf("得分: %d 分\n", game->score);
// 根据表现给出评价
if (attempts <= game->maxAttempts / 3) {
printf("太棒了!你是猜数字大师!\n");
} else if (attempts <= game->maxAttempts / 2) {
printf("表现不错!继续加油!\n");
} else {
printf("恭喜过关!还有提升空间!\n");
}
break;
}
giveHint(guess, game->secretNumber, attempts);
}
if (attempts == game->maxAttempts && guess != game->secretNumber) {
printf("\n很遗憾,机会用完了!\n");
printf("正确答案是: %d\n", game->secretNumber);
printf("得分: 0 分\n");
}
}
// 获取用户输入
int getUserGuess() {
int guess;
if (scanf("%d", &guess) != 1) {
printf("输入无效,请输入整数!\n");
while (getchar() != '\n'); // 清空输入缓冲区
return -1;
}
return guess;
}
// 提供提示
void giveHint(int guess, int secret, int attempts) {
int difference = abs(guess - secret);
if (guess < secret) {
printf("猜小了!");
} else {
printf("猜大了!");
}
// 根据接近程度给出不同提示
if (difference <= 5) {
printf(" 非常接近了!\n");
} else if (difference <= 15) {
printf(" 比较接近了!\n");
} else if (difference <= 30) {
printf(" 还有一定距离!\n");
} else {
printf(" 距离较远!\n");
}
// 特殊提示(每3次猜测提供一次额外提示)
if (attempts % 3 == 0) {
if (secret % 2 == 0) {
printf("提示:这个数字是偶数\n");
} else {
printf("提示:这个数字是奇数\n");
}
}
}
// 计算得分
int calculateScore(int attempts, int maxAttempts, Difficulty diff) {
int baseScore;
switch (diff) {
case EASY: baseScore = 100; break;
case MEDIUM: baseScore = 200; break;
case HARD: baseScore = 300; break;
}
// 根据剩余机会计算得分
int remaining = maxAttempts - attempts;
return baseScore + (remaining * 20);
}
// 显示菜单
void showMenu() {
printf("\n=== 游戏菜单 ===\n");
printf("1. 简单模式 (1-50, 10次机会)\n");
printf("2. 中等模式 (1-100, 7次机会)\n");
printf("3. 困难模式 (1-200, 5次机会)\n");
printf("4. 退出游戏\n");
}
面向对象风格版本(使用结构体和函数)
#include
#include
#include
#include
// 游戏状态枚举
typedef enum {
PLAYING,
WON,
LOST
} GameState;
// 游戏结构体
typedef struct {
int secretNumber;
int attempts;
int maxAttempts;
int minRange;
int maxRange;
GameState state;
} NumberGame;
// 游戏函数声明
void NumberGame_init(NumberGame *game, int min, int max, int maxAttempts);
bool NumberGame_makeGuess(NumberGame *game, int guess);
void NumberGame_displayHint(NumberGame *game, int guess);
void NumberGame_displayStatus(NumberGame *game);
bool NumberGame_isGameOver(NumberGame *game);
int main() {
NumberGame game;
int guess;
srand(time(0));
// 初始化游戏:范围1-100,最多7次机会
NumberGame_init(&game, 1, 100, 7);
printf("=== 面向对象风格猜数字游戏 ===\n");
printf("数字范围: %d - %d\n", game.minRange, game.maxRange);
printf("最多尝试次数: %d\n", game.maxAttempts);
printf("==============================\n");
while (!NumberGame_isGameOver(&game)) {
NumberGame_displayStatus(&game);
printf("请输入你的猜测: ");
if (scanf("%d", &guess) != 1) {
printf("输入无效,请重新输入!\n");
while (getchar() != '\n');
continue;
}
// 处理猜测
if (NumberGame_makeGuess(&game, guess)) {
printf("\n恭喜你猜对了!\n");
} else {
NumberGame_displayHint(&game, guess);
}
printf("\n");
}
// 显示最终结果
if (game.state == WON) {
printf("游戏胜利!你用了%d次机会猜中数字%d!\n",
game.attempts, game.secretNumber);
} else {
printf("游戏失败!正确答案是%d\n", game.secretNumber);
}
return 0;
}
// 初始化游戏
void NumberGame_init(NumberGame *game, int min, int max, int maxAttempts) {
game->minRange = min;
game->maxRange = max;
game->maxAttempts = maxAttempts;
game->attempts = 0;
game->state = PLAYING;
// 生成随机数
game->secretNumber = rand() % (max - min + 1) + min;
}
// 处理用户猜测
bool NumberGame_makeGuess(NumberGame *game, int guess) {
game->attempts++;
if (guess == game->secretNumber) {
game->state = WON;
return true;
}
if (game->attempts >= game->maxAttempts) {
game->state = LOST;
}
return false;
}
// 显示提示信息
void NumberGame_displayHint(NumberGame *game, int guess) {
if (guess < game->secretNumber) {
printf("猜小了!");
if (game->secretNumber - guess <= 10) {
printf(" (很接近!)");
}
} else {
printf("猜大了!");
if (guess - game->secretNumber <= 10) {
printf(" (很接近!)");
}
}
printf("\n");
}
// 显示游戏状态
void NumberGame_displayStatus(NumberGame *game) {
printf("--- 第%d次猜测 (剩余%d次) ---\n",
game->attempts + 1, game->maxAttempts - game->attempts);
}
// 检查游戏是否结束
bool NumberGame_isGameOver(NumberGame *game) {
return game->state != PLAYING;
}
C语言知识点总结
- 输入输出:printf、scanf函数的使用
- 控制结构:if-else、while循环、switch-case
- 随机数生成:rand()、srand()、time()函数
- 函数定义:函数的声明、定义和调用
- 结构体:自定义数据类型的使用
- 枚举类型:定义和使用枚举
- 头文件:stdio.h、stdlib.h、time.h的使用
编译和运行
# 使用GCC编译
gcc -o guess_number guess_number.c
# 运行程序
./guess_number
# 或者使用其他编译器
clang -o guess_number guess_number.c
# 在Windows上使用MinGW
gcc -o guess_number.exe guess_number.c
guess_number.exe
游戏运行示例
=== 猜数字游戏 ===
我已经想了一个1-100之间的数字,你有7次机会猜中它!
请输入你的猜测 (还剩7次机会): 50
猜大了,再小一点!
提示:数字在 1 到 50 之间
请输入你的猜测 (还剩6次机会): 25
猜小了,再大一点!
提示:数字在 25 到 50 之间
请输入你的猜测 (还剩5次机会): 37
猜小了,再大一点!
提示:数字在 37 到 50 之间
请输入你的猜测 (还剩4次机会): 43
猜小了,再大一点!
提示:数字在 43 到 50 之间
请输入你的猜测 (还剩3次机会): 47
恭喜你!猜对了!
你用了5次机会猜中了数字47!
游戏结束,感谢游玩!
扩展练习
- 添加游戏记录功能,保存最高分
- 实现多人游戏模式
- 添加图形界面(使用GTK或SDL)
- 实现网络对战功能
- 添加音效和动画效果
注意:这个猜数字游戏是学习C语言的绝佳练习项目,通过不断改进和扩展,可以深入理解C语言的各项特性。
提示: 这是一个重要的概念,需要特别注意理解和掌握。
注意: 这是一个常见的错误点,请避免犯同样的错误。