首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C\C++ 1A2B小游戏源码

C\C++ 1A2B小游戏源码

作者头像
弗兰克的猫
发布2018-01-09 14:55:13
2.1K0
发布2018-01-09 14:55:13
举报

  学了一段时间,心血来潮写了一个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 <iostream>
  9 #include <stdlib.h>
 10 #include <time.h>
 11 #include <string.h>
 12 #include <stdbool.h>
 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 /****************************************************************************** 
 54 *函数名称:void InitializeGame(void)
 55 *函数功能:初始化游戏,生成随机数 
 56 *入口参数:无
 57 *返 回 值:无
 58 *******************************************************************************/  
 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 /****************************************************************************** 
 70 *函数名称:void GetInput(char * input)
 71 *函数功能:读取一个字符串 
 72 *入口参数:返回读取的字符串 
 73 *返 回 值:无 
 74 *******************************************************************************/ 
 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 /****************************************************************************** 
 93 *函数名称:bool checkanswer(char * input)
 94 *函数功能:判断答案是否合法,即是否存在重复数字 
 95 *入口参数:input为待判断的答案 
 96 *返 回 值:正确则返回真,否则返回假 
 97 *******************************************************************************/ 
 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 /****************************************************************************** 
110 *函数名称:void GiveTips(char * input)
111 *函数功能:根据输入的答案来给出提示 
112 *入口参数:待判断的答案 
113 *返 回 值:无 
114 *******************************************************************************/ 
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)
123                     a++;
124                 else
125                     b++;
126                 continue;
127             }
128         }
129     }
130     cout << "Tips:" << a << "A" << b << "B\n" << endl; 
131     if (a == 4)
132         return true;
133     cout << "Enter another answer:";
134     return false;
135 }
136 
137 /****************************************************************************** 
138 *函数名称:void GetRandom(char * random)
139 *函数功能:产生一个各位数不相等的四位随机数 
140 *入口参数:random为返回的随机数 
141 *返 回 值:无 
142 *备 注:先生成一个0-9的整数数组,再随机从中取四个数,每取一个将该位置为-1 
143 *******************************************************************************/  
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 }

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-12-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档