working JAVA Hangman

This isn't complete. I still have to add the text file with all the different phrases for it to use.  But it is a working program.
A lot of the problems I had with it were because I would skip over parts of the lesson that were redundant, or would be changed later,  and then my code would end up in the wrong places, so I had to go back through the lesson and fix all the locations.


Error 1:  if you enter more then 1 letter, it adds both to the 'wrong letters'
Error 2: typing more then one correct letter will fill them in.
Fixed Errors with :
    found = true;
    index = -1;


I'm going to keep trying to mess with it to find all the problems. Will add anymore I find.
Fixes will be highlighted.




package nellie.tobey.Hangman;
import nellie.tobey.mystringmethods.MyStringMethods;
import nellie.tobey.mywindow.MyWindow;
public class Hangman extends MyWindow {
 private String phrase;
 private String clue;
 private String wrongLetters;

 public Hangman() {
  phrase = "HELLO WORLD";
  blankOutClue();
  wrongLetters = "";
  printPuzzle();
  boolean found = false;
  while(clue.contains("-") && wrongLetters.length() < 10) {
   String guess = promptForString("Guess a letter: ");
   guess= guess.toUpperCase();
   found = false;
   int index = phrase.indexOf(guess);
   if (guess.length()!= 1) {
    print("Your guess must be only 1 letter. Try again.");
      found = true;
      index = -1;

   }
   else if (clue.contains(guess) || wrongLetters.contains(guess)) {
    print("You alreay guessed " + guess + "...Try again.");
      found = true;
      index = -1;

   }


   while (index> -1){
    found = true;
    clue = MyStringMethods.replaceStringAt(clue, index, guess);
    index = phrase.indexOf(guess, index +1);
   }
   if (!found) {
    wrongLetters += guess;
   }
   printPuzzle();
  }
  if(!clue.contains("-")) {
   print("Congratulations! You win!");
  }
  if(wrongLetters.length()>=10) {
   print("You Lose. Answer was:  " + phrase);
  }
 }





 private void printPuzzle() {
  print("----------");
  switch (wrongLetters.length()) {
  case 0:
   print("|");
   print("|           " + wrongLetters);
   print("|");
   break;
  case 1:
   print("|      O");
   print("|          " + wrongLetters);
   print("|");
   break;
  case 2:
   print("|      O");
   print("|      |     " + wrongLetters);
   print("|");
   break;
  case 3:
   print("|      O");
   print("|     -|    " + wrongLetters);
   print("|");
   break;
  case 4:
   print("|      O");
   print("|    --|    " + wrongLetters);
   print("|");
  case 5:
   print("|      O");
   print("|    --|-  " + wrongLetters);
   print("|");
   break;
  case 6:
   print("|      O");
   print("|    --|--  " + wrongLetters);
   print("|     ");
   break;
  case 7:
   print("|      O");
   print("|    --|--  " + wrongLetters);
   print("|     /");
   break;
  case 8:
   print("|      O");
   print("|    --|--  " + wrongLetters);
   print("|    _/ ");
   break;
  case 9:
   print("|      O");
   print("|    --|--  " + wrongLetters);
   print("|    _/ \\");
   break;
  case 10:
   print("|      O");
   print("|    --|--  " + wrongLetters);
   print("|    _/ \\_");
   break;
  default:
   print("Try again");
   break;
  }
  print("|         " + clue);
 }

 private void blankOutClue() {
  clue = "";
  for(int i = 0; i<phrase.length(); i++) {
   char letter = phrase.charAt(i);
   if (letter ==  ' ') {
    clue += ' ';
   }
   else {
    clue += '-';
   }
  }
 }


 public static void main(String[] args) {
  new Hangman();
 }
}

Comments

Popular posts from this blog

playing with color in powershell python

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame