2014-11-04 56 views
1

這個程序運行良好,但像一個真正的賭場老闆。我希望在不增加更多可能的情況下儘可能減少支出。我想這樣做的方式是爲每個數字添加概率。有8種可能性(2,3,4,5,6,7,8,9),我希望7發生最少。所以我想將概率設定爲2-6,在75%(每個15%)8-9在20%(每個10%)和7在5%。有人能幫我嗎?如何將概率添加到遊戲中?

#include <cstdlib> 
#include <iostream> 
#include <cmath> 
#include <ctime> 

using namespace std; 
int Random(int low, int high); 
int Result(int a, int b, int c, int chips, int bet); 
int main() 
{ 
    srand(time(0)); 
    int low1(2), low2(2), low3(2); 
    int high1(9), high2(9), high3(9); 
    int menu=0; 
    int bet =0; 
    bool quit=0; 
    cout << "Player's chips: $1000" << endl; 
    int chips=1000; 
    while (!quit) 
    { 
    cout << "1) Play slot. 2) Exit."; 
    cin >> menu; 
    switch (menu) 
    { 
     case 1: 
     { 
      cout << "Enter your bet: "; 
      cin >> bet; 
      if (bet<=0 || bet>chips) 
      { 
       cout << "You did not enter a valid bet." << endl; 
       menu=1; 
      } 
      else 
      { 
       int a = Random(low1,high1); 
       int b = Random(low2,high2); 
       int c = Random(low3,high3); 
       cout << a << " " << b << " " << c << endl; 
       chips = Result(a,b,c,chips,bet); 
       cout << "Player's chips: $" << Result(a,b,c,chips,bet) << endl; 
      } 
      break; 
     } 
     case 2: 
     { 
      cout << "Exiting..." << endl; 
      quit=1; 
      break; 
     } 
     default: 
     { 
     cout << "Not a valid menu !"<<endl; 
     } 
    } 
    } 
    if (chips <=0) 
    { 
     cout << "You have $0 ! Game Over !" << endl; 
     quit=1; 
    } 
} 

int Random(int low, int high) 
{ 
    int random; 
    random = low + rand() % ((high+1)-low); 
    return random; 
} 

    int Result(int a, int b, int c, int chips, int bet) 
    { 
    if ((a==7 && b==7 && c==7)) 
    { 
     cout << "You won 10 times your bet !($" << (bet*10) << ")" << endl; 
     chips=chips+(bet*10); 
    } 
    if ((a==b==c) && !(a==7 && b==7 && c==7)) 
    { 
     cout << "You won 5 times your bet !($" << (bet*5) << ")" << endl; 
     chips=chips+(bet*5); 
    } 
     if ((a==b || a==c || b==c) && !(a==b==c) && !(a==7 && b==7 && c==7)) 
    { 
     chips=chips+(bet*3); 
     cout << "You won 3 times your bet ! ($" << (bet*3) << ")" << endl; 
     } 
    else 
    { 
     cout << "You lost your bet ! ($-" << bet << ")" << endl; 
     chips=chips-bet; 
    } 
    return chips; 
} 
+2

'(一== b ==ç )'不會做你認爲它做的事。你錯誤地認爲C++是Python。 – rightfold 2014-11-04 21:12:45

+2

如果你知道你想要的可能性和它們的範圍,爲什麼不在1-100之間生成一個數字,把這個隨機值賦給一個將它映射到一個結果的函數,並根據需要存儲/使用該結果? – StarPilot 2014-11-04 21:14:25

+0

你是對的,右撇子。我會改變 – Annam 2014-11-04 21:17:04

回答

1

介紹值的陣列[0.05,0.15,0.25,0.4,0.55,0.7,0.85]值相對應的7,8,9,2,3,4,5,6是如果生成的隨機數字> 0.85。

然後得到0隨機數r爲1

if r <=0.05 then it is 7 
else if r <= 0.15 then it is 8 
else if r <= 0.1 then it is 9 
... 
else if r <= 0.85 then it is 5 
else it is 6 

你能弄清楚如何把它寫下來作爲一個循環,如果你想要的。

+0

或使用'std :: lower_bound'代替循環。 – Jarod42 2014-11-04 21:20:51

0

我將重新編寫函數如下(它可以被修改成更好的東西,但它應該工作開始):

// Generate a random number 2-9, with weighted probabilities 
int Random() 
{ 
    int number; // number 1-9 to generate 
    int prob; // probability 

    // Generate probability value 0-99 
    prob = rand() % 100; 

    // Calculate number with desired probability "additive" 
    if (prob < 15) // 15% probability for 2 
    number = 2; 
    else if (prob < 30) // 15% probability for 3 
    number = 3; 
    else if (prob < 45) // 15% probability for 4 
    number = 4; 
    else if (prob < 60) // 15% probability for 5 
    number = 5; 
    else if (prob < 75) // 15% probability for 6 
    number = 6; 
    else if (prob < 80) // 5% probability for 7 
    number = 7; 
    else if (prob < 90) // 10% probability for 8 
    number = 8; 
    else    // 10% probability for 9 
    number = 9; 
    return number; 
} 

不要忘了添加以下代碼種子隨機數發生器:

srand (time(NULL)); 

我希望這不是對的方式一個真正的老虎機,有可能是法律問題,如果它是:-)