guess my word not working
I know I have things wrong. It runs, but not how I want it to,
first error:
the first random word it picks is 'said' every time. That's not random.
second error:
It doesn't restart the game cleanly. Things print I don't want it to.
third error:
It doesn't pick a new random word until the third play through. First two words are said.
fourth error:
It doesn't close properly when you tell it that you do not want to repeat.
I'll post a corrected version when I figure it out. Once again the cheat it the back is almost completely useless. Learning the hard way. Hurray.
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];
Random rand = new 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) {
int pick = rand.nextInt(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) {
word = originalWord;
clue = "----";
findRightPlacedLetters(guess);
findWrongPlacedLetters(guess);
}
if(guess.equals(originalWord)) {
solved = true;
print("Congratulations!");
print("number of guesses = " + guesscount);
repeat = promptForYesNo("");
print("");
pick = rand.nextInt(numberOfWords);
word = words[pick];
word = word.toUpperCase();
word = originalWord;
clue = "----";
}
if(guess.length()> 4) {
guess = promptForString("Your guess must have 4 letters.");
guess = guess.toUpperCase();
}
else {
guess = promptForString(clue);
guess = guess.toUpperCase();
guesscount = guesscount += 1;
}
}
}
}
catch(FileNotFoundException e) {
print("Could not find file " + filename);
}
catch(IOException e) {
print("Can not read file " + filename);
System.exit(0);
}
}
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();
}
}
first error:
the first random word it picks is 'said' every time. That's not random.
second error:
It doesn't restart the game cleanly. Things print I don't want it to.
third error:
It doesn't pick a new random word until the third play through. First two words are said.
fourth error:
It doesn't close properly when you tell it that you do not want to repeat.
I'll post a corrected version when I figure it out. Once again the cheat it the back is almost completely useless. Learning the hard way. Hurray.
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];
Random rand = new 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) {
int pick = rand.nextInt(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) {
word = originalWord;
clue = "----";
findRightPlacedLetters(guess);
findWrongPlacedLetters(guess);
}
if(guess.equals(originalWord)) {
solved = true;
print("Congratulations!");
print("number of guesses = " + guesscount);
repeat = promptForYesNo("");
print("");
pick = rand.nextInt(numberOfWords);
word = words[pick];
word = word.toUpperCase();
word = originalWord;
clue = "----";
}
if(guess.length()> 4) {
guess = promptForString("Your guess must have 4 letters.");
guess = guess.toUpperCase();
}
else {
guess = promptForString(clue);
guess = guess.toUpperCase();
guesscount = guesscount += 1;
}
}
}
}
catch(FileNotFoundException e) {
print("Could not find file " + filename);
}
catch(IOException e) {
print("Can not read file " + filename);
System.exit(0);
}
}
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
Post a Comment