选择java 进入自由开放的国度

随笔 - 49, 文章 - 3, 评论 - 154, 引用 - 1
数据加载中……

C++猜数字游戏

/*-----------------------------------------------------------
  猜数字游戏
  C++中数组的运用,条件语句,赋值语句,循环语句等元素的应用
-----------------------------------------------------------*/

#include <iostream>
#include <string>

using namespace std;

const int seq_size = 18;
const int max_seq  = 6;
const int max_tries = 3;  

string seq_names[max_seq] = {
  "Fibonacci",
  "Lucas",
  "Pell",
  "Triangular",
  "Square",
  "Pentagonal"
};

int elem_seq[ seq_size ] = {
   1, 2, 3,   //Fibonacci
   3, 4, 7,   //lucas
   2, 5, 12,  //Pell
   3, 6, 10,  //Triangular
   4, 9, 16,  //Square
   5, 12, 22  //Pentagonal
};

int usr_val;          //the value user inputed
int num_total;        //total times
int num_tries = 0;    //the try number
int num_rights = 0;    //the right times
int cur_tuple = 0;    //the location pointer of current array

double usr_score;     //user's score
char usr_rsp;        //try again?


bool go_for_it = true; //try again
bool next_seq  = true; //display the next number
bool got_it    = false;//right or error?


int main(){
 while ( next_seq && cur_tuple < seq_size) {
   //display the array
      cout << " the First two elements of the sequence are: "
     << elem_seq[ cur_tuple ] << ", "
     << elem_seq[ cur_tuple + 1]
     << " in the "
     << seq_names[ cur_tuple/3]
     << "  sequence.\n"
     << "What is the next element?";
    
  //初始化,进入循环,判断是否正确
  got_it    = false;
  go_for_it = true;
        num_tries = 0;
  while ((!got_it) && (go_for_it)) {
     int usr_guess;
     cin >> usr_guess;
     num_tries++;
    
     if (usr_guess == elem_seq[ cur_tuple + 2]){
       //答对了
    cout << "Congratulations! you are right! \n";
    got_it = true;
       num_rights++;
     }
     else{
       //猜错了
    //告诉用户是错的,询问是否愿意再试一次
      switch (num_tries){
      case 1 :
       cout << "Oops! Nice guess but not quite it.\n";
       break;
      case 2 :
       cout << "Hmm, Sorry, wront a second time. \n";
       break;
      case 3 :
       cout << "Ah, this is harder than it looks, isn't it?\n";
       go_for_it = false; //跳出
       break;
      default :
       cout << "It must be getting pretty frustrating by now! \n";
       break;
      }//switch
      if (num_tries < 3) cout << "Do you try it again? (Y|N)" ;
      cin  >> usr_rsp;
               if (usr_rsp == 'N' || usr_rsp == 'n')
       go_for_it = false;
     }//if
        }//small while
  
  cout << "Want to try another sequence? (Y/N)";
  char try_again;
  cin >> try_again;
  if (try_again == 'N' || try_again == 'n')
   next_seq = false;
  
  cur_tuple += 3;             //指到下一个数列
  num_total += num_tries;     //总数
 };//large while
 
  //输出得分,简单统计信息
 cout << "---------------------------- \n"
   << "the total : " << num_total
   << "the right times: " << num_rights << "\n"
   << "---------------------------- \n";
  return 0;
}

posted on 2005-12-23 16:15 soochow_hhb 以java论成败 以架构论英雄 阅读(804) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: