import java.util.Random;

/**
 * Class to play the game hangman
 * @author Barb Ericson
 * Copyright 2005 Georgia Tech
 */
public class Hangman
{
  //////////////// fields //////////////////////////////
  
  /** the thing that displays the game */
  private HangmanDisplay display = new HangmanDisplay();
  
  /** the word to be guessed */
  private String word = null;
  
  /** the array of possible words */
  private String[] wordArray = {"help","word","work","pant", "farm"};
  
  /** the random number generator */
  private Random randNumGen = new Random();
  
  /** the number of wrong guesses */
  private int numWrongGuesses = 0;
  
  /** the number of right guesses */
  private int numRightGuesses = 0;
  
  /** the characters that were guessed that were wrong */
  private String wrongGuesses = "";
  
  /** the characters that were guessed that were right */
  private char[] rightGuesses = {' ',' ',' ', ' '};
  
  ///////////////// constructors ///////////////////////
  
  /**
   * no argument constructor.  Randomly picks the word
   * from the wordArray
   */
  public Hangman()
  {
    int index = randNumGen.nextInt(wordArray.length);
    word = wordArray[index];
  }
  
  ///////////////// methods /////////////////////////////
  
  /**
   * Method to let the user guess a letter using SimpleInput
   * @return true when the guess ends the game else false
   */
  public boolean guess()
  {
    boolean done = false;
    
    // get input from user
    String guessStr = SimpleInput.getString("Enter a letter");
    
    // remove whitespace
    guessStr = guessStr.trim();
    
    // if stil have at least one letter
    if (guessStr.length() > 0)
    {
      // get first letter
      char guessChar = guessStr.charAt(0);
      
      // check this letter
      done = this.guess(guessChar);
    }
    return done;
  }
  
  /**
   * Method to guess a letter 
   * @param the letter to guess
   */
  public boolean guess(char guessChar)
  {
    int index = word.indexOf(guessChar);
    boolean done = false;
    
    // if the letter is in the word
    if (index >= 0)
    {
      // increment the number of right guesses
      numRightGuesses++;
      
      // add letter to correctly guessed letters and draw it
      rightGuesses[index] = guessChar;
      display.drawRightGuess(rightGuesses);
      
      // check if the user won
      if (numRightGuesses == 4)
      {
        display.showWon();
        done = true;
      }
    }
    else
    {
      // add letter to string with wrong letters
      wrongGuesses = wrongGuesses + guessChar + " ";
      
      // display the wrong guesses
      display.drawWrongGuess(wrongGuesses);
      
      // icrement the number of wrong guesses
      numWrongGuesses++;
      
      // draw body part based on number of wrong guesses
      if (numWrongGuesses == 1)
        display.drawHead();
      else if (numWrongGuesses == 2)
        display.drawBody();
      else if (numWrongGuesses == 3)
        display.drawLeftArm();
      else if (numWrongGuesses == 4)
        display.drawRightArm();
      else if (numWrongGuesses == 5)
        display.drawLeftLeg();
      else if (numWrongGuesses == 6)
        display.drawRightLeg();
      
      // check if this was the last wrong guess
      if (numWrongGuesses == 6)
      {
        display.showLost();
        done = true;
      }
    }
    return done;
  }
  
  /**
   * Method to play the game till the user
   * wins or loses
   */
  public void playGame()
  {
    boolean done = false;
    // loop while we haven't reached the end of the game
    while (!(done = guess()))
    {}
  }
  
  // test this class
  public static void main(String[] args)
  {
    Hangman hangman = new Hangman();
    hangman.playGame();
  }
  
}