Results 1 to 20 of 24
Thread: Hangman
- 05-12-2011, 07:50 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 28
- Rep Power
- 0
Hangman
I'm trying to create a hangman game, but the problem is, how would I make it so that if the person presses "J", "A", and "V" on the keyboard, it moves onto word2();?
Java Code:public static void WORD1() { //Java char key = ' '; c.setFont(new Font("arial", Font.PLAIN, 50)); c.setColor(Color.BLACK); c.drawString("_", 251, 472); c.drawString("_", 287, 472); c.drawString("_", 322, 472); c.drawString("_", 357, 472); while(key != 'J' || key != 'A' || key != 'V' || key != 'j' || key != 'a' || key != 'v') { key = c.getChar(); if (key == 'J' || key == 'j') { c.setFont(new Font("arial", Font.BOLD, 50)); c.setColor(Color.BLACK); c.drawString("J", 250, 470); } else if (key == 'A' || key == 'a') { c.setFont(new Font("arial", Font.BOLD, 50)); c.setColor(Color.BLACK); c.drawString("A", 285, 470); c.drawString("A", 355, 470); } else if (key == 'V' || key == 'v') { c.setFont(new Font("arial", Font.BOLD, 50)); c.setColor(Color.BLACK); c.drawString("V", 320, 470); } else if(key != 'J' || key != 'A' || key != 'V' || key != 'j' || key != 'a' || key != 'v') { COUNTER(); if(counter == 11) { c.clear(); c.setFont(new Font("arial", Font.PLAIN, 25)); c.setColor(Color.BLACK); c.drawString("You him! Try the next word!", 125, 250); c.setColor(Color.RED); c.drawString("killed", 174, 250); c.setFont(new Font("arial", Font.PLAIN, 15)); c.setColor(Color.BLACK); c.drawString("The next word will automatically start in 5 seconds.", 150, 275); WAIT(); c.clear(); counter = 0; STAND(); WORD2(); } } } }
- 05-12-2011, 08:08 PM #2
A quick observation on a line of code:
(key != 'J' || key != 'A' || key != 'V' || key != 'j' || key != 'a' || key != 'v')
If key is 'A' it is NOT 'J'. In other words this if will always be true
However being at the end of the if/else if chain it could be OK in this instance.
A GOOD IDEA - Always add a final else at the end of a chain of if/else if statements that prints out when all of the above if/else if statements were false.
Nesting the call to WORD2 inside of the WORD1 method could be a problem. Have WORD1 return a value to a higher level method that would call WORD2Last edited by Norm; 05-12-2011 at 08:11 PM.
- 05-12-2011, 10:44 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 28
- Rep Power
- 0
Alright, I'm not sure how I'd go around that though.
- 05-12-2011, 10:46 PM #4
It will probably take some thought and a redesign.
- 05-12-2011, 10:55 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 28
- Rep Power
- 0
Probably. :p
- 05-12-2011, 11:58 PM #6
Member
- Join Date
- Nov 2010
- Posts
- 28
- Rep Power
- 0
Would something like this work?
Java Code:if(key == 'j' && key == 'a' && key == 'v') { WORD2(); }
- 05-13-2011, 12:01 AM #7
How can the variable key have three different values at the same time? You need a different way to store the 3 letters so that you can test if they are J, A and V. For example: Either concatenate then in a string or store them in an array.
- 05-13-2011, 12:26 AM #8
Member
- Join Date
- Nov 2010
- Posts
- 28
- Rep Power
- 0
Okay, if I were to store them in an array, how would I call upon it so that if they pressed those three keys, move to WORD2()?
- 05-13-2011, 12:30 AM #9
Using something like this:
Java Code:if(key[0] == 'j' && key[1] == 'a' && key[2] == 'v')
- 05-13-2011, 12:40 AM #10
Member
- Join Date
- Nov 2010
- Posts
- 28
- Rep Power
- 0
I'm a beginner, so please bear with me. I've tried doing this. It works but it only moves onto the next word (WORD2()) if I click a different letter on the keyboard.
Java Code:public static void WORD1() { //Java [b][COLOR="Red"]char[] JAVA = {'j','a','v'};[/COLOR][/b] char key = ' '; c.setFont(new Font("arial", Font.PLAIN, 50)); c.setColor(Color.BLACK); c.drawString("_", 251, 472); c.drawString("_", 287, 472); c.drawString("_", 322, 472); c.drawString("_", 357, 472); while(key != 'J' || key != 'A' || key != 'V' || key != 'j' || key != 'a' || key != 'v') { key = c.getChar(); if (key == 'J' || key == 'j') { c.setFont(new Font("arial", Font.BOLD, 50)); c.setColor(Color.BLACK); c.drawString("J", 250, 470); } else if (key == 'A' || key == 'a') { c.setFont(new Font("arial", Font.BOLD, 50)); c.setColor(Color.BLACK); c.drawString("A", 285, 470); c.drawString("A", 355, 470); } else if (key == 'V' || key == 'v') { c.setFont(new Font("arial", Font.BOLD, 50)); c.setColor(Color.BLACK); c.drawString("V", 320, 470); } [b][COLOR="Red"]else if(JAVA[0] == 'j' && JAVA[1] == 'a' && JAVA[2] == 'v') { c.clear(); counter = 0; STAND(); WORD2(); }[/COLOR][/b] else if(key != 'J' || key != 'A' || key != 'V' || key != 'j' || key != 'a' || key != 'v') { COUNTER(); if(counter == 11) { c.clear(); c.setFont(new Font("arial", Font.PLAIN, 25)); c.setColor(Color.BLACK); c.drawString("You him! Try the next word!", 125, 250); c.setColor(Color.RED); c.drawString("killed", 174, 250); c.setFont(new Font("arial", Font.PLAIN, 15)); c.setColor(Color.BLACK); c.drawString("The next word will automatically start in 5 seconds.", 150, 275); WAIT(); c.clear(); counter = 0; STAND(); WORD2(); } } } }
- 05-13-2011, 02:43 AM #11
(key != 'J' || key != 'A' || key != 'V' || key != 'j' || key != 'a' || key != 'v')
This condition will always be true.
- 05-13-2011, 02:51 AM #12
Member
- Join Date
- Nov 2010
- Posts
- 28
- Rep Power
- 0
- 05-13-2011, 03:03 AM #13
If key is equal to 'J' then key != 'A' is true.
Since the comparisons are connected by ORs if any one is true then true is returned for the full conditional expression. The condition as you coded it will always be true.
What is the logic for the WORD1 method? Is it supposed to loop on some conditions? If so you need a loop of some kind. If not, remove the while loop. You need to have a design for the method before you code it.
- 05-13-2011, 03:35 AM #14
Member
- Join Date
- Nov 2010
- Posts
- 28
- Rep Power
- 0
There is a loop and all that's there is:
Java Code:public static void main(String[] args) { STAND(); WORD1(); WORD2(); WORD3(); WORD4(); WORD5(); }
- 05-13-2011, 03:43 AM #15
I have no idea what the code you posted is supposed to show or do.
The code shows 6 methods being called one after the other. No logic or loops.
- 05-13-2011, 04:08 AM #16
Member
- Join Date
- Nov 2010
- Posts
- 28
- Rep Power
- 0
I have it all working fine, the only puzzle piece missing is getting it from one word to the other when the word is complete.
- 05-13-2011, 02:57 PM #17
Not sure what this means. Your terminology in confusing.getting it from one word to the other when the word is complete
Do you mean that when one method (eg WORD1) finishes
you want the execution to go to the next method( WORD2)?
With the main() method as you show it, if the WORD1 method returns, then the WORD2 method would be called. When it returns, the WORD3 method would be called, etc
- 05-14-2011, 07:03 PM #18
Member
- Join Date
- Nov 2010
- Posts
- 28
- Rep Power
- 0
Yes, that is what I mean, I'll try returning but I've never used them. :/
- 05-14-2011, 11:27 PM #19
Time to learn how.
See Cross-post at:
http://www.daniweb.com/software-deve...threads/364323Last edited by Norm; 05-15-2011 at 12:00 AM.
- 05-15-2011, 12:42 AM #20
Member
- Join Date
- Nov 2010
- Posts
- 28
- Rep Power
- 0
Similar Threads
-
hangman
By coltragon in forum New To JavaReplies: 2Last Post: 01-16-2010, 09:56 AM -
hangman
By javaMike in forum Advanced JavaReplies: 2Last Post: 11-14-2009, 09:06 AM -
Hangman GUI help
By kurt in forum New To JavaReplies: 5Last Post: 05-22-2009, 10:22 AM -
Need help with Hangman!!!
By chinasome in forum New To JavaReplies: 10Last Post: 11-09-2008, 04:42 AM -
Hangman Help!!!
By chinasome in forum New To JavaReplies: 5Last Post: 11-08-2008, 02:30 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks