Whats up? Can someone run this program and tell me if the full game works? It'll be great if you could tell me whats wrong with it! Thanks guys!
CODE
import javax.swing.*;
import java.io.*;
public class hang {
public hang() {
String input, guess;
String current_guesses = "";
String guessed_letters = "";
String display_string;
int i, j, cnt, y;
int guesses_left = 10 , matched_letters = 0;
boolean letter_match , full_match, unique_guess;
input = JOptionPane.showInputDialog(null, "insert your word");
if (input == null) return;
cnt= input.length();
if (cnt==0) return;
if (cnt > guesses_left ) {
JOptionPane.showMessageDialog(null,"String too long. Make it shorter than " + guesses_left );
new hang();
return;
}
String hm[] =new String[cnt];
String disp[] =new String[cnt];
//put the word in an array, a letter at a time
for (i=0; i<cnt; i++) {
hm[i] = input.substring(i,i+1);
disp[i] = "_";
System.out.println(disp[i]+" "+hm[i]);
}
for ( --guesses_left; guesses_left >= 0; guesses_left-- ) {
unique_guess = true;
do {
guess = JOptionPane.showInputDialog(null, "Guess a single Letter you have not Guessed before"); // The user guesses a letter only.
if (guess==null) return;
if (guess.length()==1) {
//System.out.println("indexof = " + guessed_letters.indexOf(guess) );
if (guessed_letters.indexOf(guess) < 0 ) unique_guess = true; // -1 means not found
else unique_guess=false;
}
} while (guess.length() != 1 | (!unique_guess) ); // Force the user to only enter 1 letter
if (guessed_letters.length() == 0) guessed_letters = guess;
else guessed_letters = guessed_letters + ", " + guess; // String of guessed characters.
//l=0;
letter_match = false; // assume no match
for ( y=0; y<cnt; y++ ) { // Look for a matching character.
if (hm[y].equalsIgnoreCase(guess)) { // Does the letter match?
letter_match = true;
matched_letters++;
disp[y] = hm[y]; // Set output string to the proper letter & proper case
} // if letter matches
} // for (y=0)
full_match = true;
for (y = 0; y < cnt; y++) { if (!disp[y].equals(hm[y])) full_match = false; }
if ( full_match ) {
JOptionPane.showMessageDialog(null,"Congratulations, you won\nThe correct word was '"+input+"'");
new hang();
return;
}
for (j=0, display_string = ""; j < cnt; j++ ) display_string = display_string + disp[j];
if (letter_match) {
JOptionPane.showMessageDialog(null,"Correct guess ya beast\n"+
display_string + "\n "+guesses_left+" guesses left\nold guesses: "+ guessed_letters);
}
else {
JOptionPane.showMessageDialog(null,"Wrong guess ya beast\n"+
display_string + "\n "+guesses_left+" guesses left\n old guesses: "+guessed_letters);
}
}
JOptionPane.showMessageDialog(null, "All out of guesses, try again");
new hang();
}
public static void main(String[]args) {
new hang();
}
}
}
}