Class: Rules

Inherits:
Object
  • Object
show all
Defined in:
lib/ttt_malisa/rules.rb

Constant Summary collapse

WINNING_COMBOS =
[
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6]
]

Instance Method Summary collapse

Constructor Details

#initialize(board) ⇒ Rules

Returns a new instance of Rules.



5
6
7
# File 'lib/ttt_malisa/rules.rb', line 5

def initialize(board)
  @board = board
end

Instance Method Details

#draw?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/ttt_malisa/rules.rb', line 32

def draw?
  !winning_combination? && @board.full?
end

#game_over?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/ttt_malisa/rules.rb', line 20

def game_over?
  winning_combination? || draw?
end

#winning_combination?Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
# File 'lib/ttt_malisa/rules.rb', line 24

def winning_combination?
  WINNING_COMBOS.any? do |x, y, z|
    @board.position_taken?(x) &&
    @board.squares[x] == @board.squares[y] &&
    @board.squares[x] == @board.squares[z]
  end
end