[2016][ruby][18.0] Row#next

profile
Alpha Chen 8 years ago
parent ba95efbf88
commit b4335dda72

@ -1,10 +1,26 @@
class Row Row = Struct.new(:tiles) do
INPUT_MAP = { ?. => :safe, ?^ => :trap } INPUT_MAP = { ?. => :safe, ?^ => :trap }
attr_reader :tiles def self.from_s(input)
new(input.chars.map {|char| INPUT_MAP.fetch(char) })
end
def initialize(input) def next
@tiles = input.chars.map {|char| INPUT_MAP.fetch(char) } self.class.new(
%i[ safe safe ].insert(1, *tiles).each_cons(3).map {|l,c,r|
if l == :trap && c == :trap && r == :safe
:trap
elsif l == :safe && c == :trap && r == :trap
:trap
elsif l == :trap && c == :safe && r == :safe
:trap
elsif l == :safe && c == :safe && r == :trap
:trap
else
:safe
end
}
)
end end
end end
@ -12,7 +28,9 @@ require 'minitest/autorun'
class TestRow < Minitest::Test class TestRow < Minitest::Test
def test_row def test_row
row = Row.new('..^^.') row = Row.from_s('..^^.')
assert_equal %i[ safe safe trap trap safe ], row.tiles assert_equal %i[ safe safe trap trap safe ], row.tiles
assert_equal Row.from_s('.^^^^'), row.next
end end
end end

Loading…
Cancel
Save