Computer Science Canada My Nim game for school wont stop properly |
Author: | firewall156 [ Sat Oct 11, 2014 8:01 pm ] | ||
Post subject: | My Nim game for school wont stop properly | ||
What is it you are trying to achieve? I am trying to program a nim game. The description of the game is: The game of Nim starts with a random number of stones between 15 and 30. Two players alternates turns and on each turn may take either 1, 2 or 3 stones from the pile. The player forced to take the last stone loses. Create a Nim application that allows the user to play against the computer. In this version of the game, the application generates the number of stones to begin with, the number of stones the computer takes and the user goes first. Include code that prevents the user and computer from taking an illegal number of stones. For example, neither should be allowed to take three stones when there are only 1 or 2 left. What is the problem you are having? the game dosen't stop properly. Describe what you have tried to solve this problem I have tried putting exit when commands in different spots so it will stop but it dosen't work. Post any relevant code (You may choose to attach the file instead of posting the code if it is too long) <Answer Here>
Please specify what version of Turing you are using Turing 4.1.1 |
Author: | DemonWasp [ Sat Oct 11, 2014 11:40 pm ] |
Post subject: | RE:My Nim game for school wont stop properly |
The exit when statement only applies to the innermost loop that the statement appears inside. Since all of your exit when statements appear inside an inner, nested loop, they will only exit those loops, not the longer "main" loop. You will have to find some way to "pass" an "is the game over?" message to the outer loop. |
Author: | firewall156 [ Sun Oct 12, 2014 10:34 am ] |
Post subject: | Re: My Nim game for school wont stop properly |
NOTE!!!!!!!! i posted the wrong code!!!!!! the correct code is this: var total, balls, continue, turn, cballs : int := 0 var again,sure : string := "nm" loop randint (total, 15, 30) loop exit when again = "n" put "Total:", total put "How much do you wish to remove?(1-3)" loop turn := 1 if total = 1 and turn = 1 then cls put "YOU LOST" put "Play Again? (y/n)" get again continue := 56 end if get balls if balls >= 1 and balls <= 3 then continue := 56 else put "Invalid amount,Try again" end if exit when continue = 56 end loop exit when again = "n" total := total - balls put "now the total is:", total turn := 2 if total = 1 and turn = 2 then cls put "YOU WIN" put "Play Again? (y/n)" get again exit when again = "n" cballs := 0 elsif total = 4 and turn = 2 then cballs := 3 elsif total = 3 and turn = 2 then cballs := 2 elsif total = 2 and turn = 2 then cballs := 1 elsif total = 6 and turn = 2 then cballs := 1 else randint (cballs, 1, 3) end if put "The computer removes:", cballs total := total - cballs end loop put "are you sure?(y/n)" get sure exit when sure = "n" end loop |
Author: | Tony [ Sun Oct 12, 2014 12:51 pm ] |
Post subject: | RE:My Nim game for school wont stop properly |
Most of that code doesn't have to do with looping or existing. Post just the relevant parts (so no game, only the loop/exit structure, and minimum necessary to demo the problem). Also, describe what you mean by "the game dosen't stop properly." -- we might have a different idea of what "proper" is. |
Author: | firewall156 [ Sun Oct 12, 2014 3:10 pm ] |
Post subject: | Re: My Nim game for school wont stop properly |
the game will continue with other parts of the cod no matter how many exit when commands i put in. it should be asking if you want to play again but it continues asking for different things like for more balls to remove. the "are you sure" at the end was added as an attempt to fix the problem |
Author: | Tony [ Sun Oct 12, 2014 7:50 pm ] | ||
Post subject: | RE:My Nim game for school wont stop properly | ||
One exit is clearly enough. Throwing more statements, without really knowing where you are putting them is a poor approach. As I've said above -- post just the structure of your loops, without getting distracted by the rest of the game. Once you have the minimum amount of code, it will be very easy to see exactly what's going on. |
Author: | firewall156 [ Sun Oct 12, 2014 8:58 pm ] |
Post subject: | Re: My Nim game for school wont stop properly |
I think i cut out what was unnecessary. hope it helps you. thanks again! loop randint (total, 15, 30) loop exit when again = "n" loop turn := 1 if total = 1 and turn = 1 then put "Play Again? (y/n)" get again continue := 56 end if get balls if balls >= 1 and balls <= 3 then continue := 56 else put "Invalid amount,Try again" end if exit when continue = 56 end loop exit when again = "n" total := total - balls turn := 2 if total = 1 and turn = 2 then put "Play Again? (y/n)" get again exit when again = "n" total := total - cballs end loop put "are you sure?(y/n)" get sure exit when sure = "n" end loop |
Author: | Tony [ Sun Oct 12, 2014 9:16 pm ] | ||||
Post subject: | Re: My Nim game for school wont stop properly | ||||
firewall156 @ Sun Oct 12, 2014 8:58 pm wrote: hope it helps you.
You are mistaken. It's supposed to help you. What you should notice is that right at the start
You are at least 3 nested loops in. Which means you'd need at least 3 exits to correctly occur to get all the way out. It might be difficult to keep track of where exactly in the code the execution is happening, but a helpful technique that I use is along the lines of
Then you get a steam of execution that you can compare against what your expectations. Since you have so many loops going at the same time, you'd need labels to be distinct. |