首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何编写一个函数来检查3个数字的组合?

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

Stack Overflow用户
提问于 2017-04-09 19:50:49
回答 1查看 79关注 0票数 0

我目前正在制作一个双人骰子游戏,我需要创建一个函数来检查您掷出的骰子组合的值。例如:我滚动了3-4-2,我需要这个函数来检查是否有3-4-2的支出,示例滚动和他们的支出+代码如下

代码语言:javascript
复制
 //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`");
    }
}
EN

回答 1

Stack Overflow用户

发布于 2017-04-09 20:03:19

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

然后,问问你自己,你需要什么来处理最后的赌注?如参数中所示。例如,在您的例子中,您可能需要一个函数,该函数将初始赌注和三个骰子的值作为输入参数。

此函数将返回最终赌注。

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

希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43306354

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档