class BattleSnake::Point
- BattleSnake::Point
- Reference
- Object
Overview
Represents a Point (x,y) coordinate on the board with some helper methods for
processing of the board (i.e. #move?)
Included Modules
- Comparable(BattleSnake::Point)
- JSON::Serializable
Defined in:
battle_snake/point.crConstructors
-
.new(x : Int32, y : Int32)
Initialize from two Int32 values
- .new(pull : JSON::PullParser)
-
.new(str : String)
Initialize from a string representation i.e.
Instance Method Summary
-
#<=>(other : Point)
The comparison operator.
-
#down
Returns a new Point directly down from the instance
-
#left
Returns a new Point directly left from the instance
-
#move(direction)
Returns the point when it moves in a direction.
-
#move?(target : Point)
Determines if a given target BattleSnake::Point is reachable.
-
#right
Returns a new Point directly right from the instance
-
#to_s
Returns the String representation of the Point.
-
#up
Returns a new Point directly up from the instance
- #x : Int32
- #x=(x : Int32)
- #y : Int32
- #y=(y : Int32)
Constructor Detail
Instance Method Detail
The comparison operator. Returns 0 if the two objects are equal,
a negative number if this object is considered less than other,
a positive number if this object is considered greater than other,
or nil if the two objects are not comparable.
Subclasses define this method to provide class-specific ordering.
The comparison operator is usually used to sort values:
# Sort in a descending way:
[3, 1, 2].sort { |x, y| y <=> x } # => [3, 2, 1]
# Sort in an ascending way:
[3, 1, 2].sort { |x, y| x <=> y } # => [1, 2, 3]
Returns the point when it moves in a direction. Works the same way as #up, left, etc. but with a string parameter so it's easier to manipulate
Determines if a given target BattleSnake::Point is reachable. It returns
the direction the point itself needs to move to get to the target. It
returns an empty string if unreachable in one move.
NOTE The result of this method is a mathematic/geometric operation and does not take into account the current board/game, i.e. using negative numbers will return valid results but aren't needed or practical.
Example:
Point.new(1,1).move?(Point.new(1,2))
=> "right"
Point.new(2,1).move?(Point.new(1,1))
=> "up"
# Unreachable in one move
Point.new(1,1).move?(Point.new(3,3))
=> ""
# Valid result but not used in real scenarios
Point.new(-20,-10).move?(Point.new(-20,-11))
=> "down"