/** 2008 WCU Computer Science High School Programming Contest
* Solution to Problem Four, Copyright 2008, all rights reserved
* Western Carolina University 
* Department of Mathematics and Computer Science
*/
import java.io.*;
import java.util.*;

public class Problem4 
{
	
  /** Given the userChoice and the computerChoice this function 
  * indicates is the computer won. */
  public static boolean cpuWins(int userChoice, int cpuChoice) 
  {
    // 0 --> paper
    // 1 --> scissors
    // 2 --> rock
		
    return	(cpuChoice == 0 && userChoice == 2) ||
      (cpuChoice == 1 && userChoice == 0) ||
      (cpuChoice == 2 && userChoice == 1);
   }

  /** This returns the string associated with a number for 
  * paper/rock/scissors. */
  public static String choiceString(int choice) 
  {
    switch(choice) {
      case 0: return "paper";
      case 1: return "scissors";
      case 2: return "rock";
      default: return "(invalid)";
    }
  }

  public static int processInput()
  {
    Scanner scanIn = new Scanner(System.in);
    boolean gotInput = false;
    int userChoice = 0;

    while (!gotInput)
    {
      // get user input
      System.out.print("Please type rock, paper, or scissors " +
        "(type 'quit' to exit the program).\n>> ");
      String line = scanIn.nextLine().toLowerCase();
      userChoice = 0;
			
      // check for need to quit
      if (line.equals("quit")) System.exit(0);
			
      // check user input
      if (line.equals("paper")) 
        { userChoice = 0; gotInput = true;}
      else if (line.equals("scissors")) 
        { userChoice = 1; gotInput = true;}
      else if (line.equals("rock")) 
        { userChoice = 2; gotInput = true;}
      else 
        System.out.println("\nInvalid input. Try again.\n");
    }
    return userChoice;
  }

  public static void main(String[] args) 
  {
    Random rd = new Random( System.currentTimeMillis( ) );
		
    while (true) 
    {
      int userChoice = Problem4.processInput();
			
      // the computer picks a random choice
      int computerChoice = Math.abs(rd.nextInt() % 3);
      System.out.println("Computer Choice:" + 
        Problem4.choiceString(computerChoice));

      // decide who won
      if (userChoice == computerChoice)
        System.out.println("\nThis round is a draw.\n");
      else if (cpuWins(userChoice,computerChoice))
        System.out.println("\nThe computer won (" + 
          Problem4.choiceString(computerChoice) + " > " + 
          Problem4.choiceString(userChoice) + ").\n");
      else
        System.out.println("\nYou won (" + 
          Problem4.choiceString(userChoice) + " > " + 
          Problem4.choiceString(computerChoice) + ").\n");
				
    }      // end of while
  }
}