Class: Battlesnake::Snake

Inherits:
Object
  • Object
show all
Defined in:
lib/battlesnake/snake.rb

Overview

Represents a single Battlesnake player.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_or_hash) ⇒ Snake

Returns a new instance of Snake.

Parameters:

  • json_or_hash (String, Hash)

    can be a hash of attributes, or a JSON string which represents such a structure.



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_jsonHash (readonly)

Returns snake as a data structure usable by other objects.

Returns:

  • (Hash)

    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

#bodyArray<Location> (readonly)

Returns locations occupied by the body of the snake.

Returns:

  • (Array<Location>)

    locations occupied by the body of the snake.



17
18
19
# File 'lib/battlesnake/snake.rb', line 17

def body
  @body
end

#customizationsHash (readonly)

Returns display customizations.

Returns:

  • (Hash)

    display customizations.



38
39
40
# File 'lib/battlesnake/snake.rb', line 38

def customizations
  @customizations
end

#headLocation (readonly)

Returns location of snake head. Should be the same as body.

Returns:

  • (Location)

    location of snake head. Should be the same as body.



26
27
28
# File 'lib/battlesnake/snake.rb', line 26

def head
  @head
end

#healthInteger (readonly)

Returns health value between 0 and 100.

Returns:

  • (Integer)

    health value between 0 and 100.



23
24
25
# File 'lib/battlesnake/snake.rb', line 23

def health
  @health
end

#idString (readonly)

Returns unique string which identifies the player.

Returns:

  • (String)

    unique string which identifies the player.



11
12
13
# File 'lib/battlesnake/snake.rb', line 11

def id
  @id
end

#latencyInteger (readonly)

Returns latency of given player's API, nil if not provided.

Returns:

  • (Integer)

    latency of given player's API, nil if not provided.



20
21
22
# File 'lib/battlesnake/snake.rb', line 20

def latency
  @latency
end

#lengthInteger (readonly)

Returns number of locations occupied by snake body.

Returns:

  • (Integer)

    number of locations occupied by snake body.



29
30
31
# File 'lib/battlesnake/snake.rb', line 29

def length
  @length
end

#nameString (readonly)

Returns name of the player.

Returns:

  • (String)

    name of the player.



14
15
16
# File 'lib/battlesnake/snake.rb', line 14

def name
  @name
end

#shoutString (readonly)

Returns message to other players.

Returns:

  • (String)

    message to other players.



32
33
34
# File 'lib/battlesnake/snake.rb', line 32

def shout
  @shout
end

#squadString (readonly)

Returns squad identifer for games played with teams.

Returns:

  • (String)

    squad identifer for games played with teams.



35
36
37
# File 'lib/battlesnake/snake.rb', line 35

def squad
  @squad
end

Instance Method Details

#directionString

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.

Returns:

  • (String)

    one of (up, down, left, right)



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