Reputation: 3
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
Upvotes: 0
Views: 361
Reputation: 5490
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".
Upvotes: 1