Question

In: Computer Science

Objective Make a function to serve as a Craps dice game simulator. If you are not...

Objective

Make a function to serve as a Craps dice game simulator. If you are not familiar Craps is a game that involves rolling two six-sided dice and adding up the total. We are not implementing the full rules of the game, just a function that returns the value of BOTH dice and their total.

Details

In the function you create: Make a function that uses pointers and pass by reference to return THREE separate outputs (arrays are not allowed). Inside the function you will call rand() to represent a random dice roll of a six sided dice. One output will be the first call to rand() and represent the first dice roll. The second output will be a second call to rand() and represents the second dice roll. The third output is the total of the two other outputs added together.

You may choose either options for defining the function:

Option A:

  • RETURN: void function
  • PARAMETERS: Three variables that use pass by reference. Two of them are set to the dice rolls within the function. The third is the total of the two dice rolls added together.

Option B:

  • RETURN: int value - Total of the two dice rolls.
  • PARAMETERS: Two variables that use pass by reference, each one is set to a random dice roll.

In the Main Function: Call srand() to initialize the pseudo-random algorithm. Create three variables to hold your outputs, the two dice rolls and the total. In a for loop call your dice roll function FIVE times, each time it should set the three variables to new values. Also within the for loop print the values of the dice rolls and the total.

  • No input is required from the user this time. Everything that is output is done from the random function.
  • Add #include <time.h> to your code.
  • In the main function the call to srand looks like:
    • srand(time(NULL));
  • In the dice roll function the call to represent a six sided dice looks like:
    • rand() % 6 + 1
  • Do not just call printf from within the dice roll function! Demonstrate that you can receive multiple outputs from the same function

Solutions

Expert Solution

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

void dice_roll(int *roll_1, int *roll_2, int *total);

int main(void) 
{
  // Initialize srand function
  srand(time(NULL));
  // Create three variables to hold your outputs
  int roll_1, roll_2, total;

  //  In a for loop call your dice roll function FIVE times
  for(int i=0; i<5; i++)
  {
    // call dice_roll each time to set the three variables to new values
    dice_roll(&roll_1, &roll_2, &total);
    // Print the result
    printf("Roll 1: %d, Roll 2: %d, Total: %d\n", roll_1, roll_2, total);
  }
  
  return 0;
}

// Dice roll function
void dice_roll(int *roll_1, int* roll_2, int* total)
{
  // Generate a random number between  1 and 6 and assign to roll_1 and roll_2
  *roll_1 = rand() % 6 + 1;
  *roll_2 = rand() % 6 + 1;
  // Add the two roll to get the total
  *total = *roll_1 + *roll_2;
}



*******************************************OUTPUT******************************************

Roll 1: 5, Roll 2: 1, Total: 6
Roll 1: 2, Roll 2: 5, Total: 7
Roll 1: 6, Roll 2: 5, Total: 11
Roll 1: 6, Roll 2: 3, Total: 9
Roll 1: 5, Roll 2: 4, Total: 9



Related Solutions

Craps is a dice game in which the players make wagers on the outcome of the...
Craps is a dice game in which the players make wagers on the outcome of the roll, or a series of rolls, of a pair of dice. Most outcomes depend on the sum of the up faces of two, fair, six-sided dice. 1) What is the lower class boundary of the "6" class? 2) Find the mean. 3) Find the standard deviation. 4) Find the z-score for snake eyes.
This problem concerns the dice game craps. On the first roll of two dice, you win...
This problem concerns the dice game craps. On the first roll of two dice, you win instantly with a sum of 7 or 11 and lose instantly with a roll of 2,3, or 12. If you roll another sum, say 5, then you continue to roll until you either roll a 5 again (win) or roll a 7 (lose). How do you solve for the probability of winning?
Write a C++ program to play the dice game "Craps". Craps is one of the most...
Write a C++ program to play the dice game "Craps". Craps is one of the most popular games of chance and is played in casinos and back alleys throughout the world. The rules of the game are straightforward: A player rolls two dice. Each die has six faces. These faces contain 1, 2, 3, 4, 5, and 6 spots. After the dice have come to rest, the sum of the spots on the two upward faces is calculated. If the...
In the game of Craps, you roll two dice. When you bet on a “snake eyes”,...
In the game of Craps, you roll two dice. When you bet on a “snake eyes”, meaning a 1 on both dice, you win $30 for each $1 you bet. Otherwise, you lose your dollar. What is the probability of winning this bet? What is the expected value of making this bet? If you play this game 100 times, how much would you expect to lose?
Chicago is a group dice game that requires no skill. The objective of the game is...
Chicago is a group dice game that requires no skill. The objective of the game is to accumulate points by rolling certain combinations (GamezBuff, 2017). How do you play Chicago? There are eleven rounds in the game, one for each combination that can be made by adding two dice, namely the numbers two through 12. Each round has a target combination starting with two and going up all the way to 12. Going clockwise, the players take turns to roll...
Chicago is a group dice game that requires no skill. The objective of the game is...
Chicago is a group dice game that requires no skill. The objective of the game is to accumulate points by rolling certain combinations (GamezBuff, 2017). How do you play Chicago? There are eleven rounds in the game, one for each combination that can be made by adding two dice, namely the numbers two through 12. Each round has a target combination starting with two and going up all the way to 12. Going clockwise, the players take turns to roll...
Create a dice simulator in C#. The simulator must roll the dice 6 times and it...
Create a dice simulator in C#. The simulator must roll the dice 6 times and it must add the 3 highest rolls. So say it simulates: 4 4 5 2 1 3 the total would be 13 (4+4+5=13). Repeat this 6 different times. So if the random dice simulator simulates the outcomes below it should output: 13, 13, 14, 12, 16, 12 1) 4 4 5 2 1 3 2) 2 3 6 1 3 4 3) 5 3 1...
In the game of craps, a player (known as the shooter) rolls two fair six-sided dice....
In the game of craps, a player (known as the shooter) rolls two fair six-sided dice. The shooter immediately loses if the sum of the dice is 2, 3, or 12 and immediately wins if the sum of the dice is 7 or 11 on the first roll. If the sum is anything else (4, 5, 6, 8, 9, or 10), that number becomes the point and the shooter rolls again. The shooter now wins by rolling that same point...
In the game of craps, a pass line bet proceeds as follows: Two six-sided dice are...
In the game of craps, a pass line bet proceeds as follows: Two six-sided dice are rolled; the first roll of the dice in a craps round is called the “come out roll.” A come out roll of 7 or 11 automatically wins, and a come out roll of 2, 3, or 12 automatically loses. If 4, 5, 6, 8, 9, or 10 is rolled on the come out roll, that number becomes “the point.” The player keeps rolling the...
zyDE 9.16.1: Dice game Craps. Modify the playerWins() method to update highCredits. The current solution does...
zyDE 9.16.1: Dice game Craps. Modify the playerWins() method to update highCredits. The current solution does not work correctly. Increment numTurns in the most appropriate method. The current solution does not work correctly. As a result, the percentage of wins calculation attempts to divide by zero. // Simulates a dice game called Craps import java.util.Random; import java.text.DecimalFormat; import java.util.Scanner; public class Craps { // Game statistics private int diceTotal; private int credits; private int numTurns; private int numWins; private int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT