Introduction
This player asks you to write a two player game. The basic idea is that each of the two players wager on rolls of the dice. Two dice are rolled per player. The player with the greatest (sum) total roll wins. The dice are rolled one at a time. Players get the opportunity to bet before any rolls and after each player has rolled exactly once. But, the bet is cumulative -- it is won or lost based on the two dice taken together, not each individual roll.The game ends when either player chooses to leave or when either player runs out of money. The game always runs between rounds. If a player runs out of money after the first bet, s/he simply must bet 0 on the next round. Players can't wager more than they have.
Sample Interaction
Below is a sample run of the game:
Welcome to Dice Player 1: How much money do you bring to the table? 55.00 Player 2: How much money do you bring to the table? 35.00 Round 1 Player 1: Your balance is $55.00 Player 2: Your balance is $35.00 Player 1: How much do you wager on the first roll? 5.50 Player 2: How much do you wager on the first roll? 9.50 Player 1: Rolls 3 Player 2: Rolls 5 Player 1: How much do you wager on the second roll? 1.00 Player 2: How much do you wager on the second roll? 20.00 Player 1: Rolls 6 Player 2: Rolls 1 Player 1: Total rolled is 9 Player 2: Total rolled is 6 Player 1 wins $29.50 Player 1: Continue (Y/N)? Y Player 2: Continue (Y/N)? Y Round 2: Player 1: Your balance is $25.50 Player 2: Your balance is $64.50 Player 1: How much do you wager on the first roll? 30.00 Player 1: Sorry. You don't have that much. Player 1: How much do you wager on the first roll? ...skipping ahead...one possible ending Player 1: Continue (Y/N)? Y Player 2: Continue (Y/N)? N Game over -- Player 1 left. ...an alternative ending... Game over -- Player 2 has no money left on the table.
Required Methods
Your program can make use of as many methods as you'd like. But, minimally, it must implement and make good use of those listed below.
- getTableAmount(...)
This method should accept a player number. It should prompt the user for an amount of money. If this amount is non-negative, it should reutrn this amount. If it is negative, it should print, "Please enter a non-negative amount" and repeat. Once it has accepted a non-negative amount, it should return this amount. Pleae note the presence of dollars and cents within the example above.
- getWager(...)
This method should accept a player number. It should also accept the player's "roll 1" wager amount (pass in a 0 if roll 1) and present amoutn of money on the table.It should prompt the user for the new wager amount. This wager amount must be non-negative. It should print, "Please enter a non-negative amount" and repeat if the value is negative. If the total wager present wager plus prior wager, exceeds the amount of money on the table, it should print, "You don't have that much on the table. Please enter a smaller wager." and repeat. At the end, it should return a viable wager.
- playAgain(...)
This method should accept both player's balances as arguments. It should return false if either one is 0.00. If this isn't the case, it should ask each player if s/he wants to. If either player wants to stop, it should return false. Otherwsie, it should return true.
- getRoll(...)
This method should simualte a roll of the dice, returning a random whole number between 1 and 3, inclusive. Please see the section below about the Random class for hints.
The Random Library
Java includes a class library, Random, to support random numbers. To use this library, you need to import "java.io.util.*"To use the Random, you should first create a new generator, as follows:
Random generator = new Random();Next, you should ask it for a random number, as follows:
int randomNUmber = generator.nextInt(6) + 1;This works becuase nextInt(6) requests a random number in the range [0,6), in other words from 0 (inclusive) through, but not including 6. Since dice have six sides, we want to add 1 to what it gives us, so that the result is [1,6], in other words 1 (inclusive) - 6 (inclusive).