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...

  • 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....

  • 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...

  • (C++) Please create a tic tac toe game. Must write a Player class to store each...

    (C++) Please create a tic tac toe game. Must write a Player class to store each players’ information (name and score) and to update their information (increase their score if appropriate) and to retrieve their information (name and score).The players must be called by name during the game. The turns alternate starting with player 1 in the first move of round 1. Whoever plays last in a round will play second in the next round (assuming there is one).The rows...

  • Please, I need help with program c++. This is a chutes and ladders program. The code...

    Please, I need help with program c++. This is a chutes and ladders program. The code must be a novel code to the specifications of the problem statement. Thank you very much. Assignment Overview This program will implement a variation of the game “chutes and ladders” or “snakes and ladders:” https://en.wikipedia.org/wiki/Snakes_and_Ladders#Gameplay. Just like in the original game, landing on certain squares will jump the player ahead or behind. In this case, you are trying to reach to bottom of the...

  • C or C++ Project Overview Wild, Wild, West! Dice Game The Wild, Wild, West! is an...

    C or C++ Project Overview Wild, Wild, West! Dice Game The Wild, Wild, West! is an elimination dice game. It can be played by any number of players, but it is ideal to have three or more players. Wild, Wild, West! Requires the use of two dice. At the start of the game, each player receives a paper that will track the number of lives that player has. Each player starts the game with 6 lives. In the first round,...

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
ADVERTISEMENT