Results 1 to 8 of 8
Thread: Help with Program...Stone Game
- 06-12-2012, 07:05 AM #1
Member
- Join Date
- Jun 2012
- Posts
- 13
- Rep Power
- 0
- 06-12-2012, 08:44 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: Help with Program...Stone Game
Hi VettesRus, welcome to the forums!
When you post code, use the "code" tags: you put [code] at the start of the code and [/code] at the end. It also improves readability to use (a few) spaces to indent rather than tabs which can end up uncomfortably large when rendered on a web page. (For details see the bbcode faq.)
- 06-12-2012, 08:51 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: Help with Program...Stone Game
There is no loop in the code you posted.My problem is when I get back to the top of the loop my variables holding the remaining stones get reset when the user enters their next turn.
-----
As a matter of design you might consider writing smaller simpler methods. For example a method that performs the computer's turn, another that prompts the user and performs the human's turn. Then the main() method - complete with loop - would only have to put the other methods together. The advantage of such an approach is that by being simpler the individual parts are easier to test and easier to write correctly.
- 06-12-2012, 09:08 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: Help with Program...Stone Game
Also at javaprogrammingforums.
Please be forthright when cross posting. Forums are a place of discussion, and everybody deserves to know what else is being said. Bear in mind that some will be put off and unwilling to help if they suspect they might be wasting their time on a question answered elsewhere. And others annoyed by the suspicion that they are being treated as the gullible targets of a fishing expedition. Of course you are probably not intending to treat other forum participants this way: which is why it pays to be forthright.
- 06-12-2012, 09:17 AM #5
Member
- Join Date
- Jun 2012
- Posts
- 13
- Rep Power
- 0
Re: Help with Program...Stone Game
pbrockway you are on top of things huh lol. I appreciate the help and information though. For some reason I assumed cross posting just meant repeating old topics, I should have looked into it.
Also thank you for the code edit, I did not realize.
As far as the loop goes, I had a loop but deleted it right before I posted this because it kept changing the values of remaningStones. I should have left it for everyone to see, but did not plan on asking for help until I erased it. The only loop I can come up will ask the user how many stones they want to take when it is their turn, but when the user inputs the number, my variables adjust to the new stonesTaken. I cannot figure out how to loop it to keep subtracting the new user input without rejuvinating, if that makes any sense.
When I was running with my loop everything was fine, until the users second turn, then the stonesRemaining would jump back up to a higher value. I know this probably is hard to follow. I should just go to bed and work on it more tomorrow, then give you a better picture of what I have.
- 06-12-2012, 09:47 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: Help with Program...Stone Game
When you get a chance it *will* help people to see the loop as you have written it: problems and all.
Just wondering, but do you really need all those variables? Isn't the only important thing the number of stones remaining? And that does change as each play is made.
- 06-12-2012, 03:38 PM #7
Member
- Join Date
- Jun 2012
- Posts
- 13
- Rep Power
- 0
Re: Help with Program...Stone Game
Working on my loop now. But the user and computer can only take half of the stones left at a time so I need some of those variables. I am sure I have some unnecessary ones in there when I was just doing anything to get it to run correctly. If I can get a working loop I can most likely delete the newRemainingStones and even the compStonesTaken
- 06-12-2012, 04:55 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Help with Program...Stone Game
All you need is the number of stones left.
That's the only thing that needs to be remembered.
Things like the number taken by either the computer or the player are short-lived variables and so only need to be declared where they are used.
For example:
So the playerStones variable is only used inside the method associated with the players turn.Java Code:private void playerTurn() { // Some prompt requesting a number of stones from the player. int playerStones = <whatever code here, eg Scanner.nextInt()>; // Probably need some validating code here remainingStones -= playerStones; }
The computer would be a similar case.
The value of remainingStones will start at whatever the starting number of stines chosen is.Please do not ask for code as refusal often offends.
Similar Threads
-
How to run another program from within java coded game?
By Stefan. in forum New To JavaReplies: 8Last Post: 07-12-2011, 01:32 PM -
trying to write a program for hangman word game
By durdanto in forum New To JavaReplies: 1Last Post: 02-12-2011, 02:53 AM -
Stone, Paper, Scissors game help
By Harris68 in forum NetworkingReplies: 1Last Post: 12-19-2009, 04:33 PM -
plz i need this program in 2 days .it`s tic tac toe game
By amr in forum Java 2DReplies: 7Last Post: 07-21-2009, 02:39 AM -
Java Game Program
By software_dude_2009 in forum New To JavaReplies: 3Last Post: 02-22-2009, 04:31 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks