This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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:
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
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
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)
Joined: Oct 19, 2003
Thanks a lot for your ideas. I will try them out and hopefull get it working correctly.
Joined: Oct 19, 2003
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.