-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.cr
47 lines (37 loc) · 1.17 KB
/
board.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
require "json"
# Represents the Board as it arrives from the BattleSnake API endpoint.
#
# *@snake_points* is an `Array(BattleSnake::Point)` variable populated when a
# Board is initialized and contains all the Points that are currently occupied
# by a snake (would represent a collision).
class BattleSnake::Board
include JSON::Serializable
@[JSON::Field(key: "height")]
property height : Int32
@[JSON::Field(key: "width")]
property width : Int32
@[JSON::Field(key: "snakes")]
property snakes : Array(Snake)
@[JSON::Field(key: "food")]
property food : Array(Point)
@[JSON::Field(key: "hazards")]
property hazards : Array(Point)
@snake_points = [] of Point
getter snake_points : Array(Point)
# Executed on `after_initialize` callback and all it does is populate
# snake_points variable (`Array(Point)`) with all points that belong to a
# snake on the board
def find_snake_points
snakes.each do |snake|
snake.body.each do |point|
@snake_points << point unless @snake_points.includes?(point)
end
end
end
def living?(id)
snakes.index { |snake| snake.id == id }
end
def after_initialize
find_snake_points
end
end