aspose file tools*
The moose likes Beginning Java and the fly likes Help with Applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with Applet" Watch "Help with Applet" New topic
Author

Help with Applet

Tom Warner
Greenhorn

Joined: Oct 19, 2003
Posts: 7
Hi everyone,
I'm new to Java and have to make an applet for the game of Nim. Players take turns taking sticks(1, 2, or 3) and the player whose turn it is when only one stick remains loses. I've been trying everything but cant get it to run properly. I have to frames, one so the user can pick how many sticks to play with and the other to determine whether the user or the computer goes first. I cant get it to pause after the computer's turn so the user can pick how many sticks to take away. Any help would be greatly appreciated.
Heres the code so far:
Michael Fitzmaurice
Ranch Hand

Joined: Aug 22, 2001
Posts: 168
This code is not legal - it has non-matching braces and it will not compile. Looks like something went wrong with the formatting when you pasted this in.
Try to paste the code in exactly as it appears in your editor (assuming that version compiles successfully).
M


"One good thing about music - when it hits, you feel no pain" <P>Bob Marley
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
I ran your applet (as an Application) and I got 2 frames popping up at the beginning - once I resized them, one asked for heads or tails. One asked for how many sticks. I think you really want the heads or tails to display first, THEN show how many sticks.
I think what you may want to do is sit down with a piece of paper and figure out exactly how you want the program to work (which frames popup when) and how you want it to look. Once you have a solid design, it will be much easier to to program it. Maybe even do a small flowchart that shows what will happen when.
Also, you really should name all of you variables something useful. You did that for some variables, but then for others you didn't. For example, you have 2 different "Go" buttons named go1 and go2. I would suggest naming them something more descriptive like headsOrTailsGo and numberOfSticksGo or something like that. Makes it much easier for others to understand your code.
Good luck!
brian


My Java Freeware:<br />MACCC - <a href="http://maccc.pipasoft.com" target="_blank" rel="nofollow">http://maccc.pipasoft.com</a><br />Nator - <a href="http://nator.pipasoft.com" target="_blank" rel="nofollow">http://nator.pipasoft.com</a>
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
I would make everything dependent on the player's choice of sticks. Have methods like doComputerTurn() and doPlayerTurn(). You don't need a while loop - just wait til the player chooses sticks to do anything. Once in the actionPerformed (he/she chose number of sticks) - call doPlayerTurn(). Assuming the game is not over, you always call doComputerTurn() after doPlayerTurn() (actually, call it from doPlayerTurn()). You probabably want another method called something like isGameOver() that is called after every turn to see if the game is over and if so, announce the winner and end the game.
so your logic is somthing like:
1) doPlayerTurn()
2) isGameOver()
2a) if gameover - endGame()
3) doComputerTurn()
4) isGameOver()
4a) if gameover - endGame()
5) goto 1)

brian
Tom Warner
Greenhorn

Joined: Oct 19, 2003
Posts: 7
Thanks a lot for your ideas. I will try them out and hopefull get it working correctly.
Tom Warner
Greenhorn

Joined: Oct 19, 2003
Posts: 7
I decided to get rid of the frames and used JOptionPane's instead. I also took Brian's advice and used methods. Im still having trouble making it pause so the user can pick how many sticks after each turn. Should I get rid of the three buttons and use a TextField instead? Thanks for your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with Applet