Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
256 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require_relative 'point2d' | ||
|
||
class North | ||
def right() East.new end | ||
def left() West.new end | ||
def reverse() South.new end | ||
def vec() Point2D.new(0,-1) end | ||
|
||
def ==(other) other.is_a?(North) end | ||
def coerce(other) return self, other end | ||
end | ||
|
||
class East | ||
def right() South.new end | ||
def left() North.new end | ||
def reverse() West.new end | ||
def vec() Point2D.new(1,0) end | ||
|
||
def ==(other) other.is_a?(East) end | ||
def coerce(other) return self, other end | ||
end | ||
|
||
class South | ||
def right() West.new end | ||
def left() East.new end | ||
def reverse() North.new end | ||
def vec() Point2D.new(0,1) end | ||
|
||
def ==(other) other.is_a?(South) end | ||
def coerce(other) return self, other end | ||
end | ||
|
||
class West | ||
def right() North.new end | ||
def left() South.new end | ||
def reverse() East.new end | ||
def vec() Point2D.new(-1,0) end | ||
|
||
def ==(other) other.is_a?(West) end | ||
def coerce(other) return self, other end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class Point2D | ||
attr_accessor :x, :y | ||
|
||
def initialize(x, y) | ||
@x = x | ||
@y = y | ||
end | ||
|
||
def self.from_string(string) | ||
coords = string.split.map(&:to_i) | ||
Point2D.new(coords[0], coords[1]) | ||
end | ||
|
||
def +(other) | ||
if other.is_a?(Point2D) | ||
return Point2D.new(@x+other.x, @y+other.y) | ||
end | ||
end | ||
|
||
def coerce(other) | ||
return self, other | ||
end | ||
|
||
def to_s | ||
"#{@x} #{@y}" | ||
end | ||
|
||
def pretty | ||
"(% d,% d)" % [@x, @y] | ||
end | ||
end |