// Plays the dice game Strike Out import java.util.Scanner; public class StrikeOutGame { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.println("Welcome to the STRIKE OUT game"); int strikeOutSum; // Asks the user for an integer in the range [1-6] and // repeats until the user does so. // Assumes the user always enters an integer do { System.out.print("Please enter an integer (1-6): "); strikeOutSum = keyboard.nextInt(); if (strikeOutSum < 1 || strikeOutSum > 6) System.out.println("Error: Bad input"); } while (strikeOutSum < 1 || strikeOutSum > 6); Die die1 = new Die(); Die die2 = new Die(); // Add a random roll of a die to the user's choice to // establish the strike-out sum strikeOutSum = strikeOutSum + die1.roll(); System.out.println("The strike out sum is " + strikeOutSum); int totalPoints = 0; int strikes = 0; // Roll the dice repeatedly while the user hasn't reached three // strikes or hasn't reached 200 points while (strikes < 3 && totalPoints < 200) { int points; // roll the dice int roll1 = die1.roll(); int roll2 = die2.roll(); System.out.println(); System.out.println("You rolled " + roll1 + " " + roll2); // Need to check whether rolled the strikeOutSum first if (roll1 + roll2 == strikeOutSum) { strikes++; System.out.println("STRIKE-OUT SUM! You get a strike!"); points = 0; } else if (roll1 == roll2) { // && sum of die not equal strikeOutSum points = 10 * roll1; } else { // && sum of die not equal strikeOutSum && not doubles points = roll1 * roll2; } // Print the points for this roll, update total points gained and // display the current status of the game. System.out.println("You get " + points + " points"); totalPoints = totalPoints + points; displayStatus(totalPoints, strikes); } // Print whether wins or loses. System.out.println(); if (totalPoints >= 200) { System.out.println("YOU WIN!!"); } else { System.out.println("SORRY: You struck out."); } } // Display a summary of the current status public static void displayStatus(int points, int strikes) { String banner = "******************************"; System.out.println(banner); System.out.println ("Strikes: " + strikes); System.out.println("Your total points: " + points); System.out.println(banner); } }