Assignment Write a Java program that plays the game Rock, Paper, Scissors. Inste
ID: 3871432 • Letter: A
Question
Assignment Write a Java program that plays the game Rock, Paper, Scissors. Instead of using rock, paper and scissors use Boulder, Tissue and Chainsaw The program should generate a random choice (Boulder, Tissue or Chainsaw) then ask the user to choose Boulder, Tissue or Chainsaw. After that the program will display its choice and a message showing if the player won, lost or tied. Next, the program should prompt the user to play again or not. Once the player selects to stop playing the game, the program should print the number of wins, losses and ties by the player Run your program and show at least 2 examples of playing the game before quitting. For example, the output might look like Choose : (0-Boulder, 1-Tissue, 2=Chainsaw): 1 You chose 11ssue Computer chose Boulder You win Play again? (y/n): y Choose: (0-Boulder, I-Tissue, 2-Chainsaw): 0 You chose Boulder Computer chose Chainsaw You win Play again? (y/n): n You won 2 times You lost 0 times You tied 0 timesExplanation / Answer
import java.util.Random;
import java.util.Scanner;
public class BoulderTissueChainsaw {
private User user;
private Computer computer;
private int userScore;
private int computerScore;
private int numberOfGames;
private enum Move {
BOULDER, TISSUE, CHAINSAW;
/**
* Compares this move with another move to determining a tie, a win, or
* a loss.
*
* @param otherMove
* move to compare to
* @return 1 if this move beats the other move, -1 if this move loses to
* the other move, 0 if these moves tie
*/
public int compareMoves(Move otherMove) {
// Tie
if (this == otherMove)
return 0;
switch (this) {
case BOULDER:
return (otherMove == CHAINSAW ?1 : -1);
case TISSUE:
return (otherMove == BOULDER ? 1 :-1);
case CHAINSAW:
return (otherMove == TISSUE ? 1 :-1);
}
// Should never reach here
return 0;
}
}
private class User {
private Scanner inputScanner;
public User() {
inputScanner = newScanner(System.in);
}
public Move getMove() {
// Prompt the user
System.out.print("Boulder, Tissue, or Chainsaw? ");
// Get the user input
String userInput =inputScanner.nextLine();
userInput = userInput.toUpperCase();
char firstLetter = userInput.charAt(0);
if (firstLetter == 'R' || firstLetter == 'P' ||firstLetter == 'S') {
// User has entered a valid input
switch (firstLetter) {
case 'R':
return Move.BOULDER;
case 'P':
return Move.TISSUE;
case 'S':
return Move.CHAINSAW;
}
}
// User has not entered a valid input. Prompt again.
return getMove();
}
public boolean playAgain() {
System.out.print("Do you want to play again? ");
String userInput =inputScanner.nextLine();
userInput = userInput.toUpperCase();
return userInput.charAt(0) == 'Y';
}
}
private class Computer {
public Move getMove() {
Move[] moves = Move.values();
Random random = new Random();
int index =random.nextInt(moves.length);
return moves[index];
}
}
public BoulderTissueChainsaw() {
user = new User();
computer = new Computer();
userScore = 0;
computerScore = 0;
numberOfGames = 0;
}
public void startGame() {
System.out.println("BOULDER, TISSUE, CHAINSAW!");
// Get moves
Move userMove = user.getMove();
Move computerMove =computer.getMove();
System.out.println(" You played " +userMove + ".");
System.out.println("Computer played " +computerMove + ". ");
// Compare moves and determine winner
int compareMoves =userMove.compareMoves(computerMove);
switch (compareMoves) {
case 0: // Tie
System.out.println("Tie!");
break;
case 1: // User wins
System.out.println(userMove + " beats " + computerMove + ". You won!");
userScore++;
break;
case -1: // Computer wins
System.out.println(computerMove + " beats " + userMove + ". You lost.");
computerScore++;
break;
}
numberOfGames++;
// Ask the user to play again
if (user.playAgain()) {
System.out.println();
startGame();
} else {
printGameStats();
}
}
/**
* Prints out the statistics of the game. Calculates ties as 1/2 a win in
* percentage won.
*/
private void printGameStats() {
int wins = userScore;
int losses = computerScore;
int ties = numberOfGames - userScore -computerScore;
double percentageWon = (wins +((double) ties) / 2) / numberOfGames;
// Line
System.out.print("+");
printDashes(68);
System.out.println("+");
// Print titles
System.out.printf("| %6s | %6s | %6s | %12s | %14s | ",
"WINS", "LOSSES", "TIES", "GAMES PLAYED", "PERCENTAGE WON");
// Line
System.out.print("|");
printDashes(10);
System.out.print("+");
printDashes(10);
System.out.print("+");
printDashes(10);
System.out.print("+");
printDashes(16);
System.out.print("+");
printDashes(18);
System.out.println("|");
// Print values
System.out.printf("| %6d | %6d | %6d | %12d | %13.2f%% | ",
wins, losses, ties, numberOfGames, percentageWon * 100);
// Line
System.out.print("+");
printDashes(68);
System.out.println("+");
}
private void printDashes(intnumberOfDashes) {
for (int i = 0; i < numberOfDashes; i++) {
System.out.print("-");
}
}
public static void main(String[] args) {
BoulderTissueChainsaw game = newBoulderTissueChainsaw();
game.startGame();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.