
// Hangman.java
// Andrew Davison, ad@coe.psu.ac.th, March 2023

/* A Hangman game.

   The hangman is drawn as a series of TextImages which
   are loaded from the hangs/ folder.

   Uses Lanterna 3
   https://github.com/mabe02/lanterna

   Usage (Windows):
     > compile.bat Hangman.java
     > run.bat Hangman

   Usage (Linux):
     > ./compile.sh Hangman.java
     > ./run.sh Hangman

*/


import java.io.*;
import java.util.*;

import com.googlecode.lanterna.*;
import com.googlecode.lanterna.graphics.*;
import com.googlecode.lanterna.input.*;
import com.googlecode.lanterna.screen.*;
import com.googlecode.lanterna.terminal.*;
import com.googlecode.lanterna.gui2.*;

import com.googlecode.lanterna.terminal.swing.*;

import com.googlecode.lanterna.TextColor.ANSI;



public class Hangman 
{
  private static final int NUM_PICS = 6;

  private static final String[] words = 
      {"rainbow", "computer", "science", "programming",
       "python", "mathematics", "player", "condition",
       "reverse", "water", "board", "geeks"};

  private static Random rand = new Random();


  public static void main(String[] args)
                                throws IOException
  {
    // load the hangman 'pictures'
    TextImage[] hangs = new TextImage[NUM_PICS];
    for (int i=0; i < NUM_PICS; i++)
       hangs[i] = Utils.loadTextImage("hangs/hang" + i + ".txt");

    Screen scr = Utils.createScreen();
    scr.setCursorPosition(null);

    Utils.drawImage(scr, 40, 2, hangs[0]);
    Utils.putString(scr, 5, 5, "Let's play Hangman...", ANSI.GREEN_BRIGHT);
    Utils.pause(1000);
    
    String word = words[ rand.nextInt(words.length) ];
    char[] wordChs = word.toCharArray();
    int wordLen = wordChs.length;

    // the word being built by the user
    char[] guessChs = new char[wordLen];
    for (int i=0; i < wordLen; i++)
       guessChs[i] = '_';

    Utils.putString(scr, 5, 7, "The length of the word is : " + wordLen);
    Utils.putString(scr, 5, 9, new String(guessChs));
    
    int numWrong = 0;
    boolean foundWord = false;
    while (numWrong < NUM_PICS-1) {
      Utils.putString(scr, 5, 13, "Guess a letter please, " + 
                             (NUM_PICS-1-numWrong) + " guesses left");
      char ch = Utils.readChar(scr);
      if (ch == Utils.ESC)
        break;
      if (word.indexOf(ch) == -1) {
        numWrong += 1;
        Utils.drawImage(scr, 40, 2, hangs[numWrong], ANSI.GREEN);
        Utils.putString(scr, 5, 11, "" + ch + " not in word, try again");
        continue;
      }
    
      // check if the user's character is in the word, and
      // update the guess accordingly
      for (int i=0; i < wordLen; i++) {
        if (wordChs[i] == ch)
          guessChs[i] = ch;
      }
      Utils.putString(scr, 5, 9, new String(guessChs));

      if (word.equals(new String(guessChs))) {
        Utils.putString(scr, 5, 15, "Congratulations you win!", 
                                             ANSI.GREEN);
        foundWord = true;
        break;
      }
    }

    Utils.clearLine(scr, 5, 13, 35);   // delete the text on line 13
    if (!foundWord)
      Utils.putString(scr, 5, 15, "The word was : " + word, 
                                             ANSI.GREEN);
    
    Utils.statusLine(scr, "Press <esc> to exit");
    Utils.waitQuit(scr);
    Utils.close(scr);
  }  // end of main()



}  // end of Hangman class
