Class: RubyTictactoe::Board

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

Constant Summary

Constants included from TictactoeConstants

TictactoeConstants::AI_PLAYER, TictactoeConstants::COMPUTER_PLAYER, TictactoeConstants::EASY_LEVEL, TictactoeConstants::HARD_LEVEL, TictactoeConstants::HUMAN_PLAYER, TictactoeConstants::MARKER_O, TictactoeConstants::MARKER_X

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(num_of_rows) ⇒ Board

Returns a new instance of Board.



8
9
10
11
12
# File 'lib/board.rb', line 8

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.



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

def all_cells
  @all_cells
end

#num_of_rowsObject

Returns the value of attribute num_of_rows.



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

def num_of_rows
  @num_of_rows
end

#winning_linesObject

Returns the value of attribute winning_lines.



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

def winning_lines
  @winning_lines
end

Instance Method Details

#add_test_marker(marker, cell) ⇒ Object



51
52
53
# File 'lib/board.rb', line 51

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

#all_rowsObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/board.rb', line 38

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)


55
56
57
# File 'lib/board.rb', line 55

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

#create_board_hashObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/board.rb', line 14

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)


87
88
89
# File 'lib/board.rb', line 87

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

#game_over?Boolean

Returns:

  • (Boolean)


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

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

#get_winning_linesObject



30
31
32
33
34
35
36
# File 'lib/board.rb', line 30

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)


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

def moves_remaining?
  all_cells.has_value?(nil)
end

#open_cellsObject



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

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

#random_cellObject



91
92
93
94
95
# File 'lib/board.rb', line 91

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

#remove_marker(cell) ⇒ Object



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

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

#valid_cell?(cell) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#winner?(marker) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
77
# File 'lib/board.rb', line 71

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