自己编的猜数字小游戏

自己撸了半个小时的代码才编出来的一点点东西。。。

输入考虑不足,当输入字符时,就会崩溃。。。

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;

typedef struct model{
    int num[4] = {0,0,0,0};
    int answer[4] = {0,0,0,0};
    int flag;
}model;

model computer;
model player;

int judgec(int num[],int n,int m)
{
    for(int i = 0;i < n;i++)
    {
        if(num[i] != m)
            continue;
		else
			return 1;
    }
    return 0;
}

void GetNumFromComputer(int num[]){
    cout << "电脑正在选数,请稍等。。。" << endl;
    int n = 0;
    srand((unsigned)time(0));
    int m = rand() % 10;
    while(m == 0)
    {
        m = rand() % 10;
    }
    num[0] = m;
    for(int i = 1;i < 4;i++)
    {
        srand((unsigned)time(0));
        m = rand() % 10;
        n = judgec(num,i,m);
        if(n == 0)
            num[i] = m;
        else
            i--;
    }
}

int judgep(int num[],int number)
{
    if(number > 9999 || number < 1000)
    {
        cout << "请按要求输入数字:";
        return 1;
    }
    int x = 1000;
    int n = 0;
    int m = number / x;
    num[0] = m;
    for(int i = 1;i < 4;i++)
    {
        number = number - m * x;
        x = x / 10;
        m = number / x;
        n = judgec(num,i,m);
        if(n == 0)
            num[i] = m;
        else
        {
            cout << "请按要求输入数字:";
            return 1;
        }
    }
    return 0;
}

void enter()
{

    /**
    cout << "请输入一个大于1000且各位上数字都不相同的四位数:";
    int number;
    int flag = 1;
    while(flag != 0)
    {
        cin >> number;
        flag = judgep(player.num,number);
    }
    */
    GetNumFromComputer(computer.num);
    /**
    int n = 4;
    for(int i = 0;i < n;i++)
    {
        cout << computer.num[i];
    }
    cout << endl;
    for(int i = 0;i < n;i++)
    {
        cout << player.num[i];
    }
    */
}

int iscontinue(){
    int flag = 0;
    while(flag != 1)
    {
        cout << "您是否想继续游戏?(Y/N):";
        char c;
        cin >> c;
        if(c == 'Y' || c == 'y')
        {
            return 0;
        }
        else if(c == 'N' || c == 'n')
        {
            return 1;
        }
        else
        {
            cout << "请您按照规定输入内容!" << endl;
        }
    }
}

int judgement(int numberg){
    if(numberg < 1000 || numberg > 9999)
    {
        cout << "开什么玩笑!我让你猜的是4位数!" << endl;
        return 0;
    }
    int x = 1000;
    int m = numberg / x;
    player.answer[0] = m;
    for(int i = 1;i < 4;i++)
    {
        numberg = numberg - m * x;
        x = x / 10;
        m = numberg / x;
        player.answer[i] = m;
    }
    int numberA = 0;
    int numberB = 0;
    for(int j = 0;j < 4;j++)
    {
        if(computer.num[j] == player.answer[j])
            numberA++;
    }
    for(int i = 0;i < 4;i++)
    {
        for(int j = 0;j < 4;j++)
        {
            if(i == j)
                continue;
            if(player.answer[i] == computer.num[j])
                numberB++;
        }
    }
    if(numberA == 4)
        return 1;
    for(int i = 1;i <= numberA;i++)
        cout << "A";
    for(int i = 1;i <= numberB;i++)
        cout << "B";
    if(numberA == 0 && numberB == 0)
        cout << "电脑:我看您这运气也就别买彩票了吧!";
    cout << endl;
    return 0;
}

void play()
{
    int flags = 0;
    while(flags != 1)
    {
        cout << "请输入您猜的数字:";
        int numberg;
        cin >> numberg;
        flags = judgement(numberg);
    }
    cout << "电脑:什么?!您还没中500万?今天的您运气一定会中大奖的!" << endl;
    cout << "电脑给出的数字是:";
    for(int i = 0;i < 4;i++)
    {
        cout << computer.num[i];
    }
    cout << endl;
    return;
}

int main(){
    cout << "开始游戏!" << endl << endl;
    cout << "游戏规则说明:" << endl;
    cout << "1.电脑所选数字均为各个位上数字均不相等的四位数;" << endl;
    cout << "2.如果您所猜的数,其中若有n个数字的位置和数字与电脑所给的数相同," << endl;
    cout << "  电脑将会提示您n个字符\'A\';" << endl;
    cout << "3.如果您所猜的数,其中若有n个数字与电脑所给的数相同,但位置不同," << endl;
    cout << "  电脑将会提示您n个字符\'B\';" << endl;
    cout << "祝您游戏愉快!" << endl << endl;
    int over = 0;
    while(over != 1)
    {
        enter();
        play();
        over = iscontinue();
        cout << endl;
    }
    cout << "再见!";
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值