Class: Tictactoe::Game
- Inherits:
-
Object
- Object
- Tictactoe::Game
- Defined in:
- lib/tictactoe/tictactoe.rb
Instance Attribute Summary collapse
-
#o_player ⇒ Object
readonly
Returns the value of attribute o_player.
-
#x_player ⇒ Object
readonly
Returns the value of attribute x_player.
Instance Method Summary collapse
-
#initialize(player1, player2, random = true) ⇒ Game
constructor
A new instance of Game.
- #play ⇒ Object
Constructor Details
#initialize(player1, player2, random = true) ⇒ Game
Returns a new instance of Game.
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/tictactoe/tictactoe.rb', line 113 def initialize( player1, player2, random = true ) if random and rand(2) == 1 @x_player = player2.new("X") @o_player = player1.new("O") else @x_player = player1.new("X") @o_player = player2.new("O") end @board = Board.new([" "] * 9) end |
Instance Attribute Details
#o_player ⇒ Object (readonly)
Returns the value of attribute o_player.
125 126 127 |
# File 'lib/tictactoe/tictactoe.rb', line 125 def o_player @o_player end |
#x_player ⇒ Object (readonly)
Returns the value of attribute x_player.
125 126 127 |
# File 'lib/tictactoe/tictactoe.rb', line 125 def x_player @x_player end |
Instance Method Details
#play ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/tictactoe/tictactoe.rb', line 127 def play until @board.won? @board[@x_player.move(@board)] = @x_player.mark break if @board.won? @board[@o_player.move(@board)] = @o_player.mark end @o_player.finish @board @x_player.finish @board end |