【游戏名称】猜数字大作战
【开发环境】Dev-C++ (我用的是5.11版本)
【代码行数】约200行
【难度选择】4种难度(简单/普通/困难/地狱)
【特色功能】排行榜记录 + 彩色界面 + 动态范围提示
最近闲着没事,用C++写了个猜数字小游戏。
游戏规则很简单: 系统随机生成一个数字,你在限定次数内猜出来, 每次猜完会提示“大了”还是“小了”, 猜对后根据剩余次数计算分数,还能记录到排行榜!
支持4种难度:
– 简单:1~100,15次机会
– 普通:100~500,13次机会
– 困难:500~2000,10次机会
– 地狱:1000~10000,8次机会
截图就不放了,Dev-C++直接编译运行就行。 代码贴在下面,有需要的自取:
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<string>
#include<vector>
#include<algorithm>
#include<conio.h>
#include<windows.h>
#include<fstream>
//版本高的可以直接加<bits/stdc++.h>
//不要忘了#include<conio.h>和#include<windows.h>
using namespace std;
struct Player{
string name;
int score;
int attempts;
};
vector<Player> ranking;
void setColor(int color){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
}
void saveRanking(){
ofstream file("ranking.txt");
if(!file.is_open()) return;
file<<ranking.size()<<"\n";
for(int i=0;i<ranking.size();i++){
file<<ranking[i].name<<"\n";
file<<ranking[i].score<<"\n";
file<<ranking[i].attempts<<"\n";
}
file.close();
}
void loadRanking(){
ranking.clear();
ifstream file("ranking.txt");
if(!file.is_open()) return;
int n;
file>>n;
for(int i=0;i<n;i++){
Player p;
file>>p.name;
file>>p.score;
file>>p.attempts;
ranking.push_back(p);
}
file.close();
}
void addRanking(Player p){
for(int i=0;i<ranking.size();i++){
if(ranking[i].name==p.name){
if(p.score>ranking[i].score){
ranking[i].score=p.score;
ranking[i].attempts=p.attempts;
}
saveRanking();
return;
}
}
ranking.push_back(p);
saveRanking();
}
void printTitle(){
system("cls");
setColor(14);
cout<<"==================================================\n";
cout<<"| |\n";
cout<<"| ★★★ 猜 数 字 大 作 战 ★★★ |\n";
cout<<"| |\n";
cout<<"==================================================\n";
setColor(7);
cout<<"\n";
}
void printMenu(){
printTitle();
setColor(11);
cout<<" ┌──────────────────────┐\n";
cout<<" │ 1. 开始游戏 │\n";
cout<<" │ 2. 查看排行榜 │\n";
cout<<" │ 3. 游戏说明 │\n";
cout<<" │ 4. 退出游戏 │\n";
cout<<" └──────────────────────┘\n";
setColor(7);
cout<<"\n 请输入选项(1-4):";
}
void printLoading(){
system("cls");
setColor(10);
cout<<"\n\n\n\n\n\n";
cout<<" ";
for(int i=0;i<30;i++){
cout<<"█";
Sleep(30);
}
cout<<"\n\n";
cout<<" 加载中,请稍候...\n";
Sleep(500);
system("cls");
}
void printRule(){
printTitle();
setColor(15);
cout<<"\n";
cout<<" ============== 游 戏 说 明 ==============\n\n";
setColor(7);
cout<<" 目标:猜出系统随机生成的数字\n\n";
cout<<" 规则:\n";
cout<<" 1. 系统会生成一个指定范围内的整数\n";
cout<<" 2. 你有若干次机会猜出这个数字\n";
cout<<" 3. 每次猜完会提示「大了」或「小了」\n";
cout<<" 4. 猜对后根据剩余次数计算分数\n";
cout<<" 5. 分数会记录到排行榜\n\n";
cout<<" 计分规则:剩余次数 x 10 = 得分\n\n";
cout<<" 提示:数字范围会随着猜测次数缩小!\n\n";
setColor(11);
cout<<" 按任意键返回主菜单...";
getch();
}
void showRanking(){
printTitle();
setColor(15);
cout<<"\n";
cout<<" ============== 荣 誉 榜 ==============\n\n";
setColor(7);
if(ranking.empty()){
setColor(12);
cout<<"\n\n 暂无记录,快来挑战吧!\n\n";
setColor(7);
}else{
setColor(14);
cout<<" 排名 玩家姓名 得分 所用次数\n";
cout<<" -------------------------------------\n";
setColor(7);
for(int i=0;i<ranking.size()&&i<10;i++){
for(int j=i+1;j<ranking.size();j++){
if(ranking[j].score>ranking[i].score){
Player temp=ranking[i];
ranking[i]=ranking[j];
ranking[j]=temp;
}
}
setColor(i==0?14:i==1?11:i==2?10:7);
cout<<" "<<i+1<<" ";
cout<<ranking[i].name<<" ";
cout<<ranking[i].score<<" ";
cout<<ranking[i].attempts<<"\n";
}
}
setColor(7);
cout<<"\n\n 按任意键返回主菜单...";
getch();
}
int chooseLevel(){
system("cls");
printTitle();
setColor(15);
cout<<"\n";
cout<<" ============== 选 择 难 度 ==============\n\n";
setColor(7);
cout<<" 1. 简单 (1~100, 15次机会)\n";
cout<<" 2. 普通 (100~500, 13次机会)\n";
cout<<" 3. 困难 (500~2000, 10次机会)\n";
cout<<" 4. 地狱 (1000~10000,8次机会)\n\n";
setColor(11);
cout<<" 请选择难度(1-4):";
int level;
cin>>level;
return level;
}
int game(int level){
system("cls");
printTitle();
int minNum=1,maxNum=100;
int maxAttempts=15;
if(level==2){
minNum=100;maxNum=500;maxAttempts=13;
}else if(level==3){
minNum=500;maxNum=2000;maxAttempts=10;
}else if(level==4){
minNum=1000;maxNum=10000;maxAttempts=8;
}
srand(time(0));
int ans=rand()%(maxNum-minNum+1)+minNum;
int guess,cnt=0;
int low=minNum,high=maxNum;
setColor(11);
cout<<"\n ==============================================\n";
cout<<" 游 戏 开 始 !\n";
cout<<" ==============================================\n";
setColor(7);
cout<<"\n 数字范围:"<<minNum<<" ~ "<<maxNum<<"\n";
cout<<" 剩余次数:"<<maxAttempts<<" 次\n\n";
while(cnt<maxAttempts){
setColor(14);
cout<<" ----------------------------------------------\n";
setColor(7);
cout<<" 第 "<<cnt+1<<" 次猜 | 范围 ["<<low<<" ~ "<<high<<"] 请输入数字:";
cin>>guess;
if(guess<low||guess>high){
setColor(12);
cout<<" 请输入 "<<low<<"~"<<high<<" 之间的数字!\n\n";
setColor(7);
continue;
}
cnt++;
if(guess==ans){
setColor(10);
cout<<"\n ==============================================\n";
cout<<" 🎉🎉🎉 恭喜你猜对了!🎉🎉🎉\n";
cout<<" ==============================================\n";
int score=(maxAttempts-cnt+1)*10;
cout<<"\n 答案就是 "<<ans<<"\n";
cout<<" 你用了 "<<cnt<<" 次机会\n";
cout<<" 得分:"<<score<<"\n\n";
setColor(7);
cout<<" 请输入你的名字(不超过10个字符):";
string name;
cin>>name;
Player p;
p.name=name;
p.score=score;
p.attempts=cnt;
addRanking(p);
setColor(11);
cout<<"\n 成绩已记录到排行榜!\n";
setColor(7);
cout<<"\n 按任意键返回主菜单...";
getch();
return score;
}else if(guess>ans){
setColor(12);
cout<<" 大了!再小一点\n";
setColor(7);
high=guess-1;
}else{
setColor(10);
cout<<" 小了!再大一点\n";
setColor(7);
low=guess+1;
}
cout<<"\n";
if(cnt==maxAttempts){
setColor(12);
cout<<"\n ==============================================\n";
cout<<" 😢 很遗憾,机会用完了!\n";
cout<<" ==============================================\n";
cout<<"\n 正确答案是:"<<ans<<"\n\n";
setColor(7);
cout<<" 按任意键返回主菜单...";
getch();
return 0;
}
}
return 0;
}
int main(){
system("title 猜数字大作战");
system("color 0A");
loadRanking();
while(true){
printMenu();
int choice;
cin>>choice;
if(choice==1){
int level=chooseLevel();
game(level);
}else if(choice==2){
showRanking();
}else if(choice==3){
printRule();
}else if(choice==4){
saveRanking();
system("cls");
setColor(12);
cout<<"\n\n\n\n\n\n\n";
cout<<" ==============================\n";
cout<<" 感谢游玩,再见!\n";
cout<<" ==============================\n";
setColor(7);
Sleep(1000);
exit(0);
}else{
setColor(12);
cout<<" 无效选项,请重新输入!\n";
setColor(7);
Sleep(500);
}
}
return 0;
}
欢迎提建议,也欢迎魔改! 觉得好玩的话评个分和关注再走~



![表情[xiaoku]-ZDZL](https://bbs.zdzltop.com/wp-content/themes/zibll/img/smilies/xiaoku.gif)

