PROBLEM

You are to implement a simulation of the game of Craps utilizing the State design pattern. Players (shooters) rotate rounds, in which a round is played by a single shooter. A round is played in two stages the come out phase and the point phase. A player may win, lose, or establish point in the come outphase. If an establish point occurs in this phase, then the game proceeds to the point phase. The possible outcomes during play of the game is summarized below.

Come Out Phase

  • roll of 2, 3 or 12 (craps, i.e., lose)
  • roll of 7 or 11 (natural, i.e., wins)
  • roll of 4,5,6,8,9, or 10 (establishes point, i.e., must roll this number again before rolling 7)

Point Phase

  • roll of established point before rolling a 7 (player hits the point, i.e., wins)
  • roll of 7 (seven out, i.e., loses)
  • roll of anything other than point or 7, rolls again

SCENARIO

In the actual game of Craps, the rounds rotate between any number of players. We will write a program that simulates the playing of rounds for two players, the player and the computer. Given that the rolling of dice will be performed by a call to a random number generator, the only difference between actions of the program for a player vs. the computer is that the player gets to indicate how much they wish to bet, while the computer will randomly generate its bet. At the start of the program, the number of rounds to be played is entered. The program then displays all of the outcomes of play, tallying the total winnings for both the player and computer.

APPROACH

Following the State design pattern to implement the program. The context would be the current point to match in the second Point phase (if a 4,5,6,8,9 or 10 is rolled in the come out phase). In the come out phase, there is not a point value to store, so it can be set to 0 in the context. Thus, the point context in the this problem is similar to the CustomerAccount object of the State pattern given in class (and in the lecture notes). And whereas in the banking problem each state implemented the appropriate actions after a deposit or withdrawal, each state in this problem would simulate what should occur after each roll.

EXAMPLE OUTPUT

State of game ...
How many rounds would you like to play? 2


Here we go, you are shooting ...
How much would you like to wager this round? 10
I wager $20 (computer)

roll of 5 (point) roll of 6 roll of 8 roll of 7 (craps)

Now its my turn ... roll of 2 (craps)

Your current winnings: -$10
My current winnings: -$20

------------

Here we go, you are shooting ...
How much would you like to wager this round? $20
I wager $15

roll of 7 (natural - win)

Now its my turn ... roll of 9 (point) roll of 6 roll of 8
roll of 9 (hit the point - win)

Your current winnings: $10
My current winnings: -$5

Game Over – Humans Rule!
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.