/*
* COSC 1336
* Homework #4
*/
import java.util.Scanner;
public class Rock {
public static void main(String[] args) {
Scanner PlayerInput = new Scanner(System.in);
int Player = 3; //create and initialize outside of accepted value (for error checking with < && >
int Computer = 3; //create and initialize outside of accepted values the play for the computers.
char PlayAgain = 'y'; //create and initialize the PlayAgain variable to allow the loop to run the first time without input.
int ComputerWin = 0; //number of times you lost.
int PlayerWin = 0; //number of times you won.
int isTie = 0; //number of times you tied.
// Prompt to let the user input Scissor (1), rock(2), paper (3) .
while (PlayAgain == 'y') {
System.out.println(""); //extra line.
System.out.print("Please select Paper (1), Rock (2), or Scissors (3): "); //print the request to selection option to screen.
Player = PlayerInput.nextInt(); // read user input number
Computer = (int)(Math.random()*3+1); //generate a choice for the computer.
// Check user input was within range.
while (Player > 3 || Player < 1) {
System.out.println("I'm sorry that is not a valid choice, please choose Paper (1) Rock (2) or Scissors (3).");
Player = PlayerInput.nextInt();
}
String PlayerInputString = ("");
switch (Player) {
case 1: PlayerInputString = "Paper (1).";
break;
case 2: PlayerInputString = "Rock (2).";
break;
case 3: PlayerInputString = "Scissors (3).";
break;
}
System.out.println("You have selected " + PlayerInputString);//Display Users selection.
//Create a string to output based on random computer variable
String ComputerInputString = ("");
switch (Computer) {
case 1: ComputerInputString = "Paper (1).";
break;
case 2: ComputerInputString = "Rock (2).";
break;
case 3: ComputerInputString = "Scissors (3).";
break;
}
System.out.println("The computer has selected " + ComputerInputString); //Displays Computer's selection.
//See who won.
if (Player == Computer) {
System.out.println("It's a tie!");
System.out.println("");
isTie++;
}
else if (Player == 1 && Computer != 3) {
System.out.println("You Win! :-)");
System.out.println("");
PlayerWin++;
}
else if (Player == 2 && Computer != 1) {
System.out.println("You Win! :-)");
System.out.println("");
PlayerWin++;
}
else if (Player == 3 && Computer != 2 ) {
System.out.println("You Win! :-)");
System.out.println("");
PlayerWin++;
}
else {
System.out.println("The computer won! :-(");
System.out.println("");
ComputerWin++;
}
PlayAgain = PlayAgain();//set the variable PlayAgain based on the results of the PlayAgain() method
}
//print the score when you decide you are done playing.
System.out.println("\nYou won " + PlayerWin + " times!");
System.out.println("You lost " + ComputerWin + " times!");
System.out.println("You tied " + isTie + " times!");
PlayerInput.close(); //close scanner input.
}
//method to see if you want to play again.
public static char PlayAgain() {
Scanner PlayerInput = new Scanner(System.in);
char PlayAgain = 'y';
System.out.print("Do you want to play again? Yes (y), or No (n): " );
PlayAgain = PlayerInput.next().charAt(0);
if (PlayAgain == 'Y') {
PlayAgain = 'y';
}
return PlayAgain;
}
}