栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

CC++ 1A2B小游戏源码

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

CC++ 1A2B小游戏源码

学了一段时间,心血来潮写了一个1A2B小游戏,很多人应该玩过,是一个挺有意思的益智小游戏,之前用易语言写过,现在又用C++重写了一下。

编译运行无错,整体程序设计思路为:进入循环,初始化游戏,读入一个数,判断是否合法,判断是否符合规则,判断是否正确,再给出答案提示。各部分都用函数封转方便管理和维护。不过有一点确实还需要改进,就是在输入输出语句的使用上,显得有些许混乱,用一个单独的函数来二次封装也许会更好,这样也能方便控制程序在任何时候都能退出游戏和做出最外层的响应。

1A2B游戏规则介绍:

你和对手分别选定一个四位数,各位数字不要重复。
游戏开始后,由双方分别猜对方所选定的四位数,猜测的结果将会列在自己的猜测历史列表,并以A和B来表示结果。
  A代表猜测的数字中,数字相同且位置也正确的个数。
  B代表猜测的数字中,数字相同但位置不一样的个数。
  举例来说,如果对方的数字为1234,且你猜的数字为5283,其中2被猜到且位置正确,3也被猜到但位置不对,所以结果会出现1A1B。
  比赛由先完整猜出对方数字的人获得胜利(也就是先得到4A的玩家)。
  代码如下:

 1 // name:1A2B.cpp
  2 // author:Frank
  3 // descraption: 1A2B Game, in the beginning, the program will generate a random four-digits number
  4 //   in which the digits is not equal to each other, you need to guess it, and as you
  5 //  enter your answer, there would be a tips likes: xAyB. x means the count of right
  6 //  numbers those are in the right sites, y means the count of right numbers those
  7 //  are not in the right sites. 4A0B means you have got the right number.
  8 #include 
  9 #include 
 10 #include 
 11 #include 
 12 #include 
 13 
 14 void InitializeGame(void);
 15 void GetInput(char * input);
 16 bool CheckAnswer(char * input);
 17 bool GiveTips(char * input);
 18 void GetRandom(char * random);
 19 using namespace std;
 20 
 21 char answer[5] = "";
 22 char input[10] = "";
 23 int times = 0;
 24 
 25 int main(int argc, char** argv) {
 26     char c;
 27     while (true){
 28  cout << "Enter 'S' to start the game, 'Q' to quit." << endl;
 29  c = toupper(getchar());
 30  while(getchar() != 'n');
 31  if (c == 'Q')
 32      break;
 33  else if(c == 'S'){
 34      cout << "Game Start! Enter your answer:" << endl;
 35      times = 0;
 36      InitializeGame();//初始化游戏
 37 //     cout << "The answer is: " << answer << endl; 
 38      GetInput(input); //输入猜测值 
 39      //检查猜测是否正确 不正确则给出提示 
 40      while(GiveTips(input) == false){
 41   times++;
 42   GetInput(input);  
 43      }
 44      times++;
 45      cout << "Congratulations! You have got it after " << times << " times." << endl;
 46  }else
 47      cout << "only 'S' and 'Q' are received." << endl;     
 48     }
 49 
 50     return 0;
 51 }
 52 
 53   
 59 void InitializeGame(void){
 60     static bool init_rand = false;
 61     if (init_rand == false){
 62  srand ((unsigned) time(NULL)); //如果未初始化则初始化随机数种子 
 63  init_rand = true;
 64     }
 65     GetRandom(answer);//生成随机数 
 66 //    cout << answer << endl;
 67 }
 68 
 69  
 75 void GetInput(char * input){
 76     gets(input);
 77     while(true){
 78  if(strlen(input) != 4){
 79      cout << "Please input a 4-digits number!" << endl;
 80      gets(input);    
 81      continue;    
 82  }
 83  if(CheckAnswer(input) == false){
 84      cout << "There couldn't be two same character in your answer!" << endl;
 85      gets(input);//不合法则重新输入 
 86      continue;
 87  }
 88  break;
 89     }
 90 }
 91 
 92  
 98 bool CheckAnswer(char * input){
 99     char temp[5];
100     strcpy (temp, input);
101     for(int i = 0; i < 4; i++){
102  for(int j = i + 1; j < 4; j++)
103      if(temp[i] == input[j])
104   return false;
105     } 
106     return true; 
107 }
108 
109  
115 bool GiveTips(char * input){
116 //    cout << "I'm checking." << endl;
117     int a = 0, b = 0;
118     for(int i = 0; i < 4; i++){
119  for(int j = 0; j < 4; j++){
120 //     cout << "i:" << i << "j:" << j << endl; 
121      if (input[i] == answer[j]){
122   if(i == j)
123a++;
124   else
125b++;
126   continue;
127      }
128  }
129     }
130     cout << "Tips:" << a << "A" << b << "Bn" << endl; 
131     if (a == 4)
132  return true;
133     cout << "Enter another answer:";
134     return false;
135 }
136 
137   
144 void GetRandom(char * random){
145     int i, j[10], k;
146     for (i = 0; i < 10; i++){
147  j[i] = i;
148     }
149     for(i = 0; i < 4; i++){
150  //生成第i个随机数 
151  k = (int)rand() % 10;//k为下标 
152  while (j[k] == -1){
153      k = (k + 1) % 10; 
154  }
155  random[i] = '0' + j[k];
156  j[k] = -1;
157     }
158 }

代码格式相对工整,每个函数都比较简短,便于阅读和理解。当然,如果有更好的建议,还望不啬赐教。

真正重要的东西,用眼睛是看不见的。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/232840.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号