Class: AIGames::FourInARow::PlayingField
- Inherits:
-
Object
- Object
- AIGames::FourInARow::PlayingField
- Includes:
- Observable
- Defined in:
- lib/ai_games/four_in_a_row/playing_field.rb
Overview
This class represents the playing field of the game. It has a given number of columns and rows, and each field can either be empty or filled. If a field is filled, the name of the player whose chip fills the field is stored in it.
Instance Attribute Summary collapse
-
#columns ⇒ Object
The number of columns of the playing field.
-
#rows ⇒ Object
The number of rows of the playing field.
Instance Method Summary collapse
-
#get_cell(row, column) ⇒ Object
Returns the cell at the given position.
-
#initialize(rows = 0, columns = 0) ⇒ PlayingField
constructor
Initialize the playing field with the given number of rows and columns.
-
#set_cell(row, column, owner) ⇒ Object
Updates a cell.
Constructor Details
#initialize(rows = 0, columns = 0) ⇒ PlayingField
Initialize the playing field with the given number of rows and columns.
41 42 43 |
# File 'lib/ai_games/four_in_a_row/playing_field.rb', line 41 def initialize(rows = 0, columns = 0) resize_field rows, columns end |
Instance Attribute Details
#columns ⇒ Object
The number of columns of the playing field
35 36 37 |
# File 'lib/ai_games/four_in_a_row/playing_field.rb', line 35 def columns @columns end |
#rows ⇒ Object
The number of rows of the playing field
38 39 40 |
# File 'lib/ai_games/four_in_a_row/playing_field.rb', line 38 def rows @rows end |
Instance Method Details
#get_cell(row, column) ⇒ Object
Returns the cell at the given position.
46 47 48 |
# File 'lib/ai_games/four_in_a_row/playing_field.rb', line 46 def get_cell(row, column) @fields[row][column] end |
#set_cell(row, column, owner) ⇒ Object
Updates a cell. If the owner of the cell changes, all observers of the playing field get notified.
52 53 54 55 56 57 58 59 |
# File 'lib/ai_games/four_in_a_row/playing_field.rb', line 52 def set_cell(row, column, owner) cell = @fields[row][column] current_owner = cell.owner return if current_owner == owner cell.owner = owner notify_observers(cell, owner) end |