class BattleSnake::Context

Overview

A BattleSnake::Context is the representation of the game as it arrives from the Webhook API request to src/app.cr endpoints.

The context's key method is #valid_moves

Included Modules

Defined in:

battle_snake/context.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(pull : JSON::PullParser) #

[View source]

Instance Method Detail

def blast_valid_moves! #

Similar to BattleSnake::Context#valid_moves but considers all valid moves from enemies. Returns a hash with all the valid :moves, :neighbors and :risky_moves (we might collide with enemy) available for #you.

:moves is an Array(BattleSnake::Point) that containts the directions from the given #point that are valid to move without dying ("up"/"left"/"down"/"right").

:risky_moves is an Array(BattleSnake::Point) that containts the directions from the given #point that are valid to move but there's a chance we could die ("up"/"left"/"down"/"right").

:neighbors is a {} of String => BattleSnake::Point that contains those directions' coordinates.


[View source]
def board : Board #

[View source]
def board=(board : Board) #

[View source]
def check_collisions #

Checks collisions from snakes on the board and removes snakes that die


[View source]
def dup(bump_turn = true) #

Method that returns a new BattleSnake::Context copy of the instance. Can pass in false as parameter to avoid incrementing the context's turn (i.e. context.dup(false)).


[View source]
def enemies #

Returns an Array of BattleSnake::Snake snakes from the context that aren't #you.


[View source]
def game : Game #

[View source]
def game=(game : Game) #

[View source]
def lost? #

Returns true when #you is dead (lost the game), returns false otherwise.


[View source]
def move(snake_id, direction, pop_body = true) #

Simulate a move of a snake by id in some direction. Optional param pop_body that defaults as true. If false it won't pop the body of the snake being moved (sometimes snakes may have been popped already)


[View source]
def turn : Int32 #

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

[View source]
def valid_moves(point : Point) #

Returns a hash with all the valid :moves and :neighbors available from a given BattleSnake::Point.

:moves is an Array(BattleSnake::Point) that containts the directions from the given #point that are valid to move without dying ("up"/"left"/"down"/"right").

:neighbors is a {} of String => BattleSnake::Point that contains those directions' coordinates.

Example:

context.valid_moves(Point.new(1,1))
=> {
  moves: [ "up", "right" ],
  neighbors: { Point.new(2,1), Point.new(1,2) }
}

NOTE A common method to help manipulate the results is BattleSnake::Point#move?. An example of this in practice is the Strategy::Utils.a_star method implementation.

TODO Take into account the last point of snakes that will move on next turn, which would be in fact valid moves (not counted at the moment).


[View source]
def won? #

Returns true if #you won, returns false otherwise.


[View source]
def you : Snake #

[View source]
def you=(you : Snake) #

[View source]