Among the most popular betting games is a dice game called "craps", which is very popular in entertainment venues and streets around the world. The rules of the game are simple:
The player rolls two dices. Each dice has six sides, containing 1, 2, 3, 4, 5 and 6 points respectively. After rolling the dice, calculate the sum of the points of the two upward faces.
1. If the sum of points thrown for the first time is 7 or 11, the player wins;
2. If the first throw points are only 2, 3 or 12 (called "craps"), then the player loses (i.e. the dealer wins);
3. If the points thrown for the first time are only sums of 4, 5, 6, 7, 8, 9 or 10, then this sum becomes the player's "target points". To win, the player must roll dice continuously until the points are the same as the target points, that is, "the points are obtained." But before getting points, if you throw 7, you will lose.
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; unsigned int rollDice(); int main() { enum Status {CONTINUE, WON, LOST}; //This is to customize a variable type, which is similar to int and double. To put it bluntly, it is to set an enumeration class variable class //The keyword of this class is Status. Variables defined in this type can only take a few values in the enum, and these values correspond to numbers. srand(static_cast<unsigned int>(time(0))); unsigned int myPoint = 0; Status gameStatus = CONTINUE; unsigned int sumOfDice = rollDice(); switch (sumOfDice) { case 7: case 11: gameStatus = WON; break; case 2: case 3: case 12: gameStatus = LOST; break; default: gameStatus = CONTINUE; myPoint = sumOfDice; cout << "Point is " << myPoint << endl; break; }; while (gameStatus == CONTINUE){ sumOfDice = rollDice(); if (sumOfDice == myPoint) gameStatus = WON; else if (sumOfDice == 7) gameStatus = LOST; } if (gameStatus == WON) cout << "Player wins" << endl; else cout << "Player lose" << endl; } unsigned int rollDice(){ unsigned int die1 = 1 + rand() % 6; unsigned int die2 = 1 + rand() % 6; unsigned int sum = die1 + die2; cout << "Player rolled: " << die1 << " + " << die2 << " = " << sum << endl; return sum; }
I used this as a note, mainly to remember this problem. When I want to implement the betting game in a loop and count the winning or losing, I will naturally think of putting a for loop on the outside to execute it, but this will involve a problem, namely, the generation of random numbers. According to my initial understanding, as the loop progresses, the different seeds provided to srand will be produced in each loop with different random sequences. That is the code as follows
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; unsigned int rollDice(); int main() { int winNum = 0; int loseNum = 0; enum Status {CONTINUE, WON, LOST}; //This is to customize a variable type, which is similar to int and double. To put it bluntly, it is to set an enumeration class variable class //The keyword of this class is Status. Variables defined in this type can only take a few values in the enum, and these values correspond to numbers. for (int i = 0; i < 100000; ++i) { srand(static_cast<unsigned int>(time(0))); unsigned int myPoint = 0; Status gameStatus = CONTINUE; unsigned int sumOfDice = rollDice(); switch (sumOfDice) { case 7: case 11: gameStatus = WON; break; case 2: case 3: case 12: gameStatus = LOST; break; default: gameStatus = CONTINUE; myPoint = sumOfDice; // cout << "Point is " << myPoint << endl; break; }; while (gameStatus == CONTINUE) { sumOfDice = rollDice(); if (sumOfDice == myPoint) gameStatus = WON; else if (sumOfDice == 7) gameStatus = LOST; } if (gameStatus == WON) { // cout << "Player wins" << endl; winNum++; } else { // cout << "Player lose" << endl; loseNum++; } } cout << "WIN: " << winNum << endl; cout << "LOSE: " << loseNum << endl; } unsigned int rollDice(){ unsigned int die1 = 1 + rand() % 6; unsigned int die2 = 1 + rand() % 6; unsigned int sum = die1 + die2; // cout << "Player rolled: " << die1 << " + " << die2 // << " = " << sum << endl; return sum; }
However, the statistical run results are:
WIN: 0
LOSE: 100000
In other words, once you lose the first time, you will always lose. Why?
The reason is that the program runs too fast, and the timestamp provided by time(0) is provided in seconds, and the program is executed within 1s, so the seed is naturally provided, and the sequence of rand is the same, so it keeps winning or losing. So what if I have to do is not win or lose? This way, set the number of loops to a larger extent, so that the program cannot be run within 1s, so can you provide different timestamps? And the fact is that this is also feasible. As shown in the figure, I set up a 1000w cycle.
WIN: 8703168
LOSE: 1296832
Of course, treating the symptoms but not the root cause, this still has not reached the random number that is unpredictable in every cycle. We have to learn other things at the moment. Let’s think about this issue when I have time~
The above is the detailed content of the sample code for implementing the dice roll game based on C++. For more information about the C++ dice roll game, please follow my other related articles!