Question: IN JAVA LANGUAGE PLEASE problem: Write a program to play the pig game against the computer. At each turn, the current player will roll a
IN JAVA LANGUAGE PLEASE
problem:
Write a program to play the pig game against the computer. At each turn, the current player will roll a pair of dice and accumulates points. The goal is to reach to 100 or more points before your opponent does. (For the testing purposes use 30 instead of 100 points) If, on any turn, the player rolls a 1, all the points accumulated for that round are forfeited and the control of the dice moves to the other player. If the player rolls two 1s in one turn, the player loses all the points accumulated thus far are forfeited and the control moves to the other player. The player may voluntarily turn over the control of the dice after each roll. Therefore player must decide to roll again (be a pig) and risk losing points, or relinquish control of the dice, possibly allowing the other player to win. Computer is going to flip a coin to choose the first player
my code so far: i dont know how to do all the steps in play method please fix those
import java.util.*; public class PigGame { public static final int POINT = 30; public static final int FORFEIT_POINTS = 20; public static void main(String[] args) { // Main method: Calls the method play, keep playing the game as long as there are more players. Scanner kb = new Scanner(System.in); Random rand = new Random(); Description(); start(kb); } //1 //description of the game public static void Description() { System.out.println("***********************************************************************************"); System.out.println("* You are about to play the pig game against the computer. *"); System.out.println("* On each turn, the current player will roll a pair of dice *"); System.out.println("* and acumulates points. The goal is to reach to40 points *"); System.out.println(); System.out.println(); System.out.println(); System.out.println(); System.out.println(); System.out.println(); System.out.println(); } //2 //method that accept a scanner object as its parameter //promots user to enter "yes" or "no" //as long as the user is not entering a valid input prompt the user again public static void start (Scanner kb) { System.out.print("yes or no?"); String answer = kb.next(); kb.nextLine(); if (answer.equalsIgnoreCase("yes")) { Play(kb,rand); } if (answer.equalsIgnoreCase("no")) { System.out.println("end of game, thanks for playing"); } else { System.out.println("invalid "); start(kb); } } //6. Play: this method calls the other methods to play the game public static void Play(Scanner kb,Random rand) { /* STEP1: Declare all the needed variables to keep track of the scores for each player, and Boolean variables to indicate who is playing at the moment. STEP2: Ask the users name STEP3: Decide who start the game first by calling one of the methods you created to flip the coin. STEP4: Write conditional statements to switch the game between the computer and the player based on the dice rolled and overall points. Read the output and the program description to figure out the conditions. You need to use couple while loops: one loop for the human player, one loop for the computer player. The previous two loops will be nested in another while loop to witch the game between the two players. declaring your variables While (there is no winner) { some codes While (human is playing) { some codes, conditional statements } you may need some codes While (computer is playing) { some codes, conditional statements } you may need some codes } STEP5: keep playing the game until the player or the computer has 100 or more points (make it 40 for simplicity) you are not allowed to hardcode anything */ String comp = Flip(rand); String compName = RandomName(rand); int points = 0; System.out.println("Hi my name is " + compName); System.out.print("what is your name? "); String name = kb.next(); kb.nextLine(); System.out.println("Hi " + name + ", I am filling the coid to determine who will go first"); System.out.println("Press any key to start the game"); int dice = PairOfDice(rand); System.out.println(dice); } //3 //flip the coin // this method accepts a random object and returns "head" or "tails" //based on the random number that was generated public static String Flip(Random rand) { int FlipCoin = rand.nextInt(2); String computer = ""; switch (FlipCoin) { case 0: computer = "heads"; break; case 1: computer = "tails"; break; } return computer; } //4 //roll two dice //thi method accepts a random object //generates two random number representing one of the numbers of a dice //returns the sum of the dice public static int PairOfDice (Random rand) { int dice1; dice1 = RandDice(rand); int dice2; dice2 = RandDice(rand); System.out.println("Dice 1: " +dice1); System.out.println("Dice 2: " +dice2); int finalDice = dice1 + dice2; return finalDice; } public static int RandDice(Random rand) { int randdice = rand.nextInt(); int dice = 0 ; switch(randdice) { case 0: dice = dice +1; break; case 1: dice = dice +2; break; case 2: dice = dice +3; break; case 3: dice = dice +4; break; case 4: dice = dice +5; break; case 5: dice = dice +6; break; } return dice; } //5 //choose a name for the computer //come up with 10 different name for the computer //than select a random name from the list that you created //return the selected name public static String RandomName(Random rand) { int randname = rand.nextInt(10); String computerName = ""; switch (randname) { case 0: computerName = "Lisa"; break; case 1: computerName = "Kathy"; break; case 2: computerName = "Hali"; break; case 3: computerName = "Jack"; break; case 4: computerName = "Alex"; break; case 5: computerName = "Max"; break; case 6: computerName = "Jill"; break; case 7: computerName = "James"; break; case 8: computerName = "Martha"; break; case 9: computerName = "Lauren"; break; } return computerName; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
