C\C++ 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 <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( 17 bool GiveTips( 18 void GetRandom( 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: 35             times =  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. 46         }else
 47             cout << Only 'S' and 'Q' are received. endl;     
 48     }
 49 
 50     return  51 }
 52 
 53 /****************************************************************************** 
 54 *函数名称:void InitializeGame(void)
 55 *函数功能:初始化游戏,生成随机数 
 56 *入口参数:无
 57 *返 回 值:无
 58 *******************************************************************************/  
 59  60     static bool init_rand =  61     if (init_rand ==  62         srand ((unsigned) time(NULL)); 如果未初始化则初始化随机数种子 
 63         init_rand =  64  65     GetRandom(answer);生成随机数 
 66     cout << answer << endl;
 67  68 
 69  70 *函数名称:void GetInput(char * input)
 71 *函数功能:读取一个字符串 
 72 *入口参数:返回读取的字符串 
 73 *返 回 值:无 
 74 */ 
 75  input){
 76     gets(input);
 77     while( 78         if(strlen(input) != 4 79             cout << Please input a 4-digits number! 80             gets(input);    
 81             continue;    
 82         }
 83         if(CheckAnswer(input) ==  84             cout << There couldn't be two same character in your answer! 85             gets(input);不合法则重新输入 
 86              87  88          89  90  91 
 92  93 *函数名称:bool checkanswer(char * input)
 94 *函数功能:判断答案是否合法,即是否存在重复数字 
 95 *入口参数:input为待判断的答案 
 96 *返 回 值:正确则返回真,否则返回假 
 97  98  99     char temp[5];
100     strcpy (temp,input);
101     for(int i = 0; i < 4; i++102         int j = i + 1; j < 4; j++103             if(temp[i] == input[j])
104                 return 105     } 
106     ; 
107 108 
109 110 *函数名称:void GiveTips(char * input)
111 *函数功能:根据输入的答案来给出提示 
112 *入口参数:待判断的答案 
113 114 115 116     cout << "I'm checking." << endl;
117     int a = 0,b = 118     119         int j = 0; j < 120             cout << "i:" << i << "j:" << j << endl; 
121             if (input[i] == answer[j]){
122                 if(i == j)
123                     a++124                 125                     b++126                 127 128 129 130     cout << Tips:" << a << A" << b << B\n endl; 
131     if (a == 132         133     cout << Enter another answer:"134     135 136 
137 138 *函数名称:void GetRandom(char * random)
139 *函数功能:产生一个各位数不相等的四位随机数 
140 *入口参数:random为返回的随机数 
141 142 *备 注:先生成一个0-9的整数数组,再随机从中取四个数,每取一个将该位置为-1 
143 144  random){
145     int i,j[10],k;
146     for (i = 10; i++147         j[i] = i;
148 149     for(i = 150         生成第i个随机数 
151         k = (int)rand() % 10;k为下标 
152         while (j[k] == -1153             k = (k + 1) % 154 155         random[i] = 0' + j[k];
156         j[k] = -157 158 }

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

 

相关文章

一.C语言中的static关键字 在C语言中,static可以用来修饰局...
浅谈C/C++中的指针和数组(二) 前面已经讨论了指针...
浅谈C/C++中的指针和数组(一)指针是C/C++...
从两个例子分析C语言的声明 在读《C专家编程》一书的第三章时...
C语言文件操作解析(一)在讨论C语言文件操作之前,先了解一下...
C语言文件操作解析(三) 在前面已经讨论了文件打开操作,下面...