/** * Lab7.java * Course: SE1011 * Term: Fall 2015 * Assignment: Lab 7 * Author: Anonymous * Date: 10/21/2015 */ package class8_2_Elevens; import java.util.Scanner; /** * A Game to be played using the Die.java random die generator * * The player rolls a ten-sided die and a six-sided die,attempting to get as close to 11 as possible without * going over. If the player gets 11 exactly, they * automatically win, but if they go over 11, the computer automatically wins. After both dies have been * rolled once, one of them can be rolled again to attempt a better total. The computer will always re-roll * the smaller die if they have a lower score than the human player. * */ public class Lab7 { // TODO: Can we reduce the state of this class? private boolean isLoser = false; //if true, the player is the loser private int playerRoll1; //the roll of the player's six-sided die private int playerRoll2; //the roll of the player's ten-sided die private int playerTotal = 0; //the total of both the player's die rolls (default 0) private int comTotal; //the total of both the computer's rolls public static void main(String[] args) { Lab7 theGame = new Lab7(); theGame.playGame(); } public boolean isWinner() { return playerTotal == 11 || playerTotal > comTotal; } private void playGame() { Scanner in = new Scanner(System.in); int comRoll1; //the roll of the computer's six-sided die int comRoll2; //the roll of the computer's ten-sided die String rollSelect; //selects which die to re-roll on the menu String roll; //selects to roll the dice or not System.out.println("Welcome to Eleven, the game where the player rolls a ten-sided die and a six-sided die,\n " + "attempting to get as close to 11 as possible without going over. If the player gets 11 exactly, they\n " + "automatically win, but if they go over 11, the computer automatically wins. After both dies have been\n " + "rolled once, one of them can be rolled again to attempt a better total. The computer will always re-roll\n " + "the smaller die if they have a lower score than the human player.\n "); System.out.println("Enter r to roll the dice or q to quit"); roll = in.next(); if (roll.equals("r") || roll.equals("R")) { do { isLoser = false; System.out.println("You rolled the dice:"); Die playerDie1 = new Die(); Die playerDie2 = new Die(10); playerRoll1 = playerDie1.roll(); playerRoll2 = playerDie2.roll(); playerTotal = playerRoll1 + playerRoll2; System.out.println("You rolled " + playerRoll1 + " on the six-sided die, and " + playerRoll2 + " on the ten-sided die for a total of " + playerTotal); if (playerTotal < 11) { System.out.println("Enter 6 to re-roll the six-sided die, 1 to re-roll the ten sided die, or 0 to keep the current rolls."); rollSelect = in.next(); switch (rollSelect.charAt(0)) { case '6': reRollOneDie(playerDie1,1); break; case '1': reRollOneDie(playerDie2,2); break; case '0': break; } //end switch statement }//end player re-roll if statement else if (playerTotal == 11) { // UNNEEDED -- isWinner will return true in this case } else { isLoser = true; } if (isWinner() == false && isLoser == false) { Die comDie1 = new Die(); Die comDie2 = new Die(10); comRoll1 = comDie1.roll(); comRoll2 = comDie2.roll(); comTotal = comRoll1 + comRoll2; System.out.println("The computer rolled " + comRoll1 + " on the six-sided die, and " + comRoll2 + " on the ten-sided die for a total of " + comTotal); if (comTotal < 11 && comTotal <= playerTotal) { System.out.println("The computer is re-rolling the six-sided die."); comRoll1 = comDie1.roll(); comTotal = comRoll1 + comRoll2; System.out.println("The computer rolled " + comRoll1 + " on the six-sided die, and " + comRoll2 + " on the ten-sided die for a total of " + comTotal); }//end computer re-roll if statement else if (comTotal == 11) { isLoser = true; } else if (comTotal > 11) { // no longer needed. // TODO: Make sure isWinner returns true in this case } if (comTotal > playerTotal && comTotal < 11) { isLoser = true; } else if (playerTotal > comTotal && playerTotal < 11) { // no longer needed // TODO: Make sure isWinner returns true in this case } } //end computer roll if statement if (isLoser) { System.out.println("Sorry, You Lose! Try Again Later!"); } else if (isWinner()) { System.out.println("Congratulations! You Won!"); } System.out.println("Play Again? Enter r to play again or q to quit"); roll = in.next(); }while (roll.equalsIgnoreCase("r")); } // end roll if statement else if (roll.equals("q") || roll.equals("Q")){ System.out.println("Exiting the program, Thanks for playing!"); } //end roll q else if statement else { System.out.println("Invalid entry, restart the program and try again."); } System.out.println("Exiting the program, Thanks for playing!"); } //end main method private void reRollOneDie(Die playerDie, int numDie) { int playerRoll = playerDie.roll(); if(numDie == 1) { this.playerRoll1 = playerRoll; } else { this.playerRoll2 = playerRoll; } this.playerTotal = this.playerRoll1 + this.playerRoll2; System.out.println("You rolled " + playerRoll1 + " on the six-sided die, and " + playerRoll2 + " on the ten-sided die for a total of " + playerTotal); if (playerTotal > 11) { isLoser = true; } } } //end class Lab7