2017-04-09 26 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的情況必須在「每個骰子的相同數字」之前。

希望這會有所幫助。

相關問題