Class: Board
- Inherits:
-
Object
- Object
- Board
- Defined in:
- lib/board.rb
Instance Attribute Summary collapse
-
#column_scores ⇒ Object
readonly
Returns the value of attribute column_scores.
-
#dimension ⇒ Object
readonly
Returns the value of attribute dimension.
-
#game_history ⇒ Object
readonly
Returns the value of attribute game_history.
-
#left_diagonal_score ⇒ Object
readonly
Returns the value of attribute left_diagonal_score.
-
#player_one ⇒ Object
readonly
Returns the value of attribute player_one.
-
#player_two ⇒ Object
readonly
Returns the value of attribute player_two.
-
#player_value ⇒ Object
readonly
Returns the value of attribute player_value.
-
#right_diagonal_score ⇒ Object
readonly
Returns the value of attribute right_diagonal_score.
-
#row_scores ⇒ Object
readonly
Returns the value of attribute row_scores.
Instance Method Summary collapse
- #corner_occupied? ⇒ Boolean
- #full? ⇒ Boolean
-
#initialize(dimension = 3) ⇒ Board
constructor
A new instance of Board.
- #is_empty_at?(row, column) ⇒ Boolean
- #move(row, column, value = nil) ⇒ Object
- #number_of_moves_made ⇒ Object
- #random_corner_cell ⇒ Object
- #undo_move ⇒ Object
- #value_at(row, column) ⇒ Object
- #winner ⇒ Object
- #winning_value ⇒ Object
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_scores ⇒ Object (readonly)
Returns the value of attribute column_scores.
2 3 4 |
# File 'lib/board.rb', line 2 def column_scores @column_scores end |
#dimension ⇒ Object (readonly)
Returns the value of attribute dimension.
2 3 4 |
# File 'lib/board.rb', line 2 def dimension @dimension end |
#game_history ⇒ Object (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_score ⇒ Object (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_one ⇒ Object (readonly)
Returns the value of attribute player_one.
2 3 4 |
# File 'lib/board.rb', line 2 def player_one @player_one end |
#player_two ⇒ Object (readonly)
Returns the value of attribute player_two.
2 3 4 |
# File 'lib/board.rb', line 2 def player_two @player_two end |
#player_value ⇒ Object (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_score ⇒ Object (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_scores ⇒ Object (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
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
37 38 39 |
# File 'lib/board.rb', line 37 def full? @game_history.size == @dimension*@dimension end |
#is_empty_at?(row, column) ⇒ 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_made ⇒ Object
25 26 27 |
# File 'lib/board.rb', line 25 def number_of_moves_made @game_history.size end |
#random_corner_cell ⇒ Object
60 61 62 |
# File 'lib/board.rb', line 60 def random_corner_cell corner_cells[rand(corner_cells.size)] end |
#undo_move ⇒ Object
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 |
#winner ⇒ Object
51 52 53 |
# File 'lib/board.rb', line 51 def winner -player_value end |
#winning_value ⇒ Object
41 42 43 |
# File 'lib/board.rb', line 41 def winning_value @dimension end |