class TicTacToeRu::Board

Constants

WIN_POS

Attributes

board[R]
symbol[RW]

Public Class Methods

new() click to toggle source
# File lib/tic_tac_toe_ru/board.rb, line 8
def initialize
  @board = (1..9).each_slice(3).to_a
end

Public Instance Methods

check_free_cell(value) click to toggle source
# File lib/tic_tac_toe_ru/board.rb, line 29
def check_free_cell(value)
  values = grid.select { |k| k == value }.values.flatten
  @board[values.first][values.last].class == Fixnum
end
grid_draw() click to toggle source
# File lib/tic_tac_toe_ru/board.rb, line 17
def grid_draw
  @board.each { |el| puts "## #{el} ##" }
end
next_move(value) click to toggle source
# File lib/tic_tac_toe_ru/board.rb, line 12
def next_move(value)
  values = grid.select { |k| k == value }.values.flatten
  @board[values.first][values.last] = symbol
end
no_free_ceels?() click to toggle source
# File lib/tic_tac_toe_ru/board.rb, line 25
def no_free_ceels?
  true if @board.flatten.all? { |el| el.class == String }
end
win_positions?() click to toggle source
# File lib/tic_tac_toe_ru/board.rb, line 21
def win_positions?
  horizontal || vertical || diagonals
end

Private Instance Methods

diagonals() click to toggle source
# File lib/tic_tac_toe_ru/board.rb, line 44
def diagonals
  [ [board[0][0], board[1][1], board[2][2]],
    [board[0][2], board[1][1], board[2][0]] ].any? &WIN_POS
end
grid() click to toggle source
# File lib/tic_tac_toe_ru/board.rb, line 49
def grid
  hash = { 1 => [0, 0],
           2 => [0, 1],
           3 => [0, 2],
           4 => [1, 0],
           5 => [1, 1],
           6 => [1, 2],
           7 => [2, 0],
           8 => [2, 1],
           9 => [2, 2]
  }
end
horizontal() click to toggle source
# File lib/tic_tac_toe_ru/board.rb, line 36
def horizontal
  @board.any? &WIN_POS
end
vertical() click to toggle source
# File lib/tic_tac_toe_ru/board.rb, line 40
def vertical
  @board.transpose.any? &WIN_POS
end