Class: XO::GameContext

Inherits:
Struct
  • Object
show all
Defined in:
lib/xo/engine/game_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#gridObject

Returns the value of attribute grid

Returns:

  • (Object)

    the current value of grid



5
6
7
# File 'lib/xo/engine/game_context.rb', line 5

def grid
  @grid
end

#turnObject

Returns the value of attribute turn

Returns:

  • (Object)

    the current value of turn



5
6
7
# File 'lib/xo/engine/game_context.rb', line 5

def turn
  @turn
end

Instance Method Details

#check_turn(turn) ⇒ Object

Raises:

  • (ArgumentError)


24
25
26
# File 'lib/xo/engine/game_context.rb', line 24

def check_turn(turn)
  raise ArgumentError, "invalid turn symbol, #{turn}" unless Grid.is_token?(turn)
end

#next_turnObject



20
21
22
# File 'lib/xo/engine/game_context.rb', line 20

def next_turn
  Grid.other_token(turn)
end

#resetObject



7
8
9
# File 'lib/xo/engine/game_context.rb', line 7

def reset
  set_turn_and_clear_grid(:nobody)
end

#set_turn_and_clear_grid(turn) ⇒ Object



11
12
13
14
# File 'lib/xo/engine/game_context.rb', line 11

def set_turn_and_clear_grid(turn)
  self.turn = turn
  grid.clear
end

#switch_turnsObject



16
17
18
# File 'lib/xo/engine/game_context.rb', line 16

def switch_turns
  self.turn = next_turn
end