//********** //SimpleCalculator.java //John Purcell EID: 824479727 //CS 160: HW6 //November 3, 2008 //This is a game of rock-paper-scissors. The user inputs their choice //and the computer "rolls" it's choice and returns who wins. //********** import java.util.Scanner; import java.util.Random; public class RockPaperScissors { public static int Checker (String r){ String v = r.toLowerCase(); if (v.equals("rock")){ return 0; } if (v.equals("paper")){ return 1; } if (v.equals("scissors")){ return 2; } else{ return 3; } } //the method "Checker(r)" first converts the input to lower case //then tests it against the three choices. The "3" output then //becomes the "invalid entry" value. public static void main (String[] args){ Scanner S = new Scanner(System.in); System.out.println("Let's play Rock-Paper-Scissors."); System.out.println("Your choice?"); String x = S.next(); Random R = new Random(); int y = R.nextInt(2); //R.nextInt is 2 because that will return either 0, 1 or 2 //the switch/catch tree will then try all 3 of the possible //outputs. switch (y){ case 0://computer chooses rock if (Checker(x)==0){ System.out.println("I chose Rock"); System.out.println("The game was a tie"); break; } if (Checker(x)==1){ System.out.println("I chose Rock"); System.out.println("You won"); break; } if (Checker(x)==2){ System.out.println("I chose Rock"); System.out.println("I won"); break; } else{ System.out.println("Invalid choice"); //The only else case is Checker==3, but since else //would handle the outcome and allow proper compiling, //if (checker(x)==3) is not used. break; } case 1://computer chooses paper if (Checker(x)==0){ System.out.println("I chose Paper"); System.out.println("I won"); break; } if (Checker(x)==1){ System.out.println("I chose Paper"); System.out.println("The game was a tie"); break; } if (Checker(x)==2){ System.out.println("I chose Paper"); System.out.println("You won"); break; } else{ System.out.println("Invalid choice"); break; } case 2://computer chooses scissors if (Checker(x)==0){ System.out.println("I chose Scissors"); System.out.println("You win"); break; } if (Checker(x)==1){ System.out.println("I chose Scissors"); System.out.println("I win"); break; } if (Checker(x)==2){ System.out.println("I chose Scissors"); System.out.println("The game was a tie"); break; } else{ System.out.println("Invalid choice"); break; } } } }