#include<iostream.h>
#include<stdlib.h>
#include<time.h>
#include<fstream.h>
#include<conio.h>
#include<windows.h>
#include<string.h>
#include<iomanip.h>
//开始值 默认值
int yi=1;//高级菜单 停顿
int y=1;//记录菜单 停顿
int tf=1;//游戏说明 停顿
int qc=1;//不用游戏助手
int js=1;//不计时
int cc=0;//未提示存储
int data[10];//存10个阿拉伯数字
int ar[10][4];//记录各次机会的猜测,对应a[4]
int cr1[10],cr2[10];//对应count1,count2,
int cb[10][4];//对应c[4]
char j[2][5]={"普通","计时"};//游戏状态
char q[2][5]={"独立","助手"};
char pass[7];//密码
void pword()//读取密码
{
ifstream in("f://mima.txt");
in.getline(pass,255);
}
void intro()//说明
{
cout.flush();
cout<<"/n ******************************************************************"<<endl;
if(tf) Sleep(400);
cout<<" ** 猜数游戏 **"<<endl;
if(tf) Sleep(400);
cout<<" ** 1.用户从键盘输入4位不重复的数,来匹配计算机给出的4位随 **"<<endl;
if(tf) Sleep(400);
cout<<" ** 机数,若数字和位数均等同,表示用户赢了。 **"<<endl;
if(tf) Sleep(400);
cout<<" ** 2.每猜一次,计算机均给出提示信息(x,y),x表示数字、位置 **"<<endl;
if(tf) Sleep(400);
cout<<" ** 都匹配的个数,y表示数字匹配但位置不匹配的个数。 **"<<endl;
if(tf) Sleep(400);
cout<<" ** 3.积分规则:每猜错一次扣10分,若猜对1个数,奖励50分。 **"<<endl;
if(tf) Sleep(400);
cout<<" ** 4.每次游戏共9次机会。 **"<<endl;
if(tf) Sleep(400);
cout<<" ** 5.高级——游戏助手,可以标记猜中的数和数字匹配但位置不 **"<<endl;
if(tf) Sleep(400);
cout<<" ** 匹配的数,并列出10个阿拉伯数字中你未用过的数。 **"<<endl;
if(tf) Sleep(400);
cout<<" ** 6.高级——计时游戏,会计算时间,采用游戏助手时,参考用 **"<<endl;
if(tf) Sleep(400);
cout<<" ** 时100 s,但游戏时间不作为计分指标。 **"<<endl;
if(tf) Sleep(400);
cout<<" ******************************************************************"<<endl;
tf=0;
}
void start()//开始打印
{
cout<<endl;
cout<<"* * * 数字竞猜游戏 * * *"<<endl;
cout<<endl;
cout<<" 退出(E) 积分(S) 帮助(H) 高级(M)"<<endl;
cout<<" 新游戏(C) 界面更新(X) 查看答案(K) 存储|记录(R)"<<endl;
cout<<" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
cout<<"/n 请输入4位不重复的数或菜单选项"<<endl;
cout<<"/n 游戏状态:"<<j[js]<<" "<<q[qc]<<endl;
}
int key() //读按键的代码
{
cout.flush();
int ch1,ch2;
ch1=getch();
if(ch1==0)
{
ch2=getch();
ch2=ch2<<8;
return(ch2);
}
else
return(ch1);
}
void Initia(int *a) //随机产生四位四位不同的数
{
int m,n,i,j;//个十百千
srand( (unsigned)time(NULL));
m=rand()%10;
n=rand()%10;
while (m==n )
{
m = rand()%10;
}
i = rand()%10;
while ((m==i)||(n==i))
{
i = rand()%10;
}
j = rand()%9+1;
while ((j==m)||(j==n)||(j==i))
{
j=rand()%9+1;
}
a[0] = j;
a[1] = i;
a[2] = n;
a[3] = m;
}
int nCompare(int a[4],int b[4],int &count1,int &count2,int c[4])
/*比较
count2是数相同、位置不同的个数,count1数和位置都相同的个数,通过参数返回
c[4]记录对应位数 */
{
count1 = 0,count2 = 0;
c[0]=c[1]=c[2]=c[3]=0;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(a[i]==b[j] && i==j)
{
count1++;
c[j]=1;//在数字、位置都对的位置上记1
}
if(a[i]==b[j] && i!=j)
{
count2++;
c[j]=2;//在数字对、位置不对的位置上记2
}
}
}
return count1;//返回位置和数都对的个数
}
void help(int count1,int count2,int c[4])//帮助
{
if(count1==0)
cout<<"/n 数字、位置都正确的个数为0"<<endl;
else
{
cout<<"/n 数字、位置都正确的有:";
for(int i=0;i<4;i++)
{
if(c[i]==1) cout<<" 第"<<i+1<<"位 ";
}
cout<<endl;
}
if(count2==0)
cout<<"/n 数字正确、位置不正确的个数为0"<<endl;
else
{
cout<<"/n 数字正确、位置不正确的有:";
for(int i=0;i<4;i++)
{
if(c[i]==2) cout<<" 第"<<i+1<<"位 ";
}
cout<<endl;
}
cout<<endl;
}
void shumi(char mm[7])//输密码
{
for(int i=0;i<6;i++)
{
mm[i]=key();
cout<<"*";
}
mm[6]='/0';
cout<<endl;
}
int code(int a[4])//密码
{
int lefttime=3;
int kh;
char aa[7];
while(lefttime)
{
cout<<"/n 请输入密码(6位):";
shumi(aa);
lefttime--;
if(strcmp(pass,aa)==0)
{
cout<<"/n 密码输入正确"<<endl;
cout<<"/n 答案为:"<<a[0]<<a[1]<<a[2]<<a[3]<<endl;
cout<<"/n 你已放弃游戏"<<endl;
kh=1;
break;
}
else
{
cout<<"/n 密码输入错误。"<<endl;
cout<<"/n 继续输密码请按Enter键,放弃输入请按其他键....."<<endl;
int con;
con=key();
if(con!=13)
{
return 0;
break;
}
}
}
if(lefttime==0 && strcmp(pass,aa)!=0)
{
cout<<"/n 对不起,你只有三次机会。"<<endl;
kh=0;
}
return kh;
}
int hang()//取文本中最后一行的游戏次数
{
ifstream in("f://猜数游戏记录.txt");//
char str[80];
char st[50000]="";
while(!in.eof())
{
in.getline(str,80);
strcat(st,str);
}
int n=strlen(st);
char aa[5]={st[n-42],st[n-41],st[n-40],st[n-39]};
return atoi(aa);
}
void disp()//浏览记录
{
if(hang()==0)
cout<<"/n 记录为空/n";
else
{
ifstream in("f://猜数游戏记录.txt");//
char str[255];
while(!in.eof())
{
in.getline(str,255);
cout<<" "<<str<<endl;
}
}
}
int kong()//判断文件是否为空
{
ifstream in("f://猜数游戏记录.txt");
char str[80];
in.getline(str,80);
if(strlen(str)==0)
return 1;
else
return 0;
}
int store(int s,float ti)//存记录
{
cc=1;//已提示存储
cout<<"/n 是否要存储游戏记录? "<<endl;
cout<<"/n 不存储(N) 存储(其他键) "<<endl;
int yn=key();
if(yn==110)
{
cout<<"/n 不存储/n";
return 0;//存储不成功
}
int nn=hang();
nn++;
char name[8]="段小龙 ";
cout<<"/n 保存分值..."<<endl;
cout<<"/n 默认姓名是“段小龙”,更替姓名请按F6键,跳过请按其他键...."<<endl;
int c=key();
if(c==16384)
{
cout<<"/n 请输入姓名:";
cin>>name;
}
ofstream outfile("f://猜数游戏记录.txt",ios::app);//分数存盘
if(kong())
{
outfile<<setw(25)<<"游戏记录"<<endl<<endl;
outfile<<setw(4)<<" "<<setw(10)<<"姓名"<<setw(6)<<"分数"<<setw(10)<<"时间"<<setw(12)<<"游戏方式"<<endl;
}
outfile<<setw(4)<<nn<<setw(10)<<name<<setw(6)<<s<<setw(10)<<ti<<setw(7)<<j[js]<<setw(5)<<q[qc]<<endl;
return 1;//存储成功
}
void dele()//删记录
{
if(hang()==0)
cout<<"/n 记录已为空"<<endl;
else
{
ofstream shan("f://猜数游戏记录.txt",ios::out|ios::trunc);
shan<<"";//用/0字符取代
cout<<"/n ";
for (int k=1;k<=20;k++)
{
cout<<"▌";
cout.flush();
Sleep(250-10*k);
}
cout<<endl;
cout<<"/n 已删除.."<<endl;
}
}
void gaoji()//高级
{
cout.flush();
cout<<" ╭———————————╮"<<endl;
cout<<" |高级▼|修改密码(F7) |"<<endl;
cout<<" ╰———|———————|"<<endl;
if(yi)
Sleep(400);
cout<<" |计时游戏(F8) |"<<endl;
if(yi)
Sleep(400);
cout<<" |———————|"<<endl;
cout<<" |游戏说明(F9) |"<<endl;
if(yi)
Sleep(400);
cout<<" |———————|"<<endl;
cout<<" |游戏助手(F10) |"<<endl;
if(yi)
Sleep(400);
cout<<" |———————|"<<endl;
cout<<" | 返回(Enter) |"<<endl;
cout<<" ╰———————╯"<<endl;
yi=0;
}
void change()//修改密码
{
cout<<"/n 修改密码 "<<endl;
char jiu[7];
cout<<"/n 请输入旧密码:";
shumi(jiu);
for(int i=0;i<3 && (strcmp(jiu,pass)!=0);i++)
{
cout<<"/n 密码错误请重新输入:";
shumi(jiu);
cout<<endl;
}
if(i==3)
{
cout<<"/n 对不起,你只有三次机会"<<endl;
return;
}
char xin[7],ne[7];
while(1)
{
cout<<"/n 请输入新密码:";
shumi(xin);
cout<<"/n 请再次输入新密码:";
shumi(ne);
if(strcmp(xin,ne)!=0)
cout<<"/n 两次输入不相同,请重新输入..."<<endl;
else
break;
}
strcpy(pass,ne);
cout<<"/n 密码修改成功。"<<endl;
ofstream outfile("f://mima.txt");
outfile<<pass;
}
void jilu()//游戏记录
{
cout.flush();
cout<<" ╭————————————╮"<<endl;
cout<<" |游戏记录▼|存储记录(E) |"<<endl;
cout<<" ╰—————|——————|"<<endl;
if(y)
Sleep(400);
cout<<" |浏览记录(S) |"<<endl;
if(y)
Sleep(400);
cout<<" |——————|"<<endl;
cout<<" |删除记录(D) |"<<endl;
if(y)
Sleep(400);
cout<<" |——————|"<<endl;
cout<<" |返回(Enter) |"<<endl;
cout<<" ╰——————╯"<<endl;
y=0;
}
void fun(int r)//print各次机会情况,及游戏助手
{
cout.flush();
system("cls");
start();
if(r==0)
return;
if(qc==1)
{
cout<<" ";
for(int k=0;k<4;k++)
{
if(cb[r][k]==1)
cout<<" "<<ar[r][k];
else
cout<<" ";
}
cout<<" ";
for(k=0;k<4;k++)
{
if(cb[r][k]==2)
cout<<ar[r][k]<<" ? ",ar[r][k];
}
cout<<"/n ~ ~ ~ ~ ";
for(k=0;k<10;k++)
{
if(data[k]==-1)
cout<<" ";
else
cout<<" "<<data[k];
}
}
cout<<"/n/n 你的猜测 提示信息"<<endl;
for(int i=1;i<=r;i++)
{
cout<<" "<<i<<" "<<ar[i][0]<<setw(2)<<ar[i][1]<<setw(2)<<ar[i][2]<<setw(2)<<ar[i][3]
<<" "<<"("<<cr1[i]<<","<<cr2[i]<<")"<<endl;
char ch[4];
for(int k=0;k<4;k++)
{
if(cb[i][k]==1) ch[k]='^';
else if(cb[i][k]==2) ch[k]='?';
else ch[k]=' ';
}
if(qc==0)
cout<<" - - - "<<endl;
else
cout<<" "<<setw(2)<<ch[0]<<setw(2)<<ch[1]<<setw(2)<<ch[2]<<setw(2)<<ch[3]<<endl;
}
}
void game()//游戏主题
{
cout.flush();
system("cls");
start();
cout<<"/n 游戏开始..."<<endl;
for(int dd=0;dd<10;dd++)
{
data[dd]=dd;//赋上各数字
}
int a[4]={0};//存随机产生的四位不同的数
int b[4]={0};//存用户输入的四位数
Initia(a); //随机产生四位不同的数
int k=0;//第几次猜
int s=0;//分数
float ti=0;//时间
time_t ts=0,te=0;
int t=1;//控制后面计时的外重循环
int m=0;
int n=0;
cc=0;//未提示存储
int ff=0;//存储成功否
if(js)//若计时,象征性给出秒表
{
cout<<"/n 进入后开始计时"<<endl;
system("pause");
cout<<"/n 开始计时:";
ts=time(NULL);
for( m=0;m<120 && t;m++)
{
for(n=0;n<=9;n++)
{
cout<<setw(2)<<m<<":"<<n;
cout.flush();
Sleep(100);
cout<<"/b/b/b/b";
if(kbhit())
{
system("cls");
start();
t=0;//退出外重循环
break;
}
}
}
}
for(int i=8;i>=0;i--)
{
int shi;//存按键的码值
int tu=1;//控制能否继续本次游戏
while(1)//输入循环
{
char str[8];
cout<<"/n 请输入 :";
cin>>str;//先提取字符串
int number=atoi(str);//转换为数字
int d=number/100;
int cw=number%100;
int b[4]={0};//存用户输入的四位数
b[3]=cw%10;
b[2] =cw/10;
b[1]=d%10;
b[0]=d/10;
int count1,count2;
int c[4];//记录数字的位数
int r;//猜对的数字个数
while(number!=0 && tu==0 )//不能继续游戏却输入了数字
{
cout<<"/n **********************************************"<<endl;
cout<<" ** 你已知答案或用完9次机会,或已存储 , **"<<endl;
cout<<" ** 不能再继续本次游戏。 **"<<endl;
cout<<" **********************************************"<<endl;
cout<<"/n 新游戏(C) 跳过(其他键)"<<endl;
shi=key();
if(shi==99)
{
strcpy(str,"c ");
}
else
{
cout<<"/n 请输入菜单选项:";
cin>>str;
}
number=atoi(str);
}
int rec=1;//控制记录子菜单的循环
if(number==0)//进入菜单
{
switch(str[0])
{
case 'm'://高级
case 'M':
while(rec)//子菜单循环
{
cout.flush();
system("cls");
gaoji();
cout<<"/n 请按键..."<<endl;
shi=key();
while( shi!=16640 && shi!=16896 && shi!=17152 && shi!=17408 && shi!=13)//按键错误
{
cout<<"/n 按键错误,请重新按键..."<<endl;
shi=key();
}
switch(shi)
{
case 16896://普通游戏还是计时游戏
if(k>0 && cc==0)
{
cout<<"/n 你已游戏"<<endl;
ff=store(s,ti);
if(ff) tu=0;//不能继续本次游戏
}
cout<<"/n 更改(Y) 退出(其他键)"<<endl;
cout<<"/n 当前状态:";
while(1)
{
if(js==0)
cout<<"普通游戏";
else
cout<<"计时游戏";
shi=key();
if(shi==121)
{
js=(!js);
cout<<"/b/b/b/b/b/b/b/b";
}
else
break;
}
break;
case 17152://游戏说明
cout.flush();
system("cls");
intro();
system("pause");
break;
case 16640://修改密码
cout.flush();
system("cls");
change();
cout.flush();
system("pause");
break;
case 17408://游戏助手
if(k>0 && cc==0)
{
cout<<"/n 你已游戏"<<endl;
ff=store(s,ti);
if(ff) tu=0;//不能继续本次游戏
}
cout<<"/n 更改(Y) 退出(其他键)/n";
cout<<"/n 当前状态:";
while(1)
{
if(qc==1)
cout<<"助手";
else
cout<<"独立";
shi=key();
if(shi==121)
{
qc=(!qc);
cout<<"/b/b/b/b";
}
else
break;
}
break;
case 13://返回
rec=0;
fun(k);
break;
}
}
break;
case 'C'://新游戏
case 'c':
if(k>0 && cc==0)
{
cout<<"/n 你已游戏"<<endl;
store(s,ti);
}
game();
break;
case 'X'://更新界面
case 'x':
fun(k);
break;
case 'e'://退出
case 'E':
if(k>0 && !cc)
{
cout<<"/n 你已游戏"<<endl;
store(s,ti);//存储
}
cout.flush();
system("cls");
cout<<"/n/n";
cout<<" 谢谢使用!/n";
exit(0);
break;
case 's'://积分
case 'S':
cout<<"/n 积分为:"<<s<<endl;
cout.flush();
system("pause");
fun(k);
break;
case 'h'://帮助
case 'H':
help(count1,count2,c);
system("pause");
fun(k);
break;
case 'k'://查看答案
case 'K':
if(code(a))//密码正确
{
if(k>0 && !cc)
{
ff=store(s,ti);
}
tu=0;//不能继续本次游戏
}
system("pause");
fun(k);
break;
case 'r'://记录
case 'R':
while(rec)
{
cout.flush();
system("cls");
jilu();
cout<<"/n 请按键... /n";
shi=key();
while( shi!=100 && shi!=101 && shi!=115 && shi!=13)
{
cout<<"/n 按键错误,请重新按键.../n";
shi=key();
}
switch(shi)
{
case 100://删除
dele();
system("pause");
break;
case 101://存储
if(k==0)
{
cout<<"/n 还未进入游戏,建议不要存储/n";
}
ff=store(s,ti);
if(ff)//存储成功
tu=0;//不能继续本次游戏
system("pause");
break;
case 115://浏览
disp();
cout.flush();
system("pause");
break;
case 13://返回
rec=0;
fun(k);
break;
}
}
break;
default:
cout<<"/n 输入错误/n";
system("pause");
fun(k);
}
}
else if((number>9999)||(number<1000)||(b[0]==b[1])||(b[0]==b[2])||(b[0]==b[3])
||(b[1]==b[2])||(b[1]==b[3])||(b[2]==b[3]))//输入错误
{
cout<<"/n ************************************************/n";
cout<<" ** 输入错误,请重新输入。 **/n";
cout<<" ** 错误提示:不是4位数或有重复的数等 。 **/n";
cout<<" ************************************************/n";
}
else
{
k=9-i;//第几次机会
r=nCompare(a,b,count1,count2,c);//比较
s=50*count1-10*k;
cr1[k]=count1;
cr2[k]=count2;
for(int cn=0;cn<4;cn++)
{
ar[k][cn]=b[cn];
cb[k][cn]=c[cn];
}
for( cn=0;cn<4;cn++)
{
for(int j=0;j<11;j++)
{
if(b[cn]==data[j])
data[j]=-1;
}
}
if(4==r) //答对
{
s+=10;//开始多减了10分
cout<<"/n ************************************************/n";
cout<<" ** ^_^恭喜您,您赢了!答案就是"<<a[0]<<a[1]<<a[2]<<a[3]<<" **/n";
if(js==1)
{
te=time(NULL);
ti=float(te-ts+m+n/10.0);
cout<<" ** 用时:"<<setw(3)<<ti
<<"s"<<" **/n";
}
cout<<" ************************************************/n";
tu=0;//不能继续本次游戏
cout<<"/n 新游戏(C) 跳过(其他键)"<<endl;
shi=key();
if(shi==99)
{
if(k>0 && !cc)
{
ff=store(s,ti);
}
game();
break;
}
fun(k);
}
else if(0==i)//机会用尽
{
cout<<"/n **************************************/n";
cout<<" *** 9次机会用完,游戏结束! ***/n";
cout<<" **************************************/n";
tu=0;//不能继续本次游戏
system("pause");
fun(k);
}
else
{
fun(k);
break;
}
}
}
}
}
void main()
{
pword();
intro();
cout<<"/n 按任意键继续......"<<endl;
cout<<"/n 10秒钟后自动进入游戏加载 ";
for(int i=10;i>0;i--)
{
cout<<setw(2)<<i;
cout.flush();
Sleep(1000);
cout<<"/b/b";
if(kbhit())
{
int f=key();
break;
}
}
system("cls");
cout<<"/n/n 游戏加载中...... /n/n";
cout<<" ";
for (int k=1;k<=20;k++)
{
cout<<"▌";
cout.flush();
Sleep(50+10*k);
}
game();
}