欢迎光临
我们一直在努力

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

 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 }

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

  • 海报
海报图正在生成中...
赞(1) 打赏
声明:
1、本博客不从事任何主机及服务器租赁业务,不参与任何交易,也绝非中介。博客内容仅记录博主个人感兴趣的服务器测评结果及一些服务器相关的优惠活动,信息均摘自网络或来自服务商主动提供;所以对本博客提及的内容不作直接、间接、法定、约定的保证,博客内容也不具备任何参考价值及引导作用,访问者需自行甄别。
2、访问本博客请务必遵守有关互联网的相关法律、规定与规则;不能利用本博客所提及的内容从事任何违法、违规操作;否则造成的一切后果由访问者自行承担。
3、未成年人及不能独立承担法律责任的个人及群体请勿访问本博客。
4、一旦您访问本博客,即表示您已经知晓并接受了以上声明通告。
文章名称:《C\C++ 1A2B小游戏源码》
文章链接:https://www.456zj.com/41022.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址