I am creating a program that reads four letter words from a .txt file. The player, when they say "Yes" to play the game, the computer will randomly select an integer and then go and pick out the word at that line in the .txt file. Then the player will be given 7 tries to guess the word. What I am having trouble with is being able to call the methods in my RandomWordProvider class that will return the random integer and then the corresponding word. Here is what I have done:
public class WordGuessingGame2 {
static class RandomWordProvider {
public final List<String> words;
public RandomWordProvider() {
words = readFile();
}
public int randomInteger() {
int randomInt = (int) (Math.random() * words.size());
return randomInt;
}
public String getWord(int randomInt, String line) {
int randomPosition = randomInteger();
String randomWord = words.get(randomPosition);
return randomWord;
}
private List<String> readFile() {
List<String> wordsList = new ArrayList<>();
try {
File fourLetterWords = new File(System.getProperty("user.home"),"Documents/FourLetterWords.txt");
Scanner in = new Scanner(fourLetterWords);
while (in.hasNextLine()) {
String line = in.nextLine();
if (line!=null && !line.isEmpty()) {
wordsList.add(line);
}
}
}
catch (FileNotFoundException ex) {
System.out.println("File not found.");
}
return wordsList ;
}
}
static class PlayerCharacterEntry {
public String playerEntry() {
Scanner characterEntry = new Scanner(System.in);
System.out.print("Enter a character: ");
String playerInput = characterEntry.next();
playerInput = playerInput.toUpperCase();
return playerInput;
}
}
public static void main(String[] args) {
Scanner wantToPlay = new Scanner(System.in);
System.out.print("Welcome to the word guessing game! Would you like to play? ");
String playerAnswer = wantToPlay.next();
if (playerAnswer.equalsIgnoreCase("Yes")) {
System.out.print("\nYour objective is to guess a four letter word by entering"
+ "\nletters on your keyboard. If you can not guess the word in seven attempts,"
+ "\nyou lose! You will be told if the letter you entered is in the word, and"
+ "\nyou will be told if the letter you entered is not in the word. You will be"
+ "\nallowed to guess the word any time during your seven attempts. If at anytime"
+ "\nyou would like to terminate the game, enter the word 'terminate'. Good Luck!"
+ "\n \n");
}
if (playerAnswer.equalsIgnoreCase("No")) {
System.out.print("Maybe another time!");
System.exit(0);
}
RandomWordProvider randomWordProvider = new RandomWordProvider();
PlayerCharacterEntry playerCharacterEntry = new PlayerCharacterEntry();
playerCharacterEntry.playerEntry();
}
}
Now right here at the bottom I created the instances and I am now trying to call my randomInteger and getWord methods to my main method but I do not know how to do that. I keep getting confused on the arguments I need to include with the getWord method. If someone can show me what to do, that will be greatly appreciated!
Your first issue is that your current getWord(...)
method doesn't make sense as a public interface because you're relying on the class to generate the random number and read the file. So, you shouldn't need to send this in, although you could; it just wouldn't work, or make any difference, for your configuration.
So, the answer is, you need to have a method that takes no arguments and returns a string. Allow your code to do what you want.
public String getWord() {...}
But then in main()
you need to keep the string for a bit until the user exhausts their guesses.
String word = randomWordProvider.getWord();