6. Dice Rolling Game (Continued from 5) - gansimu569's Column - CSDN Blog

by dj11441l on 2009-12-07 17:25:25

Here is the translation of your text into English:

---

**Dice Rolling Game:**

1. **Add Bets:** Encapsulate the dice rolling game logic in a function. Initialize `bankBalance` to $1000 (the initial stake) and prompt the player to input their bet (`wager`). Use a `while` loop to check if the `wager` is less than or equal to `bankBalance`. If not, prompt the user to re-enter the `wager` until it is valid. After entering a valid `wager`, run the dice rolling game. If the player wins, increase `bankBalance` by `wager`; if the player loses, decrease `bankBalance` by `wager`, and print the new `bankBalance`. Check if `bankBalance` is 0. If so, print the message "Sorry, You Busted!" During the game, you can print some chat messages, such as "Oh, you're going for broke, huh?" or "Aw cmon, take a chance!" or "You're up big. Now's the time to cash in your chips!"

2. **Run the Dice Rolling Game 1000 Times Based on the Above Program and Answer the Following Questions:**

- (1) How many games were won or lost in the first twelve rounds and after twelve rounds?

- (2) What is the probability of winning? Is the dice rolling game fair? Why or why not?

- (3) Does the probability of winning increase the longer the game is played?

---

**Code:**

```cpp

#include

#include

#include

class Dice {

private:

static int count;

int a, b;

public:

void init(int x, int y) {

a = x;

b = y;

count++;

}

void setcount() { count = 0; }

int getdice1() { return a; }

int getdice2() { return b; }

int Getpoint() { return a + b; } // Dice points

void print() { // Print the result of rolling the dice

cout > wager;

return wager;

}

int continuegame() { // Decide whether to continue playing

cout > x;

while (x != 1 && x != 0) {

cout = 1800)

cout bankBalance) { // Check if the wager is valid

cout = 2000) {

cout bankBalance) {

cout << "Your wager exceeds your balance. Please re-enter!" << endl;

putwager();

continue;

} else

break;

}

}

cout << endl << "Game over. Welcome back next time!" << endl;

}

```

---

This code implements a dice rolling game where the player can place bets, and the results are tracked over multiple rounds. The program also includes analysis of win/loss probabilities based on a large number of simulated games.