/*
 * Solution to Problem 4 (C++)
 * 13th Annual CS Programming Contest
 * WCU Dept of Math and CS
 * Copyright 2002 , All Rights Reserved
 *
 * Problem Statement:
 * Rock, Paper, Scissors.
 * The program plays the game Rock-Paper-Scissors with the user until
 * the user does not want to play any longer. In a single game the
 * user makes a choice of rock, paper, or scissors and the computer
 * does also. The computer's choice is made randomly. Rock beats
 * scissors, scissors beats paper, and paper beats rock.
 *
 * The program must have the following properties:
 * 1) When the program prompts the user about playing another game,
 * the program must handle the user entering an invalid value (that
 * is, not yes or no) by detecting that the value is invalid and
 * repeating the prompt until a valid answer is give by the user.
 * 2) When the program prompts the user about making a choice of rock
 * paper, or scissors, the program must handle the user entering 
 * an invalid value by detecting that the value is invalid and
 * repeating the prompt until a valid answer is give by the user.
 */

#include <iostream.h>
#include <stdlib.h> // needed to use the function random()
#include <string> // needed to use the String class

string getContinue();
string getUserChoice();
string getComputerChoice();
void playGame();
bool tie(string userChoice, string computerChoice);
bool userWins(string userChoice, string computerChoice);

int main()
{
  cout << "Do you want to play a game of Rock, Paper, Scissors(yes/no)?";
  while (getContinue() == "yes")
  {
    playGame();
    cout << "Do you want to play a game of Rock, Paper, Scissors" 
      << "(yes/no)?";
  }
  cout << "Thanks for playing." << endl;
}

string getContinue()
{
  string answer;

  cin >> answer;
  while (answer != "yes" && answer != "no")
  {
    cout << "Your answer was not yes or no. Please try again.";
    cin >> answer;
  }
  return answer;
}

string getUserChoice()
{
  string answer;

  cin >> answer;
  while (answer != "rock" && answer != "paper" && answer != "scissors")
  {
    cout << "Your answer was not rock, paper, or scissors. Please try again."; 
    cin >> answer;
  }
  return answer;
}

void playGame()
{
  cout << "Please enter your choice of rock, paper, or scissors: ";
  string userChoice = getUserChoice();
  string computerChoice = getComputerChoice();
  if (tie(userChoice, computerChoice))
    cout << "Tie: user == " << userChoice << "; computer == " 
    	<< computerChoice << endl;
  else if (userWins(userChoice, computerChoice))
    cout << "User wins: user == " << userChoice << "; computer == " 
    	<< computerChoice << endl;
  else
    cout << "Computer wins: user == " << userChoice << "; computer == " 
    	<< computerChoice << endl;
}

string getComputerChoice()
{
  int randVal = random() % 3;  // needed since random() returns an int
  				// value that is between 0 and a large value
  if (randVal == 0)
    return "rock";
  else if (randVal == 1)
    return "paper";
  else
    return "scissors";
}

bool tie(string userChoice, string computerChoice)
{
  return userChoice == computerChoice;
}

bool userWins(string userChoice, string computerChoice)
{
  if (userChoice == "rock" && computerChoice == "scissors")
    return true;
  else if (userChoice == "paper" && computerChoice == "rock")
    return true;
  else if (userChoice == "scissors" && computerChoice == "paper")
    return true;
  else 
    return false;
}

