Welcome to Dream.In.Code
Become a Java Expert!

Join 150,033 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,585 people online right now. Registration is fast and FREE... Join Now!




Hangman

 
Reply to this topicStart new topic

Hangman

thegreatpan12
6 Jun, 2008 - 09:57 PM
Post #1

New D.I.C Head
*

Joined: 6 Jun, 2008
Posts: 7

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();
}
}
}
}

User is offlineProfile CardPM
+Quote Post

pbl
RE: Hangman
6 Jun, 2008 - 10:01 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Works perfectly for me.... but
as I am the one who proposed the word... easy to win
User is online!Profile CardPM
+Quote Post

mensahero
RE: Hangman
6 Jun, 2008 - 10:04 PM
Post #3

c0mput3rz Are Only Human
Group Icon

Joined: 26 May, 2008
Posts: 664



Thanked: 17 times
Dream Kudos: 75
My Contributions
I see you created another thread.. well the reply to my previous post in your first thread.. blink.gif blink.gif


well.. I know the game hangman.. its where a blank word needs to be guessed by trying various keys. . but in you game I don't get it..

it simply not work.. I cant find the right ANSWER since I gave up guessing it.. and the only correct key is the first key I ever tried..

for example:

1. WHen I press x.. it will become ... _X << meaning it is correct its the last char of the ANSWER..

2. well that basically ends up they're.. it's always game over.. blink.gif


TO PBL:

may I know what is the correct answer? I cant find it in the code..

This post has been edited by mensahero: 6 Jun, 2008 - 10:05 PM
User is offlineProfile CardPM
+Quote Post

thegreatpan12
RE: Hangman
6 Jun, 2008 - 10:08 PM
Post #4

New D.I.C Head
*

Joined: 6 Jun, 2008
Posts: 7

QUOTE(pbl @ 6 Jun, 2008 - 11:01 PM) *

Works perfectly for me.... but
as I am the one who proposed the word... easy to win

ahh ok atleast it works haha thanks for your help i really appreciate it
User is offlineProfile CardPM
+Quote Post

mensahero
RE: Hangman
6 Jun, 2008 - 10:14 PM
Post #5

c0mput3rz Are Only Human
Group Icon

Joined: 26 May, 2008
Posts: 664



Thanked: 17 times
Dream Kudos: 75
My Contributions
QUOTE(thegreatpan12 @ 6 Jun, 2008 - 11:08 PM) *

QUOTE(pbl @ 6 Jun, 2008 - 11:01 PM) *

Works perfectly for me.... but
as I am the one who proposed the word... easy to win

ahh ok atleast it works haha thanks for your help i really appreciate it



owkie.. I may look like a desperate guy who want to prove you program doesn't work or something.. but I just feel like theres a need to tell you the it doesn't work..

Yes it compiles.. it runs.. but the bottom blink.gif line is.. the main purposes of the program doesn't work.. blink.gif

IDK if PBL did guessed the right word.. but I can't seem to understand how he said it works perfectly.. blink.gif

Don't get me wrong.. I just want to tell you that its not working the way it supposed to be.. and if you may..

WHAT IS THE CORRECT WORD? blink.gif

User is offlineProfile CardPM
+Quote Post

pbl
RE: Hangman
6 Jun, 2008 - 10:20 PM
Post #6

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(mensahero @ 6 Jun, 2008 - 11:14 PM) *

IDK if PBL did guessed the right word.. but I can't seem to understand how he said it works perfectly.. blink.gif

Don't get me wrong.. I just want to tell you that its not working the way it supposed to be.. and if you may..


It compiles....
OK easy the user enters the word to guess....
If you suggest a letter not in the word it tells you
If you enter a good letter it shows it to you
So basically the program works but as I said "easy... the player enters the word to guess"

User is online!Profile CardPM
+Quote Post

mensahero
RE: Hangman
6 Jun, 2008 - 10:28 PM
Post #7

c0mput3rz Are Only Human
Group Icon

Joined: 26 May, 2008
Posts: 664



Thanked: 17 times
Dream Kudos: 75
My Contributions
QUOTE(pbl @ 6 Jun, 2008 - 11:20 PM) *

QUOTE(mensahero @ 6 Jun, 2008 - 11:14 PM) *

IDK if PBL did guessed the right word.. but I can't seem to understand how he said it works perfectly.. blink.gif

Don't get me wrong.. I just want to tell you that its not working the way it supposed to be.. and if you may..


It compiles....
OK easy the user enters the word to guess....
If you suggest a letter not in the word it tells you
If you enter a good letter it shows it to you
So basically the program works but as I said "easy... the player enters the word to guess"



LMAOOOOOOOO!!!!!!! NOW I KNOW I'M STUPID!!!!!!!!!!!! blink.gif blink.gif blink.gif blink.gif blink.gif blink.gif blink.gif blink.gif blink.gif blink.gif blink.gif

I THINK I SHOULD COMMIT A SUICIDE OR SOMETHING.. mad.gif

Now I finally got how the game works.. you give a WORD and pretend you don't know it and guess the hell out of it.. blink.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 09:38PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month