Skip to content

Instantly share code, notes, and snippets.

@ajbinky
Created October 1, 2018 23:34
Show Gist options
  • Save ajbinky/e9f926af30b80bc506b7cb5af55a8736 to your computer and use it in GitHub Desktop.
Save ajbinky/e9f926af30b80bc506b7cb5af55a8736 to your computer and use it in GitHub Desktop.
package week4.rps;
import java.util.Scanner;
public class RockPaperScissors {
private static final String TIE = "It was a tie";
private static final String PLAYER_ONE_WINS = "Player One wins";
private static final String PLAYER_TWO_WINS = "Player Two wins";
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String playerOne;
String playerTwo;
String outcome = null;
System.out.print("Player One, enter rock/paper/scissors: ");
playerOne = scan.nextLine().toLowerCase();
System.out.print("Player Two, enter rock/paper/scissors: ");
playerTwo = scan.nextLine().toLowerCase();
scan.close();
// Tie
if (playerOne.equals(playerTwo)) {
outcome = TIE;
}
// Player One chooses rock
if (playerOne.equals("rock")) {
if (playerTwo.equals("paper")) {
outcome = PLAYER_TWO_WINS;
}
if (playerTwo.equals("scissors")) {
outcome = PLAYER_ONE_WINS;
}
}
// Player One chooses paper
if (playerOne.equals("paper")) {
if (playerTwo.equals("rock")) {
outcome = PLAYER_ONE_WINS;
}
if (playerTwo.equals("scissors")) {
outcome = PLAYER_TWO_WINS;
}
}
// Player One chooses scissors
if (playerOne.equals("scissors")) {
if (playerTwo.equals("rock")) {
outcome = PLAYER_TWO_WINS;
}
if (playerTwo.equals("paper")) {
outcome = PLAYER_ONE_WINS;
}
}
// Print the outcome
System.out.println(outcome);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment