#include <iostream> #include <fstream> #include <time.h> #define LOSETIMES 10 using namespace std; typedef struct gamerecorddata { char username[20]; int times; }recorddata; // 储存用户记录的结构体 void Helper(); //帮助 int StartGame(); //进行游戏 class GameRecord //用来记录数据的类 { public: GameRecord(); //初始化排行榜 ~GameRecord(); //关闭程序时写入排行榜 int Check( int times ); //检查是否能加入排行榜 int Insert( char * name, int times ); //插入排行榜 void FreshRecord(); //刷新排行榜将数据写入文件 void Display(); //显示排行榜 void Clear(); //清空排行榜 private: recorddata data[11]; //排行榜的10个记录 }; class RNumber //产生随机数的类 { public: RNumber(); //初始化,产生符合要求的随机数 int Check(char * number); //检查输入数字的匹配程度 void Display(); //输出产生的随机数 private: char RanNum[5]; //存放产生的随机数 }; int main() { int times,flag; //记录猜解次数和入榜标志 char name[20],choice; //存放用户名和选择字符 GameRecord Record; //创建记录对象 while(1) { system("cls"); cout<<"------------------------------------"<<endl; cout<<"1 进行游戏"<<endl; cout<<"2 排行榜"<<endl; cout<<"3 清空排行榜"<<endl; cout<<"4 显示帮助"<<endl; cout<<"5 退出游戏"<<endl; cout<<"------------------------------------"<<endl; cout<<"请输入 1~5: "; cin>>choice; switch(choice) { case'1': times=StartGame(); if(times) { flag=Record.Check(times); //判断是入榜 if(flag) { cout<<endl<<"恭喜你进入排行榜!请输入你的昵称:"<<endl; cin>>name; Record.Insert( name, times); Record.Display(); } else { cout<<endl<<"无法进入排行榜!请再接再厉!"<<endl; Record.Display(); } } break; case'2': Record.Display(); break; case'3': Record.Clear(); break; case'4': Helper(); break; case'5': return 0; } } } int StartGame() { RNumber number; char num[5]; int i, flag=0; for( i=1; i<=10; i++ ) { cout<<"第"<<i<<"次猜数:"; cin>>num; flag=number.Check( num ); //检查是否猜解正确 if(flag) { cout<<endl<<"猜对了!用了"<<i<<"次"<<endl; cout<<"正确答案是 "; number.Display(); return i; } } cout<<"你失败了,正确答案是:"; number.Display(); system("pause"); return 0; } RNumber::RNumber() { int i, j, flag; strcpy(RanNum,"0000"); srand( (unsigned)time( NULL ) ); for( i=0; i<4; i++ ) { RanNum[i]=rand()%10+'0'; //产生0~9的随机字符 flag=0; if(RanNum[0]=='0') flag=1; //防止最高位为0 for( j=0; j<i; j++ ) if( RanNum[i] == RanNum[j] ) flag=1; //标记重复 if( flag ) i--; //如果重复则重新产生一个字符 } } int RNumber::Check( char * num ) { int i, j; char result[5]="0A0B"; for( i=0; i<4; i++ ) { for( j=0; j<4; j++ ) { if( num[j] == RanNum[i] ) result[2]++; //记录数字相符的个数 } if( num[i] == RanNum[i] ) result[0]++; //记录数字相符且位置正确的个数 } if(result[0]=='4'&&result[2]=='4') { cout<<"猜数结果 "<<result<<endl<<endl; return 1; } else { cout<<"猜数结果 "<<result<<endl<<endl; cout<<RanNum<<endl; return 0; } } void RNumber::Display() { cout<<RanNum<<endl; } GameRecord::GameRecord() { int i; recorddata defaultdata={"noname",LOSETIMES}; fstream datafile; fstream check("record.dat",ios::in|ios::binary); if(!check) //如果文件不存在,则初始化排行榜 { for(i=0;i<11;i++) { strcpy(data[i].username,"noname"); data[i].times=LOSETIMES; } } else //文件存在,读入到排行榜 { check.read((char *)data,11*sizeof(recorddata)); check.close(); } } GameRecord::~GameRecord() //结束时,写入排行榜 { FreshRecord(); } void GameRecord::FreshRecord() //刷新,写入排行榜 { ofstream datafile; datafile.open("record.dat",ios::out|ios::binary); datafile.write((char *)data,11*sizeof(recorddata)); datafile.close(); } int GameRecord::Check(int times) //检查是否入榜 { if(times<=data[9].times) return 1; else return 0; } int GameRecord::Insert(char * name,int times)//插入排行榜 { int i=9; while(i>=0&×<=data[i].times) { data[i+1].times=data[i].times; strcpy(data[i+1].username,data[i].username); i--; } data[i+1].times=times; strcpy(data[i+1].username,name); FreshRecord(); return 1; } void GameRecord::Clear() //清空排行榜 { int i; for(i=0;i<11;i++) { strcpy(data[i].username,"noname"); data[i].times=LOSETIMES; } cout<<"清空成功!"<<endl; system("pause"); } void GameRecord::Display() //显示排行榜 { int i; cout<<endl<<"排行榜"<<endl; for(i=0;i<10;i++) { cout<<i+1<<"/t"<<data[i].username<<"/t"<<data[i].times<<endl; } system("pause"); } void Helper() //提示 { cout<<endl; cout<<" 游戏提示:用如1A2B的形式提示玩家当前猜数字的匹配情况,"; cout<<"例如:1A表示玩家猜测的数字中有1个是数值与系统产生数字相符合,"; cout<<"并且位置也完全符合,2B表示玩家猜测数字中有2个与系统产生的原始数字相符,"; cout<<"但位置不符。用这种方式提示用户对自己猜测进行调整。"<<endl; cout<<"一共有"<<LOSETIMES<<"猜解机会"<<endl; cout<<endl; system("pause"); }