Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

you use two dice. On the first roll of the dice the player immediately wins when

ID: 3623121 • Letter: Y

Question

you use two dice. On the first roll of the dice the player immediately wins when the roll is 7 or 11, and loses when the roll is 2, 3, or 12.   In this case the game is over – we know if the player has won or lost. (See below for code to doll a die.)

If 4, 5, 6, 8, 9, or 10 is rolled, that number becomes the “keyValue."   The game is not over yet.

To determine if the player has won or lost, the player must then roll the dice repeatedly until either a 7 or the keyValue is rolled.

If a 7 is rolled first, then the player wins.

If the player rolls the keyValue first, then the player loses. (There is a loop here.)

Write a program that plays the game using the rules stated above. There is no user input. Run it a few times watching the behavior in the debugger until you are convinced that it is playing by the rules.

The program should just determine if the player will win or lose.

After your program can play one game, add a loop so that it plays the game 20000 times. Add counters that count how many times the player wins (and where), and how many times the player loses. Post your answers on the Discussion board as soon as you get them.

3.1 After the 20000 games, compute the percentage of times the player will win.   Output this value.

3.2 What is the percentage of times the player will win on the first roll?      Output this value.

3.3 What is the percentage of times the player will win by rolling a 7? Output this value.

A percentage is between 0.0 and 100.0.       3.2 added to 3.3 should equal 3.1.

Explanation / Answer

#include #include #include main(){ int rollADie = 0, rollCounter = 0, key = 0, stop = 0, BigCounter = 20000, firstRollWins = 0, otherWins = 0, firstRollLoses = 0, otherLoses = 0, total,totalWins, totalLoses; double percentage; srand(time(NULL)); //call this only once - seed the random generator //beginning of main loop, clearing counters every time through loop for(BigCounter; BigCounter != 0; BigCounter--){ system("cls"); stop = 0; key = 0; rollADie = 0; rollCounter = 0; do{ //first roll rollADie += rand() % 6 + 1; //in the range of 1-6 rollCounter++; }while(rollCounter < 2); //storing first roll in key to be used in key rolls key = rollADie; printf("You rolled: %i ", key); //if first roll is 7 or 11 you win if(key == 7 || key == 11){ printf("You Win! "); firstRollWins++; } //if 2, 3 or 12 is not rolled on first roll keep rolling till key is rolled else if(key != 2 & key != 3 & key != 12){ //keep rolling till key is rolled while(stop != key){ rollADie = 0; rollCounter = 0; do{ rollADie += rand() % 6 + 1; //in the range of 1-6 rollCounter++; }while(rollCounter < 2); //keep storing the key in stop, it key is rolled stop = rollADie; printf("You rolled: %i ", stop); //you win when value in stop is equal to value in key first rolled if(stop == key){ printf("You Win! "); otherWins++; } //if you roll 7 now, you lose if(stop == 7){ printf("You Lose! "); otherLoses++; stop = key; } } } // if you roll 2, 3 or 12 on first roll, you lose else if(key == 2 || key == 3 || key == 12){ printf("You Lose! "); firstRollLoses++; //counts first roll loses } } system("cls"); //clear screan total = firstRollWins + otherWins + firstRollLoses + otherLoses; totalWins = firstRollWins + otherWins; totalLoses = firstRollLoses + otherLoses; printf("First roll loses: %i ", firstRollLoses); printf("Other loses: %i ", otherLoses); printf("First roll wins: %i ", firstRollWins); printf("Other wins: %i ", otherWins); //(Given amount/ Total amount) x100 calculates percentage percentage = ((double)totalWins / total) * 100; printf("Chances of winning: %.2lf percent ", percentage); percentage = ((double)firstRollWins / total) * 100; printf("Chances of winning on first roll: %.2lf percent ", percentage); percentage = ((double)otherWins / total) * 100; printf("Chances of winning on key roll: %.2lf percent ", percentage); system("pause");}