
-----------------------------------
firewall156
Sat Oct 11, 2014 8:01 pm

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)




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  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  2
    end for
end loop




Please specify what version of Turing you are using
Turing 4.1.1

-----------------------------------
DemonWasp
Sat Oct 11, 2014 11:40 pm

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
Sun Oct 12, 2014 10:34 am

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 