class BattleSnake::Context
- BattleSnake::Context
- Reference
- Object
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
- JSON::Serializable
Defined in:
battle_snake/context.crConstructors
Instance Method Summary
-
#blast_valid_moves!
Similar to
BattleSnake::Context#valid_moves
but considers all valid moves from enemies. - #board : Board
- #board=(board : Board)
-
#check_collisions
Checks collisions from snakes on the board and removes snakes that die
-
#dup
Returns a shallow copy of this object.
- #enemies
- #game : Game
- #game=(game : Game)
-
#move(snake_id, direction, pop_body = true)
Simulate a move of a snake by id in some
direction
. - #turn : Int32
- #turn=(turn : Int32)
-
#valid_moves(point : Point)
Returns a hash with all the valid
:moves
and:neighbors
available from a givenBattleSnake::Point
. - #you : Snake
- #you=(you : Snake)
Constructor Detail
Instance Method Detail
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.
Returns a shallow copy of this object.
This allocates a new object and copies the contents of
self
into it.
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)
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).