Class: Battlesnake::Turn
Overview
Represents a single iteration (turn) of a Battlesnake game.
Instance Attribute Summary collapse
-
#as_json ⇒ Hash
readonly
Board as a data structure usable by other objects.
-
#board ⇒ Board
readonly
Board attributes as a Board object.
-
#game ⇒ Hash
readonly
Game attributes as a data structure.
-
#you ⇒ Snake
readonly
Your own snake attributes as a Snake object.
Instance Method Summary collapse
-
#initialize(json_or_hash) ⇒ Turn
constructor
Returns a new instance of Turn.
-
#others ⇒ Array<Snake>
Returns all snakes, minus your snake.
Methods inherited from Base
Constructor Details
#initialize(json_or_hash) ⇒ Turn
Returns a new instance of Turn.
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_json ⇒ Hash (readonly)
Returns 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 |
#board ⇒ Board (readonly)
Returns board attributes as a Board object.
12 13 14 |
# File 'lib/battlesnake/turn.rb', line 12 def board @board end |
#game ⇒ Hash (readonly)
Returns game attributes as a data structure.
9 10 11 |
# File 'lib/battlesnake/turn.rb', line 9 def game @game end |
#you ⇒ Snake (readonly)
Returns 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
#others ⇒ Array<Snake>
Returns all snakes, minus your snake.
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 |