guess my word JAVA completed

This is the working version:
I'll highlight the changes that fixed it.

package nellie.tobey.wordmastermind;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Random;

import nellie.tobey.mystringmethods.MyStringMethods;
import nellie.tobey.mywindow.MyWindow;
public class WordMastermind extends MyWindow {
 private String word;
 private String clue;
 private static final int numberOfWords = 342;
 private static final String filename = "wordMastermind.txt";

 public WordMastermind() {
  String words[] = new String[numberOfWords]; 
  //this fixed random
  //int rand = (int) (Math.random() * numberOfWords);


  try {
   BufferedReader in = new BufferedReader(new FileReader(new File(filename)));
   for(int i = 0; i < numberOfWords; i++) {
    words[i] = in.readLine();
   }

   in.close();
   boolean repeat = true;
   while(repeat) {
    //here random now
    int pick = (int) (Math.random() * numberOfWords);

    word = words[pick];
    word = word.toUpperCase();
    clue = "----";

    String originalWord = word;
    print(" Guess my 4 letter word.");
    print(" the \"X\" means a correct letter in the wrong place.");
    print(" the \"O\" means a correct letter in the correct place.");

    String guess= promptForString("Guess my four letter word:");
    guess = guess.toUpperCase();
    boolean solved = false;
    int guesscount = 1;

    while (!solved) {
     if(guess.length() == 4 && !guess.equals(originalWord)) {
      guess = promptForString(clue);
      guess = guess.toUpperCase();
      word = originalWord;
      clue = "----";
      findRightPlacedLetters(guess);
      findWrongPlacedLetters(guess);
      guesscount += 1;

     }
     if(guess.equals(originalWord)) {
      solved = true;
      print("Congratulations!");
      print("number of guesses = " + guesscount);
      repeat = promptForYesNo("");
      print("");
      word = words[pick];
      word = word.toUpperCase();
      word = originalWord;
      clue = "----";
     }

     if(guess.length()> 4) {
      guess = promptForString("Your guess must have 4 letters.");
      guess = guess.toUpperCase();
     }

    }
    System.exit(0);
   }
  }
  catch(FileNotFoundException e) {
   print("Could not find file " + filename);
  }
  catch(IOException e) {
   print("Can not read file " + filename);
  }
 }


 private void findRightPlacedLetters(String guess) {
  for(int i = 0; i < guess.length(); i++) {
   String guessLetter = guess.substring(i, i+1);
   String wordLetter = word.substring(i, i+1);
   if(guessLetter.equals(wordLetter)) {
    clue = MyStringMethods.replaceStringAt(clue, i, "O");
    word = MyStringMethods.replaceStringAt(word, i, "-");
   }
  }
 }
 private void findWrongPlacedLetters(String guess) {
  for(int i = 0; i < guess.length(); i++) {
   String letter = guess.substring(i, i+1);
   int letterLoc = word.indexOf(letter);
   if (letterLoc > -1) {
    word = MyStringMethods.replaceStringAt(word, letterLoc, "-");
    String clueLetter = clue.substring(i,i+1);
    if(clueLetter.equals("-")) {
     clue = MyStringMethods.replaceStringAt(clue, i, "X");
    }
   } 
  }
 }

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

 }
}

Comments

Popular posts from this blog

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame

JavaScript and a Matrix