Help with a hangman game
843789Apr 27 2010 — edited Apr 29 2010i need to have a character array to hold the word to be guessed, a character array to initially hold an underscore for each letter of the word to be guessed, a character array of unguessed letters, an integer variable to store the number of unguessed letters, a String array to hold the dictionary, and an integer variable to store the number of wrong guesses
You are required to print out a list of all available (unguessed) letters to the screen before each guess. As letters are guessed, deleted the letter from the array. Be sure to delete properly and keep a counter of the number of letters in the array.
You are to use an array of characters to store the word being guessed, and an array of characters with an underscore for each letter to be filled in. As the player correctly guesses a letter, replace the underscore with the letter guessed. When a player makes a correct guess, all instances of that letter should be filled in.
You will be provided with a dictionary.txt file that will serve as your dictionary. dictionary.txt will have an int as the first value, which is the number of words in the file. Store these in an array of Strings. You should generate a random number to determine which word will be played. Use the following code:
Random rand = new Random();//This line of code should be executed only once.
int index = rand.nextInt(numWords); //this will return an integer between 0 (inclusive) and numWords (exclusive). This line of code should be executed every time you choose a new word.
If a player guesses a letter that is not in the word being guessed, increment the number of wrong guesses. After each guess, display the number of misses. You may display a hangman if you wish, but that is not necessary.
If the player fills in all letters, display a ?Win? message. After 7 wrong guesses, display a message that he/she lost and display the word.
Allow a user to play the game repeatedly until he/she chooses to quit.
when i am in my unguessed method and have the user type in a letter, do i need to set that incoming letter to a variable before i try to use my for loop to update the list?
also in my wrongGuess method, I want to assign the next character that the person enters in to a variable called letter. once i do that i want to check if with the word to see if it is indeed a part of the word. if it isnt then i will increment the number of wrong guesses
my teacher is having us read from a file called dictionary.txt and having us use the Random import. we were never taught this so im not exactly sure where i should be putting any of it.
also, i was just looking for a little guidance for setting up the run method to call in main
Here is the code that i have. I am having a lot of difficulties and looking for a little help. Thanks a lot!!
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Random;
import java.util.Scanner;
public class Hangman
{
private char[] word;
private char[] underscore;
private char[] unguessed;
private String[] dictionary;
private int storeUnguessed=26;
private int numberWrong=0;
char letter;
Random rand;
char[] alphabet = new char[26];
{
alphabet[0]='a';
alphabet[1]='b';
alphabet[2]='c';
alphabet[3]='d';
alphabet[4]='e';
alphabet[5]='f';
alphabet[6]='g';
alphabet[7]='h';
alphabet[8]='i';
alphabet[9]='j';
alphabet[10]='k';
alphabet[11]='l';
alphabet[12]='m';
alphabet[13]='n';
alphabet[14]='o';
alphabet[15]='p';
alphabet[16]='q';
alphabet[17]='r';
alphabet[18]='s';
alphabet[19]='t';
alphabet[20]='u';
alphabet[21]='v';
alphabet[22]='w';
alphabet[23]='x';
alphabet[24]='y';
alphabet[25]='z';
}
public Hangman()
{
}
public void unguessed()
{
Scanner in = new Scanner(System.in);
int numLetters=26;
System.out.print("Unguessed Letters: " + alphabet);
System.out.print("Please enter a letter: ");
? // have the user type in a letter then remove it
for(int index=0; index<numLetters-1; index++)
{
alphabet[index]=alphabet[index+1];
}
numLetters--;
}
public void wrongGuess()
{
Scanner in=new Scanner(System.in);
for(int index=0; index<word.length;index++)
{
System.out.print("Please enter a letter: ");
char letter = in.next();
if(letter==word[index])
{
}
else
{
this.numberWrong++;
}
}
}
public void endGame()
{
for(int index=0; index<underscore.length;index++)
{
if(underscore[index]=='_' && numberWrong>7)
{
System.out.print("YOU LOSE! The word was: "
+ this.word);
}
}
}
public void readDictionary()
{
Scanner in=null;
int numWords=0;
try
{
in = new Scanner(new File("dictionary.txt"));
}
catch (FileNotFoundException ex)
{
System.out.print("File not found");
System.exit(1);
}
while(in.hasNext()&& numWords<dictionary.length)
{
dictionary[numWords]=in.next();
numWords++;
}
in.close();
Random rand = new Random();
}
public void gameWord()
{
int numWords=dictionary.length-1;
int index = rand.nextInt(numWords);
String temp=dictionary[index];
word=new char[temp.length()];
}
public void underscoreWord()
{
underscore=new char[word.length];
for(int index=0; index<underscore.length; index++)
{
if(underscore[index]==' ')
{
underscore[index]='_';
}
if(letter==word[index])
{
underscore[index]=letter;
}
}
}
public void run()
{
/* -read in from dictionary
* -choose a random word
* -print out unguessed letters
* -print out underscores
* -ask the user for a letter
* -read in the letter
* -go through word and underscore
* -check to see if it is the end of a game
* -ask for another letter
* ???????
*/
}
public static void main(String [] args)
{
Hangman game = new Hangman();
game.run();
}
}