Question

Need it in C language
C Arrays Chapter 6 268 6.20 (Game of Craps) Write a program that runs 1000 games of craps (without human interven- tion) and

0 0
Add a comment Improve this question Transcribed image text
Answer #1


#include <stdio.h>
#include <stdlib.h>

enum game{Continue, Won, Lost};
// dice rollturn
int roll()
{
int sum=rand() % 7 + 1+rand() % 7 + 1;
return sum;
}
// display
void display(int won[], int lost[], int countwin, int countlose)
{

int count=countwin+countlose; // total loss and wons
int gamelength=0;
for(int i=0; i<21; i++) // run loop for 21 times
{
// display after 21 wins and loss
if(i==21)
{
printf("\n******Roll after 20 *****");
printf("\nGames won :: %d",won[20]);
printf("\tGames Lost :: %d",lost[20] );
}
// display before 21 wins and loss
else if(i<21)
{
printf("\n******Roll Turn Number %d *****",i+1);
printf("\nGames won :: %d" ,won[i]);
printf("\tGames Lost :: %d",lost[i] );
}
// calculate length of game
gamelength = gamelength + (won[i]*i+1 + lost[i]*i+1);
}
printf("\nChances of winning game :: %d" ,(countwin*100/count));
printf("\nThe average game length is %0.4f rolls.\n", ((float)gamelength/(float)count));

}
  
void gameofcraps(int won[], int lost[], int countwin, int countlose)
{
int total=0;
int points=0;
enum game g;
int rollturn;
// run 1000 times
for(int i=0; i<1000; i++)
{
rollturn=0;
total=roll();
// check win or loss
switch(total)
{
case 7:
case 11:g=Won;
break;
case 2:
case 3:
case 12:g=Lost;
break;
default:g=Continue;
points=total;
break;
  
}
// check if gams is to be Continued or not
while(g==Continue)
{
total=roll();
rollturn++;
if(total==points)
g=Won;
else if(total==7)
g=Lost;
}
// for turm >21
if(rollturn>=21)
rollturn=20;
if(g==Won)
{
won[rollturn]++;
countwin++;
}
else
{
lost[rollturn]++;

countlose++;
}

}
// call display
display(won, lost, countwin, countlose);

}
int main()
{
int won[21]={0};
int lost[21]={0};

int countwin=0;
int countlose=0;
gameofcraps(won, lost, countwin, countlose);

return 0;
}


Output

*Roll Turn Number 10 * ** Games Lost:: Games Won 6 8 k***Roll Turn Number 11 * **** Games Lost:: Games won 7 9 **Roll Turn Nu

Add a comment
Know the answer?
Add Answer to:
Need it in C language C Arrays Chapter 6 268 6.20 (Game of Craps) Write a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem 9 A single game of craps (a dice game) consists of at most two rolls...

    Problem 9 A single game of craps (a dice game) consists of at most two rolls of a pair of six sided dice. The ways to win are as follows: Win-the first roll of the pair of dice sums to either 7 or 1 (you win, game over, no second roll Win the first roll of the pair of dice does NOT sum to either 7 or 1 but the sum of the second roll is equal to the sum...

  • Write a c++ program that simulates a million of games in craps. I am having a...

    Write a c++ program that simulates a million of games in craps. I am having a trouble getting to loop a million of times. I am trying to use a const for a million. This is the code so far. #include <iostream> #include <cstdlib>// contains prototypes for functions srand and rand #include <ctime>// contains prototype for function time #include <iomanip> using namespace std; int rollDice(); // rolls dice, calculates and displays sum void printstats(); int totroll = 0, games, point,...

  • Java programming Write a simulation of the Craps dice game. Craps is a dice game that...

    Java programming Write a simulation of the Craps dice game. Craps is a dice game that revolves around rolling two six-sided dice in an attempt to roll a particular number. Wins and losses are determined by rolling the dice. This assignment gives practice for: printing, loops, variables, if-statements or switch statements, generating random numbers, methods, and classes. Craps game rules: First roll: The first roll (“come-out roll”) wins if the total is a 7 or 11. The first roll loses...

  • C++ programming language, starting out with C++. Looping Constructs Write a program to simulate the casino...

    C++ programming language, starting out with C++. Looping Constructs Write a program to simulate the casino game "craps". It should execute N number of games so that you can compute the probability(%) of the "player" winning and the "house" winning. Probability(%) of the "player" winning = PlayerWins / N * 100. Probability(%) of the "house" winning = HouseWins / N * 100. The rules are: Player rolls two dice. When the sum is 7 or 11 on first throw, player...

  • Craps

    Write a C++ game that plays Craps. Craps is a game played with a pair of dice. The shooter (the player with the dice) rolls a pair of dice and the number of spots showing on the two upward faces are added up. If the opening roll (called the ‘come out roll’) is a 7 or 11, the shooter wins the game. If the opening roll results in a 2 (snake eyes), 3 or 12 (box cars), the shooter loses,...

  • C# Code: Write the code that simulates the gambling game of craps. To play the game,...

    C# Code: Write the code that simulates the gambling game of craps. To play the game, a player rolls a pair of dice (2 die). After the dice come to rest, the sum of the faces of the 2 die is calculated. If the sum is 7 or 11 on the first throw, the player wins and the game is over. If the sum is 2, 3, or 12 on the first throw, the player loses and the game is...

  • Please write the program in python: 3. Design and implement a simulation of the game of...

    Please write the program in python: 3. Design and implement a simulation of the game of volleyball. Normal volleyball is played like racquetball, in that a team can only score points when it is serving. Games are played to 15, but must be won by at least two points. 7. Craps is a dice game played at many casinos. A player rolls a pair of normal six-sided dice. If the initial roll is 2, 3, or 12, the player loses....

  • Tic-Tac-Toe (arrays and multidimensional arrays) C++ Write an interactive program that plays tic-tac-toe. Represent the board...

    Tic-Tac-Toe (arrays and multidimensional arrays) C++ Write an interactive program that plays tic-tac-toe. Represent the board as a 3 X 3 character array. Initialize the array to blanks and ask each player in turn to input a position. The first player's position is marked on the board with a O and the second player's position is marked with an X. Continue the process until a player wins or the game is a draw. To win, a player must have 3...

  • Required in JAVA language Write a program that enables a user to play number guessing games....

    Required in JAVA language Write a program that enables a user to play number guessing games. The following is a nutshell description of a number guessing game. + a random number is generated + loop prompting the user to enter guesses until the user guesses the number or hits the maximum number of allowed guesses or enters the "quit" sentinel value The rest of this specification documents number guessing games in more detail. The documentation uses manifest constants that can...

  • Assignment 2 – The two player game of Poaka The game of Poaka is a dice...

    Assignment 2 – The two player game of Poaka The game of Poaka is a dice game played by two players. Each player takes turns at rolling the dice. At each turn, the player repeatedly rolls a dice until either the player rolls a 1 (in which case the player scores 0 for their turn) or the player chooses to end their turn (in which case the player scores the total of all their dice rolls). At any time during...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT