Class: XO::Engine
Instance Attribute Summary collapse
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#turn ⇒ Object
readonly
Returns the value of attribute turn.
Instance Method Summary collapse
- #continue_playing(player) ⇒ Object
- #grid ⇒ Object
-
#initialize ⇒ Engine
constructor
A new instance of Engine.
- #next_turn ⇒ Object
- #play(r, c) ⇒ Object
- #start(player) ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize ⇒ Engine
Returns a new instance of Engine.
14 15 16 17 18 |
# File 'lib/xo/engine.rb', line 14 def initialize @grid = Grid.new @turn = :nobody @state = :idle end |
Instance Attribute Details
#state ⇒ Object
Returns the value of attribute state.
12 13 14 |
# File 'lib/xo/engine.rb', line 12 def state @state end |
#turn ⇒ Object
Returns the value of attribute turn.
12 13 14 |
# File 'lib/xo/engine.rb', line 12 def turn @turn end |
Instance Method Details
#continue_playing(player) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/xo/engine.rb', line 63 def (player) raise ArgumentError, "unknown player #{player}" unless XO.is_player?(player) case state when :game_over (player) else raise NotImplementedError end self end |
#grid ⇒ Object
20 21 22 |
# File 'lib/xo/engine.rb', line 20 def grid @grid.dup end |
#next_turn ⇒ Object
24 25 26 |
# File 'lib/xo/engine.rb', line 24 def next_turn XO.other_player(turn) end |
#play(r, c) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/xo/engine.rb', line 52 def play(r, c) case state when :playing handle_play(r.to_i, c.to_i) else raise NotImplementedError end self end |
#start(player) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/xo/engine.rb', line 28 def start(player) raise ArgumentError, "unknown player #{player}" unless XO.is_player?(player) case state when :idle handle_start(player) else raise NotImplementedError end self end |
#stop ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/xo/engine.rb', line 41 def stop case state when :playing, :game_over handle_stop else raise NotImplementedError end self end |