I Finished My Hangman Program!

naruto1988

Golden Member
Jun 27, 2004
1,028
0
0
i finished the assignment on my free time because i like compsci and find puzzles fun. it was the first hard assignment i had and i had to ask my brother for help. he taught me how to use character array. on monday, i'll be upgrading the program and letting it read the word to be guessed from a dictionary file. but woohoo! i did it! haha.

here's terminalIO if you want to compile and run the program yourself.

import TerminalIO.KeyboardReader;

public class hangman {

public static void main(String args[]) {

KeyboardReader reader = new KeyboardReader();

// creates String word as "apple"
String word = "apple";

// creates an integer guessTimes which will limit the
// amount of bad guesses a user can make
int guessTimes = 5;

// creates character array guessNotYet, which
// contains as many underscores as the word length
char[] guessNotYet = new char[word.length()];
for (int i = 0; i < guessNotYet.length; i++) {
guessNotYet = '_';
}//end for

// loops with guessTimes as limit
while (guessTimes != 0) {

// prints out user's current status
// i.e. character array guessNotYet plus spaces inbetween
for (int k = 0; k < guessNotYet.length; k++)
System.out.print(guessNotYet[k] + " ");
System.out.println();//new line

// if user has guessed all letters in the word, tells them they have done so
// and ends the program by setting guessTimes to 0
int win = 0;

for (int m = 0; m < guessNotYet.length; m++) {
if (guessNotYet[m] == word.charAt(m))
win += 1;
}//end for

if (win == word.length()) {
System.out.println();
System.out.println("You guessed the word " + word + "!");
guessTimes = 0;
break;
}//end if

// asks user for guess
String input = reader.readLine("What is your guess? ");

// checks if input is only one character
if (input.length() == 1) {

// creates user's String input as character guessLetter
char guessLetter = input.charAt(0);

// declare integer n with value of 0
// and boolean goodGuess with value of false
int n = 0;
boolean goodGuess = false;

// checks if guessLetter is in String word
// if so, replace the character at occurence index #
// with guessLetter
while(n < word.length()) {
int index = word.indexOf(guessLetter,n);
if (index != -1) {
goodGuess = true;
guessNotYet[index] = guessLetter;
n = index + 1;
}//end if

// if all occurences are found and replaced,
// break out of current while loop
if (goodGuess == true && index == -1)
break;

// if no occurences are found, decrement guessTimes by one
// and break out of current while loop
if (goodGuess == false) {
guessTimes--;
break;
}//end if

}//end while loop

}//end if

// decrements guessTimes by one if user makes a bad guess
// bad guesses are ones that contain more than 1 character or no character
else guessTimes--;

}//end while

}//end public static void main

}//end public class hangman
 

mundane

Diamond Member
Jun 7, 2002
5,603
8
81
Originally posted by: joshsquall
Now use swing to develop a GUI for it.

I hate swing.

Some of the IDEs have GUI development tools - IIRC Netbeans had a pretty spiffy one. In the starting college courses we had to do all of our coding in a text editor (vim or emacs) to learn the basics, but now I just use these wonderful tools to get the job done quickly.

Oh, and congrats on finishing your first project. It's difficult to learn new paradigms and languages, especially if you have no previous experience. And for future reference, you can use the forum's attach code feature to include your code with preserved white space.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
You suffer from over commenting. And please use "attach code", so we can actually read it.

But it's very refreshing to see someone enthusiastically finish their programming homework, rather than come in here and complain about how their java prof sucks :p