学了一段时间,心血来潮写了一个1A2B小游戏,很多人应该玩过,是一个挺有意思的益智小游戏,之前用易语言写过,现在又用C++重写了一下。
编译运行无错,整体程序设计思路为:进入循环,初始化游戏,读入一个数,判断是否合法,判断是否符合规则,判断是否正确,再给出答案提示。各部分都用函数封转方便管理和维护。不过有一点确实还需要改进,就是在输入输出语句的使用上,显得有些许混乱,用一个单独的函数来二次封装也许会更好,这样也能方便控制程序在任何时候都能退出游戏和做出最外层的响应。
1A2B游戏规则介绍:
你和对手分别选定一个四位数,各位数字不要重复。
游戏开始后,由双方分别猜对方所选定的四位数,猜测的结果将会列在自己的猜测历史列表,并以A和B来表示结果。
A代表猜测的数字中,数字相同且位置也正确的个数。
B代表猜测的数字中,数字相同但位置不一样的个数。
举例来说,如果对方的数字为1234,且你猜的数字为5283,其中2被猜到且位置正确,3也被猜到但位置不对,所以结果会出现1A1B。
比赛由先完整猜出对方数字的人获得胜利(也就是先得到4A的玩家)。
代码如下:
// name:1A2B.cpp
// author:Frank
// descraption: 1A2B Game, in the beginning, the program will generate a random four-digits number
// in which the digits is not equal to each other, you need to guess it, and as you
// enter your answer, there would be a tips likes: xAyB. x means the count of right
// numbers those are in the right sites, y means the count of right numbers those
// are not in the right sites. 4A0B means you have got the right number.
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <stdbool.h> void InitializeGame(void);
void GetInput(char * input);
bool CheckAnswer(char * input);
bool GiveTips(char * input);
void GetRandom(char * random);
using namespace std; char answer[] = "";
char input[] = "";
int times = ; int main(int argc, char** argv) {
char c;
while (true){
cout << "Enter 'S' to start the game, 'Q' to quit." << endl;
c = toupper(getchar());
while(getchar() != '\n');
if (c == 'Q')
break;
else if(c == 'S'){
cout << "Game Start! Enter your answer:" << endl;
times = ;
InitializeGame();//初始化游戏
// cout << "The answer is: " << answer << endl;
GetInput(input); //输入猜测值
//检查猜测是否正确 不正确则给出提示
while(GiveTips(input) == false){
times++;
GetInput(input);
}
times++;
cout << "Congratulations! You have got it after " << times << " times." << endl;
}else
cout << "Only 'S' and 'Q' are received." << endl;
} return ;
} /******************************************************************************
*函数名称:void InitializeGame(void)
*函数功能:初始化游戏,生成随机数
*入口参数:无
*返 回 值:无
*******************************************************************************/
void InitializeGame(void){
static bool init_rand = false;
if (init_rand == false){
srand ((unsigned) time(NULL)); //如果未初始化则初始化随机数种子
init_rand = true;
}
GetRandom(answer);//生成随机数
// cout << answer << endl;
} /******************************************************************************
*函数名称:void GetInput(char * input)
*函数功能:读取一个字符串
*入口参数:返回读取的字符串
*返 回 值:无
*******************************************************************************/
void GetInput(char * input){
gets(input);
while(true){
if(strlen(input) != ){
cout << "Please input a 4-digits number!" << endl;
gets(input);
continue;
}
if(CheckAnswer(input) == false){
cout << "There couldn't be two same character in your answer!" << endl;
gets(input);//不合法则重新输入
continue;
}
break;
}
} /******************************************************************************
*函数名称:bool checkanswer(char * input)
*函数功能:判断答案是否合法,即是否存在重复数字
*入口参数:input为待判断的答案
*返 回 值:正确则返回真,否则返回假
*******************************************************************************/
bool CheckAnswer(char * input){
char temp[];
strcpy (temp, input);
for(int i = ; i < ; i++){
for(int j = i + ; j < ; j++)
if(temp[i] == input[j])
return false;
}
return true;
} /******************************************************************************
*函数名称:void GiveTips(char * input)
*函数功能:根据输入的答案来给出提示
*入口参数:待判断的答案
*返 回 值:无
*******************************************************************************/
bool GiveTips(char * input){
// cout << "I'm checking." << endl;
int a = , b = ;
for(int i = ; i < ; i++){
for(int j = ; j < ; j++){
// cout << "i:" << i << "j:" << j << endl;
if (input[i] == answer[j]){
if(i == j)
a++;
else
b++;
continue;
}
}
}
cout << "Tips:" << a << "A" << b << "B\n" << endl;
if (a == )
return true;
cout << "Enter another answer:";
return false;
} /******************************************************************************
*函数名称:void GetRandom(char * random)
*函数功能:产生一个各位数不相等的四位随机数
*入口参数:random为返回的随机数
*返 回 值:无
*备 注:先生成一个0-9的整数数组,再随机从中取四个数,每取一个将该位置为-1
*******************************************************************************/
void GetRandom(char * random){
int i, j[], k;
for (i = ; i < ; i++){
j[i] = i;
}
for(i = ; i < ; i++){
//生成第i个随机数
k = (int)rand() % ;//k为下标
while (j[k] == -){
k = (k + ) % ;
}
random[i] = '' + j[k];
j[k] = -;
}
}
代码格式相对工整,每个函数都比较简短,便于阅读和理解。当然,如果有更好的建议,还望不啬赐教。