My Nim game for school wont stop properly
Author |
Message |
firewall156
|
Posted: 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>
Turing: |
var total, turn, balls, y, count, cmove, t ,location: int := 0
randint (total, 30, 30)
put "Total:", total
for u : 1 .. total
drawfilloval (u * 21, 300, 10, 10, 9)
end for
put "You can take from 1-3 from the total"
loop
loop
locate (10, 10)
turn := 1
put "How many do you wish to take?"
get balls
if balls > 3 or balls < 1 then
put "Invalid, Enter a number from one to three"
else
put "Removed: ", balls
end if
total := total - balls
exit when balls >= 1 and balls <= 3
end loop
for u : 1 .. balls
if whatdotcolor (u * 21, 300) = 9 then
delay (20)
drawfilloval (u * 21, 300, 10, 10, 0)
t := t + 1
end if
exit when t > 2
end for
delay (1000)
put "Location:", location
loop
turn := 2
randint (cmove, 1, 3)
locate (15, 10)
put "computer takes: ", cmove
exit when balls >= 1 and balls <= 3
end loop
for u : 1 .. total
if whatdotcolor (location * 21, 300) = 9 then
delay (20)
drawfilloval (location * 21, 300, 10, 10, 0)
t := t + 1
end if
exit when t > 2
end for
end loop
|
Please specify what version of Turing you are using
Turing 4.1.1 |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
DemonWasp
|
Posted: 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. |
|
|
|
|
 |
firewall156
|
Posted: 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 |
|
|
|
|
 |
Tony

|
Posted: 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. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
firewall156
|
Posted: 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 |
|
|
|
|
 |
Tony

|
Posted: Sun Oct 12, 2014 7:50 pm Post subject: RE:My Nim game for school wont stop properly |
|
|
code: |
put "before loop"
loop
exit
end loop
put "after loop"
|
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. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
firewall156
|
Posted: 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 |
|
|
|
|
 |
Tony

|
Posted: 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
code: |
loop
randint (total, 15, 30)
loop
exit when again = "n"
loop
turn := 1
...
|
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
code: |
loop
put "starting loop A"
exit when <condition>
put "not exiting; <condition> is false"
...
put "repeating loop A"
end loop
put "finished loop A"
|
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. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
Sponsor Sponsor

|
|
 |
|
|