Class: Tictactoe::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/game-tictactoe-esegredo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(player1, player2, random = true) ⇒ Game

Returns a new instance of Game.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/game-tictactoe-esegredo.rb', line 10

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_playerObject (readonly)

Returns the value of attribute o_player.



22
23
24
# File 'lib/game-tictactoe-esegredo.rb', line 22

def o_player
  @o_player
end

#x_playerObject (readonly)

Returns the value of attribute x_player.



22
23
24
# File 'lib/game-tictactoe-esegredo.rb', line 22

def x_player
  @x_player
end

Instance Method Details

#playObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/game-tictactoe-esegredo.rb', line 24

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

			puts @board

  if @board.won? == @x_player.mark
    print "Player (#{@x_player.mark}) has won!\n\n"
  elsif @board.won? == " "
    print "Tie game.\n\n"
  else
    print "Player (#{@o_player.mark}) has won!\n\n"
  end
end