Class: TTT::ThreeByThree

Inherits:
Board
  • Object
show all
Defined in:
lib/ttt/three_by_three.rb

Instance Attribute Summary collapse

Attributes inherited from Board

#board

Instance Method Summary collapse

Methods inherited from Board

#[], #[]=, #draw_game?, #empty?, #finished?, #free?, #full?, #update

Constructor Details

#initializeThreeByThree

Returns a new instance of ThreeByThree.



6
7
8
9
10
# File 'lib/ttt/three_by_three.rb', line 6

def initialize
  self.board   = Array.new(9, " ")
  self.win_arr = [[0,1,2], [0,4,8], [0,3,6], [1,4,7], [2,5,8], [2,4,6], [3,4,5], [6,7,8]]
  self.win_block = [[0,1,3,4], [1,2,4,5], [3,4,6,7], [4,5,7,8]]
end

Instance Attribute Details

#win_arrObject

Returns the value of attribute win_arr.



5
6
7
# File 'lib/ttt/three_by_three.rb', line 5

def win_arr
  @win_arr
end

#win_blockObject

Returns the value of attribute win_block.



5
6
7
# File 'lib/ttt/three_by_three.rb', line 5

def win_block
  @win_block
end

Instance Method Details

#block_winner?Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
# File 'lib/ttt/three_by_three.rb', line 21

def block_winner?
  win_block.each do |win_combo|
    return true if board[win_combo[0]] == board[win_combo[1]] &&
                   board[win_combo[1]] == board[win_combo[2]] &&
                   board[win_combo[2]] == board[win_combo[3]] &&
                   board[win_combo[0]] != " "
  end
  false
end

#board_typeObject



31
32
33
# File 'lib/ttt/three_by_three.rb', line 31

def board_type
  "three_by_three"
end

#winner?Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
# File 'lib/ttt/three_by_three.rb', line 12

def winner?
  win_arr.each do |win_combo|
    return true if board[win_combo[0]] == board[win_combo[1]] &&
                   board[win_combo[1]] == board[win_combo[2]] &&
                   board[win_combo[0]] != " "
  end
  block_winner?
end