C++小练习:猜数游戏

简介: C++小练习:猜数游戏

编写猜数小游戏。要去玩家从数列中猜一个数,每猜对一个加一分,并(使用文件)记录玩家的最高分。

 

//----斐波那契数列 猜数游戏----
//----时间:2019.11.20---------
//
 
 
#include<vector>
#include<string>
#include<iostream>
#include<fstream>
 
#include<cstdlib>
#include<time.h>
using namespace std;
 
 
//斐波那契数列生成函数
//输入参数:int 数列大小
//返回值:斐波那契数组首地址
const vector<int>*
fibon_seq(int size)
{
  static vector<int> elems;
  const int max_size = 1024;
  if (size <= 0 || size > max_size)
  {
    cerr << "错误的大小\n";
    return 0;
  }
  for (int ix = elems.size(); ix < size; ++ix) {
    if (ix == 0 || ix == 1)
      elems.push_back(1);
    else
      elems.push_back( elems[ix - 1] + elems[ix - 2]);
  }
 
  return &elems;
}
 
void write_score_file(string user_name,int num_right)
{
  ofstream outfile("score.txt",ios_base::app); //追加模式打开
  if (!outfile) {
    cerr << "无法打开score.txt\n";
  }
  else {
    outfile << user_name<<' '<<num_right<<endl;
  }
 
}
void read_score_file(string &user_name, int & num_right) {
  ifstream infile("score.txt");
  if (!infile) {
    cerr << "无法打开文件score.txt\n";
  }
  else {
    string name;
    int n_right;
    while (infile >> name) {
      infile >> n_right;
      if (name == user_name && n_right>num_right) {
        num_right = n_right;
      }
    }
  }
}
int main()
{
  string user_name;
  int num_right = 0;
  int highest_score = 0; //最高分
  cout << "Please enter your name:";
  cin >> user_name;
  cout << "\n"
    << "Hello,"
    << user_name
    <<endl;
  int fibo_size = 10;
  const vector<int>* fibo = fibon_seq(fibo_size);
  for (int i = 0; i < (*fibo).size(); i++) {
    cout << "第" << i + 1 << "个:" << (*fibo)[i] << ' ';
    if ((i + 1) % 3 == 0)
      cout << "\n";
  }
  cout << endl;
 
  bool willing_try = true;
 
  while (willing_try) {
    srand((unsigned)time(NULL));
    int index = rand() % fibo_size;
 
    cout << "猜一个数:";
    int n;
    cin >> n;
    if (n == (*fibo)[index]) {
      cout << "猜对了\n再猜一次吗(y\n)";
      num_right++;
    }
    else {
      if (n > (*fibo)[index])
        cout << "太大了\n";
      else
        cout << "太小了\n";
      cout << "再猜一次吗(y\n):";
    }
    char choice;
    cin >> choice;
 
    if (choice != 'y' && choice != 'Y') {
      willing_try = false;
    }
  }
 
  if (num_right > highest_score)
  {
    cout << "\n----新纪录: " << num_right<<"----"<< endl;
 
    write_score_file(user_name, num_right);
  }
 
  return 0;
}
相关文章
|
1月前
|
C++
C++:OJ练习(每日练习系列)
C++:OJ练习(每日练习系列)
45 2
|
1月前
|
IDE 测试技术 开发工具
Poco新增对cocos c++游戏的支持
Poco新增对cocos c++游戏的支持
|
1月前
|
Shell C++
C++:OJ练习(每日练习系列)
C++:OJ练习(每日练习系列)
34 1
|
1月前
|
Serverless C++
C++:OJ练习(每日练习!)
C++:OJ练习(每日练习!)
41 0
|
30天前
|
算法 C语言 容器
从C语言到C++_18(stack和queue的常用函数+相关练习)力扣(上)
从C语言到C++_18(stack和queue的常用函数+相关练习)力扣
25 0
|
2天前
|
C++ 容器
C++之评委打分案例(vector与deque容器练习)
C++之评委打分案例(vector与deque容器练习)
8 1
|
13天前
|
定位技术 C++ Windows
第一人称射击游戏 C++控制台版(未完成)
第一人称射击游戏 C++控制台版(未完成)
第一人称射击游戏 C++控制台版(未完成)
|
1月前
|
编译器 C语言 C++
从C语言到C++⑥(第二章_类和对象_中篇_续)大练习(日期类)+笔试选择题(下)
从C语言到C++⑥(第二章_类和对象_中篇_续)大练习(日期类)+笔试选择题
31 2
从C语言到C++⑥(第二章_类和对象_中篇_续)大练习(日期类)+笔试选择题(下)
|
20天前
|
存储 人工智能 C++
【PTA】L1-093 猜帽子游戏(C++)
【PTA】L1-093 猜帽子游戏(C++)
18 1
|
30天前
|
存储 前端开发 算法
从C语言到C++_18(stack和queue的常用函数+相关练习)力扣(下)
从C语言到C++_18(stack和queue的常用函数+相关练习)力扣
10 0