0

I'm not a skillful programmer by any means, but everyone's got to start somewhere. I've been trying to make a (very) basic maze-type in ruby, but I'm having difficulty with the while loop not exiting.

Room layout is an upside down t:

 5
 2
103
 4

Going north twice from the center should change @loc to 5, say "End", and exit the loop:

elsif @loc == 2
@loc = 5
puts "End"

But it returns to the beginning of the while loop, stranding the player.

EDIT: there's some confusion about the code, so I'm removing the block and pointing you to http://pastebin.com/EFWVBAhn

4

1 回答 1

1

The line

while @loc != 5

is accessing a variable which is not being changed. The changes are happening to cmd.loc, which is not the same variable. That line needs to be

while cmd.loc != 5

in order to access the variable.

Also you need to quote your strings (e.g. if command == west should be if command == "west"); otherwise you're telling Ruby to compare to a variable called west, which doesn't exist, rather than the string "west".

于 2013-03-04T23:51:24.230 回答