Class: Battlesnake::Snake
Overview
Represents a single Battlesnake player.
Instance Attribute Summary collapse
-
#as_json ⇒ Hash
readonly
Snake as a data structure usable by other objects.
-
#body ⇒ Array<Location>
readonly
Locations occupied by the body of the snake.
-
#customizations ⇒ Hash
readonly
Display customizations.
-
#head ⇒ Location
readonly
Location of snake head.
-
#health ⇒ Integer
readonly
Health value between 0 and 100.
-
#id ⇒ String
readonly
Unique string which identifies the player.
-
#latency ⇒ Integer
readonly
Latency of given player's API, nil if not provided.
-
#length ⇒ Integer
readonly
Number of locations occupied by snake body.
-
#name ⇒ String
readonly
Name of the player.
-
#shout ⇒ String
readonly
Message to other players.
-
#squad ⇒ String
readonly
Squad identifer for games played with teams.
Instance Method Summary collapse
-
#direction ⇒ String
The current direction the snake is facing, based on the first two segments of the body.
-
#initialize(json_or_hash) ⇒ Snake
constructor
Returns a new instance of Snake.
Methods inherited from Base
Constructor Details
#initialize(json_or_hash) ⇒ Snake
Returns a new instance of Snake.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/battlesnake/snake.rb', line 47 def initialize(json_or_hash) @as_json = json_or_hash.is_a?(String) ? JSON.parse(json_or_hash) : json_or_hash @id = @as_json['id'] @name = @as_json['name'] @latency = @as_json['latency'].empty? ? nil : @as_json['latency'].to_i @health = @as_json['health'] @length = @as_json['length'] @shout = @as_json['shout'] @squad = @as_json['squad'] @customizations = @as_json['customizations'] @body = @as_json['body'].map{ |coords| Location.new(coords) } @head = Location.new(@as_json['head']) end |
Instance Attribute Details
#as_json ⇒ Hash (readonly)
Returns snake as a data structure usable by other objects.
8 9 10 |
# File 'lib/battlesnake/snake.rb', line 8 def as_json @as_json end |
#body ⇒ Array<Location> (readonly)
Returns locations occupied by the body of the snake.
17 18 19 |
# File 'lib/battlesnake/snake.rb', line 17 def body @body end |
#customizations ⇒ Hash (readonly)
Returns display customizations.
38 39 40 |
# File 'lib/battlesnake/snake.rb', line 38 def customizations @customizations end |
#head ⇒ Location (readonly)
Returns location of snake head. Should be the same as body.
26 27 28 |
# File 'lib/battlesnake/snake.rb', line 26 def head @head end |
#health ⇒ Integer (readonly)
Returns health value between 0 and 100.
23 24 25 |
# File 'lib/battlesnake/snake.rb', line 23 def health @health end |
#id ⇒ String (readonly)
Returns unique string which identifies the player.
11 12 13 |
# File 'lib/battlesnake/snake.rb', line 11 def id @id end |
#latency ⇒ Integer (readonly)
Returns latency of given player's API, nil if not provided.
20 21 22 |
# File 'lib/battlesnake/snake.rb', line 20 def latency @latency end |
#length ⇒ Integer (readonly)
Returns number of locations occupied by snake body.
29 30 31 |
# File 'lib/battlesnake/snake.rb', line 29 def length @length end |
#name ⇒ String (readonly)
Returns name of the player.
14 15 16 |
# File 'lib/battlesnake/snake.rb', line 14 def name @name end |
#shout ⇒ String (readonly)
Returns message to other players.
32 33 34 |
# File 'lib/battlesnake/snake.rb', line 32 def shout @shout end |
#squad ⇒ String (readonly)
Returns squad identifer for games played with teams.
35 36 37 |
# File 'lib/battlesnake/snake.rb', line 35 def squad @squad end |
Instance Method Details
#direction ⇒ String
The current direction the snake is facing, based on the first two segments of the body. If snake only has a head, no direction can be determined, so nil is returned.
69 70 71 72 |
# File 'lib/battlesnake/snake.rb', line 69 def direction return @direction if defined?(@direction) @direction = length > 1 ? body[1].direction(head) : nil end |