2017-04-09 72 views
0

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

//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`"); 
    } 
} 
+0

你只需要写很多if语句,比如'if(dice1 == 1 && dice2 == 1 && dice3 == 1)result = 5 * wager;'。等。 –

+0

滚动后,按升序对骰子进行排序。这样可以更容易地检测1,2,3等序列,因为您不必检查例如3,2,1或2,1,3 –

+0

@JasonLang你能帮我整理一下吗?我真的不知道如何排序... – elburatski

回答

0

我会建议编写你的算法首先在一篇论文中,就像你在代码顶部的评论中所做的那样。

然后问问自己,你需要怎样处理最终的投注?在参数中。例如,在你的情况下,你可能需要一个函数,它将初始投注和三个骰子的值作为输入参数。

此功能将返回最后的投注。

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

希望这会有所帮助。

相关问题