Class: TicTacToe::Board

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

Constant Summary collapse

BoardError =
Class.new(StandardError)
BLANK_MARK =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parameters) ⇒ Board

Returns a new instance of Board.



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

def initialize(parameters)
  fail BoardError, "Given size is too small, must be 3 or greater" if parameters[:size] < 3

  @size = parameters[:size]
  config = parameters[:config] || blank_board_configuration
  @cells = map_configuration_to_cells(config)
end

Instance Attribute Details

#last_move_madeObject (readonly)

Returns the value of attribute last_move_made.



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

def last_move_made
  @last_move_made
end

#sizeObject (readonly)

Returns the value of attribute size.



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

def size
  @size
end

Class Method Details

.blank_markObject



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

def self.blank_mark
  BLANK_MARK
end

Instance Method Details

#all_blank?Boolean

Returns:

  • (Boolean)


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

def all_blank?
  all_coordinates.all? { |coordinates| blank?(coordinates) }
end

#all_coordinatesObject



40
41
42
# File 'lib/board.rb', line 40

def all_coordinates
  (0...@size).to_a.repeated_permutation(2).to_a
end

#all_marked?Boolean

Returns:

  • (Boolean)


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

def all_marked?
  all_coordinates.all? { |coordinates| marked?(coordinates) }
end

#blank?(coordinates) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/board.rb', line 57

def blank?(coordinates)
  self.read_cell(*coordinates) == BLANK_MARK
end

#blank_cell_coordinatesObject



44
45
46
# File 'lib/board.rb', line 44

def blank_cell_coordinates
  all_coordinates.reject { |coordinates| marked?(coordinates) }
end

#deep_copyObject



53
54
55
# File 'lib/board.rb', line 53

def deep_copy
  Board.new(size: @size, config: map_cells_to_configuration(@cells))
end

#has_winning_line?Boolean

Returns:

  • (Boolean)


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

def has_winning_line?
  lines.each do |line|
    return true if line.first != BLANK_MARK && line.all? { |cell| cell == line.first }
  end
  false
end

#last_mark_madeObject



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

def last_mark_made
  return if @last_move_made.nil?
  self.read_cell(*last_move_made)
end

#linesObject



34
35
36
37
38
# File 'lib/board.rb', line 34

def lines
  (0...@size).each_with_object([left_diag, right_diag]) do |index, lines|
    lines << row_at(index) << col_at(index)
  end
end

#mark_cell(mark, row, col) ⇒ Object



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

def mark_cell(mark, row, col)
  fail BoardError, "Cell coordinates are out of bounds" if out_of_bounds?([row, col])
  fail BoardError, "Cannot alter a marked cell" if marked?([row, col])

  @last_move_made = [row, col]
  @cells[row][col] = mark
end

#marked?(coordinates) ⇒ Boolean

Returns:

  • (Boolean)


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

def marked?(coordinates)
  !blank?(coordinates)
end

#out_of_bounds?(coordinates) ⇒ Boolean

Returns:

  • (Boolean)


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

def out_of_bounds?(coordinates)
  coordinates.any? { |i| !i.between?(0, @size - 1) }
end

#read_cell(row, col) ⇒ Object



20
21
22
23
24
# File 'lib/board.rb', line 20

def read_cell(row, col)
  fail BoardError, "Cell coordinates are out of bounds" if out_of_bounds?([row, col])

  @cells[row][col]
end