1

I am trying to have xpos and ypos change based on the direction the player enters. The program succeeds in asking Where to? but the position does not change. The script also always defaults to the last condition (puts("You're drunk.")).

For example:

> n
0
1

However, my output is:

> n
You're drunk.
0
0

This is my current code:

north = "n"
west = "w"
south = "s"
east = "e"
xpos = 0
ypos = 0

puts("WHERE TO?")
compass = gets

if compass == north
    ypos = ypos + 1
elseif compass == west
    xpos = xpos - 1
elseif compass == south
    ypos = ypos - 1
elseif compass == east
    xpos = xpos + 1
else
    puts("You're drunk.") #if the player does not submit a proper direction

    puts(xpos) #for debugging. prints zero when called showing the player has not moved
    puts(ypos)
end
3
  • Just a small side comment -- in Ruby, you don't always need the parentheses around function arguments. For examples, most of your puts(...) here can become puts .... I've noticed that this seems to the the Ruby way to do things, though your code is still correct. Commented Apr 7, 2015 at 3:15
  • 1
    You forgot to chomp! your input. Commented Apr 7, 2015 at 3:17
  • Another side note: It's elsif, not elseif, at least as far as I know. Commented Apr 7, 2015 at 3:19

2 Answers 2

1

As others have pointed out, it's probably the missing chomp and the elseif.

Just a suggestion, but this cries out for a case statement:

case compass
  when 'north'
    ypos = ypos + 1
  when 'west'
    xpos = xpos - 1
  when 'south'
   ypos = ypos - 1
  when 'east'
   xpos = xpos + 1
 else
  puts("You're drunk.") #if the player does not submit a proper direction
  puts(xpos)#for debugging. prints zero when called showing the player has not moved
  puts(ypos)
end
Sign up to request clarification or add additional context in comments.

Comments

0

It looks like you have two main issues, at least that I can see. First, elseif should be elsif. I'm surprised you aren't getting a syntax error on that.

Second, Kernel::gets returns the next line entered, up to and including the linebreak. This means that you have to remove the linebreak at the end, so that comparing with strings that don't have \r\n or \n, depending on your platform, can be compared. This is very simple: Just replace

compass = gets

with

compass = gets.chomp

(String#chomp reference)

Alternatively, if you'd like to get rid of all whitespace before and after, you could use

compass = gets.strip

(String#strip reference)

2 Comments

It's best to use chomp in this situation since it will only remove the record separator in the case that the user wants to keep white space.
@squiguy It depends on the case. For example, if OP wants to make sure that even accidental spaces don't influence which option is selected, then strip is best. However, if he wants whitespace, then yes, chomp is best.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.