1. #### In Chapter 1, you created a class called RandomGuess. In this game, players guess a number, the application generates a random number, and players determine whether they were correct. In Chapter 5, you improved the application to display a message indicating whether the player’s guess was correct, too high, or too low. Now, add a loop that continuously prompts the user for the number, indicating whether the guess is high or low, until the user enters the correct value. After the user correctly guesses the number, display a count of the number of attempts it took. Save the file as RandomGuess3.java.

import javax.swing.JOptionPane; public class RandomGuess3 { public static void main(String[] args) { int guess; int result; String msg; int attempts = 1; final int LOW = 1; final int HIGH = 10; result = LOW + (int)(Math.random() * HIGH); guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Try to guess my number between " + LOW + " and " + HIGH)); while(guess != result) { if(guess < result) msg = "Your guess was too low"; else msg = "Your guess was too high"; guess = Integer.parseInt(JOptionPane.showInputDialog(null, msg + "\nGuess again")); ++attempts; } JOptionPane.showMessageDialog(null,"Correct! You got it in " + attempts + " attempts"); } }
2. #### In Chapter 4, you created a Die class from which you could instantiate an object containing a random value from 1 through 6. Now use the class to create a simple dice game in which the user chooses a number between 2 (the lowest total possible from two dice) and 12 (the highest total possible). The user “rolls” two dice up to three times. If the number chosen by the user comes up, the user wins and the game ends. If the number does not come up within three rolls, the computer wins. Save the application as TwoDice3.java.

import javax.swing.*; public class TwoDice3 { public static void main(String[] args) { final int HIGH = 12; final int LOW = 2; int guess = 0; int total = 0; int attempts = 0; boolean isDone = false; final int MOST_ATTEMPTS = 3; String msg; while(guess < LOW || guess > HIGH) { guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Choose a number between " + LOW + " and " + HIGH)); } while(!isDone) { Die firstDie = new Die(); Die secondDie = new Die(); int value1 = firstDie.getValue(); int value2 = secondDie.getValue(); total = value1 + value2; attempts = attempts + 1; msg = "Roll " + attempts + ": " + value1 + " and " + value2 + "\nTotal: " + total; if(guess == total) { JOptionPane.showMessageDialog(null, msg + "\nYou win!"); isDone = true; } else { if(attempts == MOST_ATTEMPTS) { JOptionPane.showMessageDialog(null, msg + "\nLast try - you lose"); isDone = true; } else JOptionPane.showMessageDialog(null, msg + "\nClick OK to roll again"); } } } }
1. #### a. Using the Die class you created in Chapter 4, create a version of the dice game Pig that a user can play against the computer. The object of the game is to be the first to score 100 points. The user and computer take turns rolling a pair of dice following these rules: • On a turn, each player “rolls” two dice. If no 1 appears, the dice values are added to a running total, and the player can choose whether to roll again or pass the turn to the other player. • If a 1 appears on one of the dice, nothing more is added to the player’s total and it becomes the other player’s turn. • If a 1 appears on both of the dice, not only is the player’s turn over, but the player’s entire accumulated score is reset to 0. • In this version of the game, when the computer does not roll a 1 and can choose whether to roll again, generate a random value between 0 and 1. Have the computer continue to roll the dice when the value is 0.5 or more, and have the computer quit and pass the turn to the player when the value is not 0.5 or more. Save the game as PigDiceGame.java.

import javax.swing.*; public class PigDiceGame { public static void main(String[] args) { final int WINNING_SCORE = 100; int computerTotal = 0; int playerTotal = 0; int selection = JOptionPane.YES_OPTION;; boolean isDone = false; boolean computerChoosesToGoOn = true; String msg; String msg2 = "\nDo you want to roll the dice?"; while(!isDone) { while(selection == JOptionPane.YES_OPTION) { Die firstDie = new Die(); Die secondDie = new Die(); int value1 = firstDie.getValue(); int value2 = secondDie.getValue(); if(value1 == 1 && value2 == 1) { playerTotal = 0; selection = JOptionPane.NO_OPTION; msg2 = "\nYou rolled two 1s and cannot go on"; } else if(value1 != 1 && value2 !=1) { playerTotal = playerTotal + value1 + value2; msg2 = "\nYou can go on"; } else { selection = JOptionPane.NO_OPTION; msg2 = "\nYou rolled a 1 and cannot go on"; } if(playerTotal >= WINNING_SCORE) { msg2 = "\nYou win!"; isDone = true; computerChoosesToGoOn = false; selection = JOptionPane.NO_OPTION; } else { computerChoosesToGoOn = true; } JOptionPane.showMessageDialog(null, "Your dice are " + value1 + " and " + value2 + "\nYour total is " + playerTotal + msg2); if(selection == JOptionPane.YES_OPTION) selection = JOptionPane.showConfirmDialog(null, "\n(Your total is " + playerTotal + ")" + "\nDo you want to roll the dice?"); } selection = JOptionPane.YES_OPTION; while(computerChoosesToGoOn) { msg = "\nThe computer chooses to roll"; Die firstDie = new Die(); Die secondDie = new Die(); int value1 = firstDie.getValue(); int value2 = secondDie.getValue(); if(value1 == 1 && value2 == 1) { computerTotal = 0; computerChoosesToGoOn = false; msg = "\nComputer rolled two 1's and cannot go on"; } else if(value1 != 1 && value2 !=1) computerTotal = computerTotal + value1 + value2; else { computerChoosesToGoOn = false; msg = "\nComputer rolled a 1 and cannot go on"; } if(computerTotal >= WINNING_SCORE) { msg = "\nComputer wins!"; isDone = true; } else { if(computerChoosesToGoOn) { double compChoice = Math.random(); computerChoosesToGoOn = (compChoice >= 0.50); if(!computerChoosesToGoOn) msg = "\nThe computer chooses to not roll again"; } } JOptionPane.showMessageDialog(null, "The computer's dice are " + value1 + " and " + value2 + "\nThe computer total is " + computerTotal + msg); } } } }
2. #### Modify the PigDiceGame application so that if a player rolls a 1, not only does the player’s turn end, but all the player’s earned points during that round are eliminated. (Points from earlier rounds are not affected. That is, when either the player or computer rolls a 1, all the points accumulated since the other’s turn are subtracted.) Save the game as PigDiceGame2.java.

import javax.swing.*; public class PigDiceGame2 { public static void main(String[] args) { final int WINNING_SCORE = 100; int computerTotal = 0; int playerTotal = 0; int selection = JOptionPane.YES_OPTION;; boolean isDone = false; boolean computerChoosesToGoOn = true; String msg; String msg2 = "\nDo you want to roll the dice?"; int pointsThisRoundPlayer = 0, pointsThisRoundComputer = 0; while(!isDone) { while(selection == JOptionPane.YES_OPTION) { Die firstDie = new Die(); Die secondDie = new Die(); int value1 = firstDie.getValue(); int value2 = secondDie.getValue(); if(value1 == 1 && value2 == 1) { playerTotal = 0; pointsThisRoundPlayer = 0; selection = JOptionPane.NO_OPTION; msg2 = "\nYou rolled two 1s and cannot go on"; } else if(value1 != 1 && value2 !=1) { playerTotal = playerTotal + value1 + value2; pointsThisRoundPlayer = pointsThisRoundPlayer + value1 + value2; msg2 = "\nYou can go on"; } else { selection = JOptionPane.NO_OPTION; msg2 = "\nYou rolled a 1 and cannot go on\nSubtracting this round's " + pointsThisRoundPlayer + " points"; playerTotal = playerTotal - pointsThisRoundPlayer; pointsThisRoundPlayer = 0; } if(playerTotal >= WINNING_SCORE) { msg2 = "\nYou win!"; isDone = true; computerChoosesToGoOn = false; selection = JOptionPane.NO_OPTION; } else { computerChoosesToGoOn = true; } JOptionPane.showMessageDialog(null, "Your dice are " + value1 + " and " + value2 + "\nYour total is " + playerTotal + msg2); if(selection == JOptionPane.YES_OPTION) selection = JOptionPane.showConfirmDialog(null, "\n(Your total is " + playerTotal + ")" + "\nDo you want to roll the dice?"); } pointsThisRoundPlayer = 0; selection = JOptionPane.YES_OPTION; while(computerChoosesToGoOn) { msg = "\nThe computer chooses to roll"; Die firstDie = new Die(); Die secondDie = new Die(); int value1 = firstDie.getValue(); int value2 = secondDie.getValue(); if(value1 == 1 && value2 == 1) { computerTotal = 0; computerChoosesToGoOn = false; msg = "\nComputer rolled two 1's and cannot go on"; } else if(value1 != 1 && value2 !=1) { computerTotal = computerTotal + value1 + value2; pointsThisRoundComputer = pointsThisRoundComputer + value1 + value2; } else { computerChoosesToGoOn = false; computerTotal = computerTotal - pointsThisRoundComputer; msg = "\nComputer rolled a 1 and cannot go on" + "\nSubtracting " + pointsThisRoundComputer + " points earned this round"; pointsThisRoundComputer = 0; } if(computerTotal >= WINNING_SCORE) { msg = "\nComputer wins!"; computerChoosesToGoOn = false; isDone = true; } else { if(computerChoosesToGoOn) { double compChoice = Math.random(); computerChoosesToGoOn = (compChoice >= 0.50); if(!computerChoosesToGoOn) { msg = "\nThe computer chooses to not roll again"; pointsThisRoundComputer = 0; } } } JOptionPane.showMessageDialog(null, "The computer's dice are " + value1 + " and " + value2 + "\nThe computer total is " + computerTotal + msg); } pointsThisRoundComputer = 0; } } }
3. #### Two people play the game of Count 21 by taking turns entering a 1, 2, or 3, which is added to a running total. The player who adds the value that makes the total reach or exceed 21 loses the game. Create a game of Count 21 in which a player competes against the computer, and program a strategy that always allows the computer to win. On any turn, if the player enters a value other than 1, 2, or 3, force the player to reenter the value. Save the game as Count21.java.

import javax.swing.JOptionPane; public class Count21 { public static void main(String[] args) { int player; int computer; String msg; int total = 0; final int FACTOR = 4; final int HIGH = 3; final int LOSING_VAL = 21; final String inputMsg = "\nInput a number between 1 and " + HIGH; player = Integer.parseInt(JOptionPane.showInputDialog(null, inputMsg)); while(player < 1 || player > HIGH) player = Integer.parseInt(JOptionPane.showInputDialog(null, "Number must be between 1 and " + HIGH + "\nPlease reenter")); total += player; while(total < LOSING_VAL) { msg = "You entered " + player + " bringing the total to " + total; if(player == 1) computer = 3; else if(player == 2) computer = 2; else computer = 1; total += computer; msg += "\nComputer adds " + computer + " bringing total to " + total; msg += inputMsg; player = Integer.parseInt(JOptionPane.showInputDialog(null, msg)); while(player < 1 || player > HIGH) player = Integer.parseInt(JOptionPane.showInputDialog(null, "Number must be between 1 and " + HIGH + "\nPlease reenter")); total += player; } JOptionPane.showMessageDialog(null,"You brought the total to " + total + "\nSorry - you lose."); } }