OK so I actually have a couple basic problem but the most important problem is the array. I am making a hangman game. I have a driver that was supplied but it is actually confusing me more.
1. Does the driver get the information from the file for me, or do I need to have the code that I put in main to get the information?
2. I tried to output the String word just so that I knew file.next was working to get the information from the file, but I couldn't get it to output from either the displayGameIntro or play methods because it didn't recognize the variable. Any suggestions?
3. This has to do with #2, since I can't seem to get the String word to be recognized, I can't turn it into an array. My plan is to create an array called letter (which I commented out while i was trying to get the other stuff working) that I can use when I am checking to see if the user has entered the correct letter. Is this the wrong idea to be using. Should I not use an array to check if the guessed letter is correct?
Here is the driver:
/* This program is a word guessing game called hangman. * A person will try and guess a word before the max * number of guesses are used. Every time the user chooses an * incorrect letter another body part is displayed in the gallows. */ import java.util.*; import java.io.FileInputStream; import java.io.FileNotFoundException; public class HangmanDriver { public static final String filename = "hw7data.txt"; // Driver to run the game using the student Hangman class public static void main(String[] args) { Scanner wordsFile = null; // words data file // open the file containing the words try { wordsFile = new Scanner(new FileInputStream(filename)); } catch (FileNotFoundException e) { System.out.println("File not found or not opened."); System.exit(0); } // create object and prepare to play game Hangman game = new Hangman(wordsFile); Scanner keyboard = new Scanner(System.in); // display an introduction on the game to the player game.displayGameIntro(); // continually play new games if the user desires String playAgain; do { game.play(); System.out.print("Do you want to play again? "); playAgain = keyboard.next(); System.out.println(); } while (playAgain.toUpperCase().startsWith("Y")); System.out.println("Thanks for playing!"); } }
This is my program:
// This program runs a game of hangman // The point is to guess the word based on the number of blank spaces // // //@author: Kristen Watson //@version: 11/23/201 import java.util.Scanner; import java.io.File; public class Hangman { public final static String filename = "hw7data.txt"; public static void main(String[] args) { Scanner inputFile = null; try { inputFile = new Scanner(new File(filename)); } catch (Exception e) { System.out.println("File could not be opened: " + filename); System.exit(0); } } public Hangman(Scanner file) { // Store the file object in an instance variable for later use String word = file.next(); } //Displays the Intro to the game for the player public void displayGameIntro() { System.out.println("You have just started a game of Hangman."); System.out.println("You have seven guesses to figure out the correct word."); System.out.println("The blank spots indicate how long the word is. Each"); System.out.println("correct letter will show up in the correct spot, while"); System.out.println("a wrong guess will lead to another body part being"); System.out.println("displayed. After seven incorrect guesses, you lose."); } public void play () { // Here is some high-level pseudocode of what you want to do: // // Initialize everything, such as arrays //int [] letters = new int [word.length]; //for loop that takes word into array letters //for(int i = 0; i < word.length; i++){ // letters [i] = word[i]; // // Get one word from the data file // Loop until game is over: // Display current picture, letters guessed, wrong guesses, and word so far // Get a valid, not-yet-guessed letter from the player // Determine if the letter is in the word and update variables appropriately // Handle winning or losing } }