craps game

hey there, i wrote this code for the craps game which gives just select its own numbers and chooses if the win or looses.

but i want the main program to read the return values from the craps function and keep track of the win frequency. the craps function should return 0 for a loss and 1 for a win. the main function should count the number of attempts and the number of wins. Win percentage is the ratio of wins to tries times 100. The output should be a floating point number so you will need a static_cast operator if you count in ints.

The process of iteratively calling simulations of random processes is called Monte Carlo after the casinos on the French Riviera. Random processes are processes whose outcomes are unpredictable at the start of the processes. The study of random processes arose from calculating the odds of gambling processes and creating games which gave the illusion of fairness while guaranteeing the house would take a percentage over time.

In order to ensure that the iterative processes are truly random you must seed your random number generator properly. This is done using the “srand( time(0) );” command. If the seeding is done in the craps function and the craps function is called more than once in any given second the craps function will generate the same sequence repeatedly. This problem is cured by giving the srand command a larger scope and only executing it once in the main function before any calls to the craps function.

The final question you must answer is “What are the true odds of winning at the craps game you have just programmed?”. If you run a limited number of cycles, say 10, of the game you may win or you may lose. This is the nature of the randomness of the game. To see how random the process is simply run the programs multiple times with the same number of cycles each time. To determine an accurate number you must run the simulation many times. The standard of repeatability for your process will be 10 repeated tests with results that vary no more the 0.2% from each other.
1. the program must accept an input of the number of cycles to execute.
2. Input 100000000. That’s 100,000,000
3. For this program you will start with 10 cycles and increase by powers of 10 until the win percentage converges or until you reach the limit of 100000000.
4. the program must execute the number of cycles and compute the win percentage
5. You must display the win percentage to at least 3 decimal places in tabular form
6. Use setw(9) for the count column.

7. Create a craps function.
a. Provide a prototype for function craps.
b. Return win or lose value from function.
8. Modify main function to call craps function multiple times and record win/lose statistics
a. Move “srand” command to main.
b. Write nested loops to conduct tests
c. For ( i = 10; i < cycles; i*=10)
d. Call the craps function inside th loop.
e. Count wins and tries.
f. Calculate win percentage after exiting loop.
9. Compile
10. Debug by running with cout statements for diagnostics.
11. Test
a. Comment out the cout statements in your code and recompile.


can someone help me how to develope this code to that using these instructions, i want to learn so much how to do this. thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  #include <iostream>
#include <cstdlib>
#include <ctime>


using namespace std;

int rollDice();

int main()
{
    enum Status { CONTINUE, WON, LOST};

    int myPoint;
    Status gameStatus;

    srand( time( 0 ));
    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 loses" << endl;
}

int rollDice()
{
    int die1 = 1 + rand() % 6;
    int die2 = 1 + rand() % 6;

    int sum = die1 + die2;

    cout << " player rolled " << die1 << " + " << die2
<< " = " << sum << endl;

    return sum;
}
Last edited on
closed account (48T7M4Gy)
The way to tackle this is to add to the code for each item in turn. For instance item 1 requires you to add two lines:

1
2
int no_of_cycles = 0;
cin >> no_of_cycles;


Then just add the lines to appropriate places, test and debug then move to the next question. Given that your code is pretty good so far I don't expect too much drama.

You will have learned a lot by the time you get to the end of no. 7

Cheers :)
Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/general/175045/
Hey kemort, thank you mate for your explanation and help. It might be easy for you to follow those instructions, but I am very poor in conceptual knowledge, because it has been only a month since I started to learn this. I need someone to explain or teach me how to follow those instructions. And yeah I posted it in the other forum too, I thought it would be ok to do it. Sorry mate.
Cheers:)
Don't duplicate posts please. It disrupts the communities way of helping people who actually need it...
Topic archived. No new replies allowed.