Ruby数组中的基数方向
我正在尝试创建一个程序,当一个人向左和向右转弯时(例如,如果他们朝北和向左转弯,他们将朝北),该程序将跟踪他们的方位。我将其作为一个数组编写,该数组以两种方式工作。以下是我目前的代码:Ruby数组中的基数方向,ruby,Ruby,我正在尝试创建一个程序,当一个人向左和向右转弯时(例如,如果他们朝北和向左转弯,他们将朝北),该程序将跟踪他们的方位。我将其作为一个数组编写,该数组以两种方式工作。以下是我目前的代码: puts "What is your initial bearing?" bearing = gets.chomp puts "And what turns do you want to make?" turn_list = gets.chomp cardinal_directions = ["N", "E"
puts "What is your initial bearing?"
bearing = gets.chomp
puts "And what turns do you want to make?"
turn_list = gets.chomp
cardinal_directions = ["N", "E", "S", "W"]
position = cardinal_directions.index(bearing)
turn_list.each |direction|
case "L"
position += 1
case "R"
position -= 1
end
但有一个明显的问题。假设我的人面向西方,想右转。我如何告诉它从索引3转到索引0?或者他们在北边想左转
请原谅糟糕的代码和格式,我是新手。非常感谢你的帮助 由于您有4个可能的选项,当您的变量除以4时,您可以使用模运算(即,获取余数):
case turn_list
when "R"
position = (position + 1)%4
when "L"
position = (position - 1)%4
end
%
符号是模运算符,当两个数字被除时,它将为您提供余数。这会给你你想要的答案。例如,如果您位于位置3(西)并右转,则添加1并除以4,然后获取余数,即0(北)
(我编辑了案例“R”和“L”,因为我认为您在问题中已将它们标记为向后。)您可以使用传递1(或不传递,因为1是默认参数)向右移动,使用-1向左移动,始终保持原始位置:
cardinal_directions = ["N", "E", "S", "W"]
# => ["N", "E", "S", "W"]
position = cardinal_directions.index('S')
# => 2
cardinal_directions.rotate!(1)
# => ["E", "S", "W", "N"]
cardinal_directions[position]
# => "W"
cardinal_directions.rotate!(1)
# => ["S", "W", "N", "E"]
cardinal_directions[position]
# => "N"
cardinal_directions.rotate!(1)
# => ["W", "N", "E", "S"]
cardinal_directions[position]
# => "E"
cardinal_directions.rotate!(-1)
# => ["S", "W", "N", "E"]
cardinal_directions[position]
# => "N"
cardinal_directions.rotate!(-1)
# => ["E", "S", "W", "N"]
cardinal_directions[position]
# => "W"
我建议你雇佣一个班级来记录每个人的举止:
class Wanderers
attr_accessor :name, :bearing
def initialize(name, initial_bearing, at_south_pole = false)
@name = name
@bearing = at_south_pole ? :north : initial_bearing
@at_south_pole = at_south_pole
end
def turn_left
return :north if @at_south_pole
@bearing =
case @bearing
when :north then :west
when :west then :south
when :south then :east
when :east then :north
end
end
def turn_right
return :north if @at_south_pole
@bearing =
case @bearing
when :north then :east
when :east then :south
when :south then :west
when :west then :north
end
end
end
然后:
请注意:这不是case语句的语法。只需使用if,或者下面是case的语法:“如果他们朝北,然后左转,他们将朝北。”这真的是你的意思吗?@steenslag,这可能是真的,取决于你站在哪里。@CarySwoveland southpole?有人给那个人(@steenslag)一支雪茄。给不知道的人一个提示:
%
是余数,或模算术运算符。例3%2=1或4%5=4。谢谢,@Linuxios。我会在回答中说得更清楚一点。还感谢您注意到原始问题中不正确的大小写
语法。没问题。我只是想如果他们不熟悉Ruby中的模运算,这可能会对OP有所帮助。@Linuxios:Ruby%
中的是模或模运算符,而不是余数运算符。在这种情况下(非负值),这并没有什么区别。@cremno:是的,正式地说。(在某些情况下)它可以被认为是一个余数运算符。这是一个非常好的解决方案。OP要求返回0
和3
之间的索引。当然,这是一个简单的改变,而且可能不是建议的改变,这取决于OP的应用程序。@CarySwoveland我只是展示另一种方式。另一个答案已经涵盖了OP如何使用模来获得索引。我看我忘了北极的情况。哦,好吧。
hector = Wanderers.new "Hector", :east
hector.turn_right #=> :south
hector.turn_right #=> :west
hector.turn_right #=> :north
hector.turn_right #=> :east
hector.turn_left #=> :north
hector.bearing #=> :north
lola = Wanderers.new "Lola", :north, true
lola.turn_right #=> :north
lola.turn_right #=> :north
lola.turn_left #=> :north
lola.bearing #=> :north