class BattleSnake::Point

Overview

Represents a Point (x,y) coordinate on the board with some helper methods for processing of the board (i.e. #move?)

Included Modules

Defined in:

battle_snake/point.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(x : Int32, y : Int32) #

Initialize from two Int32 values


[View source]
def self.new(pull : JSON::PullParser) #

[View source]
def self.new(str : String) #

Initialize from a string representation i.e. "x,y"


[View source]

Instance Method Detail

def <=>(other : Point) #
Description copied from module Comparable(BattleSnake::Point)

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]

[View source]
def down #

Returns a new Point directly down from the instance


[View source]
def left #

Returns a new Point directly left from the instance


[View source]
def move(direction) #

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


[View source]
def move?(target : Point) #

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"

[View source]
def right #

Returns a new Point directly right from the instance


[View source]
def to_s #

Returns the String representation of the Point. Example:

Point.new(2,2).to_s
=> "2,2"

[View source]
def up #

Returns a new Point directly up from the instance


[View source]
def x : Int32 #

[View source]
def x=(x : Int32) #

[View source]
def y : Int32 #

[View source]
def y=(y : Int32) #

[View source]