diff options
author | Noah Loomans <noahloomans@gmail.com> | 2018-04-21 20:39:35 +0200 |
---|---|---|
committer | Noah Loomans <noahloomans@gmail.com> | 2018-04-21 20:39:35 +0200 |
commit | 811e453013ec548a3750c1fcb39f36fd4cd77dcf (patch) | |
tree | ed9166ea4aee8619fc979a45e9fefb7b68ae863d /pos.rb | |
parent | 6be27e99f15c5668f260ef7fe874400e43616c07 (diff) |
Split program into multiple files
Diffstat (limited to 'pos.rb')
-rw-r--r-- | pos.rb | 34 |
1 files changed, 34 insertions, 0 deletions
@@ -0,0 +1,34 @@ +Pos = Struct.new(:x, :y) do + def move(dir) + case dir + when :up + Pos.new(x, y - 1) + when :right + Pos.new(x + 1, y) + when :down + Pos.new(x, y + 1) + when :left + Pos.new(x - 1, y) + end + end + + def dir_from(pos) + diff_x = x - pos.x + diff_y = y - pos.y + + case Pos.new(diff_x, diff_y) + when Pos.new(0, -1) + :up + when Pos.new(1, 0) + :right + when Pos.new(0, 1) + :down + when Pos.new(-1, 0) + :left + end + end + + def to_s() + "(#{x}, #{y})" + end +end |