summaryrefslogtreecommitdiff
path: root/pos.rb
diff options
context:
space:
mode:
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