Class: Board

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dimension = 3) ⇒ Board

Returns a new instance of Board.



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

def initialize(dimension = 3)
  @dimension = dimension
  @game_history, @board = [], Array.new(@dimension) {Array.new(@dimension) {0}}
  @row_scores, @column_scores = Array.new(@dimension) {0}, Array.new(@dimension) {0}
  @left_diagonal_score, @right_diagonal_score = 0, 0
  @player_one, @player_two = 1, -1
end

Instance Attribute Details

#column_scoresObject (readonly)

Returns the value of attribute column_scores.



2
3
4
# File 'lib/board.rb', line 2

def column_scores
  @column_scores
end

#dimensionObject (readonly)

Returns the value of attribute dimension.



2
3
4
# File 'lib/board.rb', line 2

def dimension
  @dimension
end

#game_historyObject (readonly)

Returns the value of attribute game_history.



2
3
4
# File 'lib/board.rb', line 2

def game_history
  @game_history
end

#left_diagonal_scoreObject (readonly)

Returns the value of attribute left_diagonal_score.



2
3
4
# File 'lib/board.rb', line 2

def left_diagonal_score
  @left_diagonal_score
end

#player_oneObject (readonly)

Returns the value of attribute player_one.



2
3
4
# File 'lib/board.rb', line 2

def player_one
  @player_one
end

#player_twoObject (readonly)

Returns the value of attribute player_two.



2
3
4
# File 'lib/board.rb', line 2

def player_two
  @player_two
end

#player_valueObject (readonly)

Returns the value of attribute player_value.



2
3
4
# File 'lib/board.rb', line 2

def player_value
  @player_value
end

#right_diagonal_scoreObject (readonly)

Returns the value of attribute right_diagonal_score.



2
3
4
# File 'lib/board.rb', line 2

def right_diagonal_score
  @right_diagonal_score
end

#row_scoresObject (readonly)

Returns the value of attribute row_scores.



2
3
4
# File 'lib/board.rb', line 2

def row_scores
  @row_scores
end

Instance Method Details

#corner_occupied?Boolean

Returns:

  • (Boolean)


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

def corner_occupied?
  corner_cells.each { |corner| return true if @game_history.include?(corner) }
  return false
end

#full?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/board.rb', line 37

def full?
  @game_history.size == @dimension*@dimension
end

#is_empty_at?(row, column) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/board.rb', line 33

def is_empty_at?(row, column)
  @board[row-1][column-1].zero?
end

#move(row, column, value = nil) ⇒ Object



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

def move(row, column, value = nil)
  @board[row-1][column-1] = value || player_value
  update_sums(row-1, column-1, value || player_value)
  @game_history << Move.new(row, column, value || player_value)
end

#number_of_moves_madeObject



25
26
27
# File 'lib/board.rb', line 25

def number_of_moves_made
  @game_history.size
end

#random_corner_cellObject



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

def random_corner_cell
  corner_cells[rand(corner_cells.size)]
end

#undo_moveObject



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

def undo_move
  move = @game_history.pop
  update_sums(move.row-1, move.column-1, -move.player_value)
  @board[move.row-1][move.column-1] = 0
end

#value_at(row, column) ⇒ Object



29
30
31
# File 'lib/board.rb', line 29

def value_at(row, column)
  @board[row-1][column-1]
end

#winnerObject



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

def winner
  -player_value
end

#winning_valueObject



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

def winning_value
  @dimension
end