#include<iostream>
#include<cstdlib>
#include<fstream>
#include<ctime>
#include<process.h>
#include<cstring>
using namespace std;
class game
{
public:
game(){}
void file();//文件初始化,即把姓名和胜率输入
void rule();//游戏规则显示
void shengchengshuzi();//生成随机数
void getname();//输入玩家昵称
void getnum();//得到玩家输入的数字
void panduan();//系统判断数字的值和位置是否正确
char result(int);//输出判断的结果
void shuzi();//输出本次的数字
int jixu();//询问玩家是否继续
void tongji();//若选择结束,统计玩家的胜率
void baocun();//保存玩家的数据
protected:
int a[4] = { 0 }, b[4] = { 0 };
int m;
float total = 0;
float win = 0;
float acount = 0;
float bcount = 0;
float shenglv=0;
string name;
};
void game::file()
{
char buf[10] = { 0 };
fstream f1, f2;
f1.open("D:\\date.txt", ios::in);
if(!f1)
{
cout<< "文件打开出错" << endl;
}
f1.getline(buf, 10);//读取文档内容判断是否为空
if (buf[0] == NULL)//若文件为空,执行下列程序输入格式
{
f2.open("D:\\date.txt", ios::app);
if (!f2) //条件成立,则说明文件打开出错
{
cout<< "文件打开出错" << endl;
}
f2 << "姓名" << '\t' << "胜率" << '\t' << endl;
f2.close();
}
else
f1.close();
}
void game::rule()
{
cout << "游戏规则:" << endl;
cout << " 1.每次游戏有八次机会,每次输入四个0-9数字(可重复),用空格隔开" << endl;
cout << " 2.如果数字正确位置正确,会显示A,如果数字正确位置错误,会显示B。" << endl;
cout << " 3.如果四个数字全部正确,则会显示victory,若八次后还没有猜对,则会显示defeat。" << endl;
cout << " 4.例如,随机生成的数字是1 2 3 4,而你输入的数字是1 3 4 5,则会显示1A2B。" << endl;
}
void game::getname()
{
cout << "请输入你的昵称:"<<endl;;
cin>>name;//可以在输入时输入空格
}
void game::shengchengshuzi()
{
for (int i = 0; i < 4; i++)
a[i] = rand() % 10;//生成十以内的随机数
}
void game::getnum()
{
cout << "猜测数字是:";
for (int i = 0; i < 4; i++)
cin >> b[i];//玩家输入数字
}
void game::panduan()
{
int vis[8];
acount = bcount = 0;
for (int i = 0; i < 4; i++)
{
if (a[i] == b[i])
{
acount++;
vis[i]=1;
}
}
for (int j = 0; j < 4; j++)
{
for (int i = 0; i < 4; i++)
{
if (a[i] == b[j] &&!vis[i])
{
bcount++;
vis[i]=1;
break;
}
}
}
}
char game::result(int t)
{
win = total = 0;
cout << "数字结果是:";
if (acount == 4)
{
cout << "victory" << endl;
total++;
win++;
return 'v';
}
else if (t == 0 && acount != 4)
{
total++;
cout << "defeat" << endl;
}
else
{
cout << acount << "A" << bcount << "B" << endl;
cout << "你还有" << t << "次机会" << endl;
}
}
void game::shuzi()
{
cout << "本次数字是:";
for (int i = 0; i < 4; i++)
cout << a[i] << " ";
cout << endl;
}
int game::jixu()
{
cout << "输入1继续游戏,输入0结束游戏" << endl;
cin >> m;
if (m == 1)
{
rule();
}
return m;
}
void game::tongji()
{
shenglv = (win * 1.0 * 100) / total;
cout << "姓名:" << name << "胜率:" <<shenglv<< endl;
}
void game::baocun()
{
fstream f;
f.open("D:\\date.txt", ios::in | ios::out | ios::app);
if (!f) //条件成立,则说明文件打开出错
cout << "文件打开出错" << endl;
f << name << '\t' << shenglv << "%" << '\t' << endl;
f.close();
}
int main()
{
srand(time(NULL));//生成随机数
game g;
g.file();//文件格式化
g.rule();//显示规则
g.getname();//得到玩家昵称
int m = 1;
int t;
while (m == 1)
{
t = 8;
g.shengchengshuzi();//生成随机数
while (t--)
{
g.getnum();
g.panduan();//判断是否正确
char c = g.result(t);
if (c == 'v')
break;//若正确结束
}
g.shuzi();
m = g.jixu();//结束标志
}
g.tongji();//统计胜率
g.baocun();//保存数据
cout << "您可以稍后在'D:\\date.txt'文档里查看您的胜率" << endl;
猜数字 C++代码
最新推荐文章于 2024-09-20 20:27:00 发布