Class: TicTacToe::Line

Inherits:
Object
  • Object
show all
Defined in:
lib/tic_tac_toe_mchliakh/board/line.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(board, squares) ⇒ Line

Returns a new instance of Line.



5
6
7
8
9
# File 'lib/tic_tac_toe_mchliakh/board/line.rb', line 5

def initialize(board, squares)
  @board = board
  @squares = squares
  listen
end

Instance Attribute Details

#squaresObject (readonly)

Returns the value of attribute squares.



3
4
5
# File 'lib/tic_tac_toe_mchliakh/board/line.rb', line 3

def squares
  @squares
end

Instance Method Details

#&(other) ⇒ Object



11
12
13
# File 'lib/tic_tac_toe_mchliakh/board/line.rb', line 11

def &(other)
  squares & other.squares
end

#can_lose?(player) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
# File 'lib/tic_tac_toe_mchliakh/board/line.rb', line 20

def can_lose?(player)
  squares.taken_by_opponent(player).count == 2 &&
    squares.empty.one?
end

#can_win?(player) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
# File 'lib/tic_tac_toe_mchliakh/board/line.rb', line 15

def can_win?(player)
  squares.taken_by(player).count == 2 &&
    squares.empty.one?
end

#could_lose?(player) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
# File 'lib/tic_tac_toe_mchliakh/board/line.rb', line 30

def could_lose?(player)
  squares.taken_by_opponent(player).one? &&
    squares.empty.count == 2
end

#could_win?(player) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/tic_tac_toe_mchliakh/board/line.rb', line 25

def could_win?(player)
  squares.taken_by(player).one? &&
    squares.empty.count == 2
end

#square_takenObject



35
36
37
38
39
# File 'lib/tic_tac_toe_mchliakh/board/line.rb', line 35

def square_taken
  if three_in_a_row?
    @board.three_in_a_row(squares.first.player)
  end
end