- 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
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