Class: Battlesnake::Turn

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

Overview

Represents a single iteration (turn) of a Battlesnake game.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#==

Constructor Details

#initialize(json_or_hash) ⇒ Turn

Returns a new instance of Turn.

Parameters:

  • json_or_hash (String, Hash)

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



24
25
26
27
28
29
30
31
# File 'lib/battlesnake/turn.rb', line 24

def initialize(json_or_hash)
  data = json_or_hash.is_a?(String) ? JSON.parse(json_or_hash) : json_or_hash

  @as_json = data
  @game = data['game']
  @board = Board.new(data['board'])
  @you = Snake.new(data['you'])
end

Instance Attribute Details

#as_jsonHash (readonly)

Returns board as a data structure usable by other objects.

Returns:

  • (Hash)

    board as a data structure usable by other objects.



6
7
8
# File 'lib/battlesnake/turn.rb', line 6

def as_json
  @as_json
end

#boardBoard (readonly)

Returns board attributes as a Board object.

Returns:

  • (Board)

    board attributes as a Board object.



12
13
14
# File 'lib/battlesnake/turn.rb', line 12

def board
  @board
end

#gameHash (readonly)

Returns game attributes as a data structure.

Returns:

  • (Hash)

    game attributes as a data structure.



9
10
11
# File 'lib/battlesnake/turn.rb', line 9

def game
  @game
end

#youSnake (readonly)

Returns your own snake attributes as a Snake object.

Returns:

  • (Snake)

    your own snake attributes as a Snake object.



15
16
17
# File 'lib/battlesnake/turn.rb', line 15

def you
  @you
end

Instance Method Details

#othersArray<Snake>

Returns all snakes, minus your snake.

Returns:



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

def others
  return @others if defined?(@others)
  @others = board.snakes.reject{ |snake| snake.id == you.id }
end