C++实现1A2B小游戏(源代码)

最近感冒了(〃>_<;〃),更新放慢为3天2更,可以吗
_| ̄|● ===》▇█▇▇▇█▇
>求关注,求点赞,求评论<
Thanks♪(・ω・)ノ
1A2B游戏规则介绍:(不看不知道,一看吓一跳)
你和对手分别选定一个四位数,各位数字不要重复。
游戏开始后,由双方分别猜对方所选定的四位数,猜测的结果将会列在自己的猜测历史列表,并以A和B来表示结果。
A代表猜测的数字中,数字相同且位置也正确的个数。
B代表猜测的数字中,数字相同但位置不一样的个数。
举例来说,如果对方的数字为1234,且你猜的数字为5283,其中2被猜到且位置正确,3也被猜到但位置不对,所以结果会出现1A1B。
比赛由先完整猜出对方数字的人获得胜利(也就是先得到4A的玩家)。

#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[5] = "";
char input[10] = "";
int times = 0;
int main(int argc, char** argv) {
    char c;
    while (true){
        cout << "输入“S”开始游戏,“Q”退出游戏." << endl;
        c = toupper(getchar());
        while(getchar() != '\n');
        if (c == 'Q')
            break;
        else if(c == 'S'){
            cout << "游戏开始!输入您的答案:" << endl;
            times = 0;
            InitializeGame();//初始化游戏
//            cout << "The answer is: " << answer << endl; 
            GetInput(input); //输入猜测值 
            //检查猜测是否正确 不正确则给出提示 
            while(GiveTips(input) == false){
                times++;
                GetInput(input);                
            }
            times++;
            cout << "恭喜!你已经得到了它 " << times << " times." << endl;
        }else
            cout << "只接收“S”和“Q”。" << endl;     
    }
    return 0;
}
/****************************************************************************** 
 *函数名称: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) != 4){
            cout << "请输入一个4位数!" << endl;
            gets(input);    
            continue;    
        }
        if(CheckAnswer(input) == false){
            cout << "你的答案中不能有两个相同的字符!" << endl;
            gets(input);//不合法则重新输入 
            continue;
        }
        break;
    }
}
/****************************************************************************** 
 *函数名称:bool checkanswer(char * input)
 *函数功能:判断答案是否合法,即是否存在重复数字 
 *入口参数:input为待判断的答案 
 *返 回 值:正确则返回真,否则返回假 
 *******************************************************************************/ 
bool CheckAnswer(char * input){
    char temp[5];
    strcpy (temp, input);
    for(int i = 0; i < 4; i++){
        for(int j = i + 1; j < 4; 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 = 0, b = 0;
    for(int i = 0; i < 4; i++){
        for(int j = 0; j < 4; 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 == 4)
        return true;
    cout << "输入另一个答案:";
    return false;
}
/****************************************************************************** 
 *函数名称:void GetRandom(char * random)
 *函数功能:产生一个各位数不相等的四位随机数 
 *入口参数:random为返回的随机数 
 *返 回 值:无 
 *备 注:先生成一个0-9的整数数组,再随机从中取四个数,每取一个将该位置为-1 
 *******************************************************************************/  
void GetRandom(char * random){
    int i, j[10], k;
    for (i = 0; i < 10; i++){
        j[i] = i;
    }
    for(i = 0; i < 4; i++){
        //生成第i个随机数 
        k = (int)rand() % 10;//k为下标 
        while (j[k] == -1){
            k = (k + 1) % 10; 
        }
        random[i] = '0' + j[k];
        j[k] = -1;
    }
}

这次比较
这其实是我第七次发帖子哟!
你的一个赞,一个关注,一个评论就是对我这个中学生最的鼓励
>求关注,求点赞,求评论<
Thanks♪(・ω・)ノ

发布了8 篇原创文章 · 获赞 15 · 访问量 895

猜你喜欢

转载自blog.csdn.net/zhaoyipengmzyc/article/details/104238332
今日推荐