如何编写一个检查3个数字组合的函数?

时间:2017-04-09 11:50:49

标签: c++

我目前正在制作一个2人骰子游戏,我需要创建一个功能来检查你掷出的骰子组合的价值。例如:我打了3-4-2,我需要这个功能来检查是否有3-4-2的支付,例子卷和他们的支出+下面的代码

 //Rolling 1-1-1 would give you 5x your wager
 //Rolling 3 of the same number (except 1-1-1) would give you 2x wager
 //Rolling 3 different numbers (ex 1-4-6) would give you 1x your wager
 //Rolling 1-2-3 makes the player automatically lose a round and pay opposing Player 2x wager

    #include <iostream>
#include <string>
#include <time.h>
using namespace std;

void roll_3_dice(int &dice1, int &dice2, int &dice3) 
{
    srand(time(NULL));
    dice1 = rand() % 6 + 1;
    dice2 = rand() % 6 + 1;
    dice3 = rand() % 6 + 1;
    return;
}


int main()
{
    int cash = 90000;
    int wager; 
    int r;


    //dealer's die
    int dealer1;
    int dealer2;
    int dealer3;

    // your die
    int mdice1;
    int mdice2;
    int mdice3;


    while ( cash > 100 || round < 10 )
    {
        cout << "Set your wager: "<< endl;
        cin >> wager;

        while (wager < 100 || wager > 90000)
        {
            cout << "Minimum wager is 100; Maximum wager is 90000 ";
            cin >> wager;
        }

        cout << "You wagered: " << wager << endl;
        cout << "You have " << cash - wager << " remaining" << endl;
        cash = cash - wager;

        cout << endl;
        cout << "Dealer will now roll the dice" << endl;

        roll_3_dice(dealer1, dealer2, dealer3);

        cout << "Dealer rolled the following: " << endl;
        cout << dealer1 << "-" << dealer2 << "-" << dealer3 << endl;

        cout << "It's your turn to roll the dice." << endl;
        cout << endl; 
        cout << "Press any key to roll the dice" << endl;
        cin >> r;

        roll_3_dice(mdice1, mdice2, mdice3);

        cout << "You rolled the following: " << endl;
        cout << mdice1 << "-" << mdice2 << "-" << mdice3 << endl;

        system ("pause`enter code here`");
    }
}

1 个答案:

答案 0 :(得分:0)

我建议您首先在纸上编写算法,就像在代码顶部的注释中所做的那样。

然后,问问自己处理最终赌注需要什么?如在参数中。例如,在您的情况下,您可能需要一个接受初始下注的函数,以及三个骰子的值作为输入参数。

此功能将返回最终的赌注。

最后,在函数本身中,按优先级规则组织算法。 如果1-1-1给你5倍的赌注,那么这是一个特殊情况,可以在函数顶部隔离,你可以直接返回5 x初始投注,而无需进一步处理。 您只需要使用if语句组织不同的案例,关于每个语句的优先级。 例如,1-1-1案例必须在“每个骰子的相同数字”之前。

希望这有帮助。