summaryrefslogtreecommitdiff
path: root/pos.rb
diff options
context:
space:
mode:
authorNoah Loomans <noahloomans@gmail.com>2018-04-21 20:39:35 +0200
committerNoah Loomans <noahloomans@gmail.com>2018-04-21 20:39:35 +0200
commit811e453013ec548a3750c1fcb39f36fd4cd77dcf (patch)
treeed9166ea4aee8619fc979a45e9fefb7b68ae863d /pos.rb
parent6be27e99f15c5668f260ef7fe874400e43616c07 (diff)
Split program into multiple files
Diffstat (limited to 'pos.rb')
-rw-r--r--pos.rb34
1 files changed, 34 insertions, 0 deletions
diff --git a/pos.rb b/pos.rb
new file mode 100644
index 0000000..2712931
--- /dev/null
+++ b/pos.rb
@@ -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