Class: Board

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(num_of_rows) ⇒ Board

Returns a new instance of Board.



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

def initialize(num_of_rows)
  @num_of_rows = num_of_rows
  @all_cells = create_board_hash
  @winning_lines = get_winning_lines
end

Instance Attribute Details

#all_cellsObject

Returns the value of attribute all_cells.



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

def all_cells
  @all_cells
end

#num_of_rowsObject

Returns the value of attribute num_of_rows.



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

def num_of_rows
  @num_of_rows
end

#winning_linesObject

Returns the value of attribute winning_lines.



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

def winning_lines
  @winning_lines
end

Instance Method Details

#add_marker(marker, cell) ⇒ Object



48
49
50
# File 'lib/board.rb', line 48

def add_marker(marker, cell)
  all_cells[cell] = marker
end

#all_rowsObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/board.rb', line 35

def all_rows
  rows = []
  cellIDs = all_cells.keys
  beg = 0
  ending  = num_of_rows - 1
  until rows.length == num_of_rows
    rows << cellIDs[beg..ending]
    beg += num_of_rows
    ending += num_of_rows
  end
  rows
end

#available_cell?(cell) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/board.rb', line 64

def available_cell?(cell)
  valid_cell?(cell) && all_cells[cell].nil?
end

#create_board_hashObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/board.rb', line 11

def create_board_hash
  new_board = Hash.new
  alpha = 'A'
  numeric = 1
  num_of_rows.times do
    num_of_rows.times do
      cellID = numeric.to_s + alpha
      numeric += 1
      new_board[cellID] = nil
    end
    alpha = alpha.next
    numeric = 1
  end
  new_board
end

#empty?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/board.rb', line 84

def empty?
  open_cells.length == (num_of_rows * num_of_rows)
end

#game_over?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/board.rb', line 60

def game_over?
  !moves_remaining? || winner?(MARKER_X)|| winner?(MARKER_O)
end

#get_winning_linesObject



27
28
29
30
31
32
33
# File 'lib/board.rb', line 27

def get_winning_lines
  lines = []
  all_rows.each { |row| lines << row }
  all_cols.each { |col| lines << col }
  diagonals.each { |diagonal| lines << diagonal }
  lines
end

#moves_remaining?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/board.rb', line 76

def moves_remaining?
  all_cells.has_value?(nil)
end

#open_cellsObject



80
81
82
# File 'lib/board.rb', line 80

def open_cells
  all_cells.select { |k,v| v.nil? }
end

#random_cellObject



88
89
90
91
92
# File 'lib/board.rb', line 88

def random_cell
  cells = open_cells.keys
  cells_count = cells.length - 1
  cells[rand(cells_count)]
end

#remove_marker(cell) ⇒ Object



72
73
74
# File 'lib/board.rb', line 72

def remove_marker(cell)
  all_cells[cell] = nil
end

#valid_cell?(cell) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/board.rb', line 68

def valid_cell?(cell)
  all_cells.has_key?(cell)
end

#winner?(marker) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
# File 'lib/board.rb', line 52

def winner?(marker)
  board_markers = all_cells.select { |k,v| v == marker }.keys
  winning_lines.each do |line|
    return true if (line & board_markers).length == num_of_rows
  end
  false
end