Rock Paper Scissors Input File: rock-paper-scissors-sample file.txt Rock, paper,
ID: 3724704 • Letter: R
Question
Rock Paper Scissors Input File: rock-paper-scissors-sample file.txt Rock, paper, scissors is a classic childrens game. The rules are simple. At the same time, each person makes a hand representation of a rock, a sheet of paper, or a pair of scissors. Each symbol chosen has a cyclical status of dominance over the over, ie., rock bets scissors, scissors cuts papers, and paper covers rock. If the two players chose the same symbol, it results in a draw Write a program that reads a file with the users input of an "R" for "Rock, a "P for "Paper" and a "S" for "Scissors". You must then determine the winner based upon the rules above. Also, at the end of the series of game, you must present a summary of each user's wins and the number of draws. Sample input: Sample output: Player 1 Player 2 Rock Result: Winner Player 1 Player 1 Player 2 Scissors Summary: Total Number of Games played: XX Player 1 won XX games. Player 2 won XX games. Draws: XX Rock Rock Result: Draw Player 1 Player 2 Paper Scissors Result: Winner Player2Explanation / Answer
import java.util.Scanner;
public class RockPaperScissors {
public void startGame() {
System.out.println("ROCK, PAPER, SCISSORS!");
Move player1 = user.getMove();
Move player2 = user.getMove();
System.out.println(" Player 1 " + player1 + ".");
System.out.println("Player 2" + player2 + ". ");
System.out.println("-----------");
}
private enum Move {
ROCK, PAPER, SCISSORS
/**
* 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 ROCK:
return (otherMove == SCISSORS ? 1 : -1);
case PAPER:
return (otherMove == ROCK ? 1 : -1);
case SCISSORS:
return (otherMove == PAPER ? 1 : -1);
}
if (user.playAgain()) {
System.out.println();
startGame();
} else {
printGameStats();
}
// Should never reach here
return 0;
}
}
private class User {
private Scanner inputScanner;
public User() {
inputScanner = new Scanner(System.in);
}
public Move getMove() {
// Prompt the user
System.out.print("Rock, paper, or scissors? ");
// 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.ROCK;
case 'P':
return Move.PAPER;
case 'S':
return Move.SCISSORS;
}
}
// User has not entered a valid input. Prompt again.
return getMove();
}
int compareMoves = player1.compareMoves(player2);
switch(compareMoves)
{
case 0: // Tie
System.out.println("Tie!");
break;
case 1: // Player 1 wins
System.out.println(player1 + " beats " + player2 + ". You won!");
userScore++;
break;
case -1: // Player 2 wins
System.out.println(player2 + " beats " + player1 + ". You lost.");
computerScore++;
break;
}numberOfGames++;
}
private User player1;
private User player2;
private int userScore;
private int computerScore;
private int numberOfGames;
public RockPaperScissors() {
player1 = new User();
player2 = new User();
player1Score = 0;
player2Score = 0;
numberOfGames = 0;
}
private void printGameStats() {
int wins = player1Score;
int losses = player2Score;
int ties = numberOfGames - player1Score - player2Score;
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("+");
}
public static void main(String[] args) {
RockPaperScissors game = new RockPaperScissors();
game.startGame();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.