Class: AIGames::FourInARow::PlayingField

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(rows = 0, columns = 0) ⇒ PlayingField

Initialize the playing field with the given number of rows and columns.



40
41
42
# File 'lib/ai_games/four_in_a_row/playing_field.rb', line 40

def initialize(rows = 0, columns = 0)
  resize_field rows, columns
end

Instance Attribute Details

#columnsObject

The number of columns of the playing field



34
35
36
# File 'lib/ai_games/four_in_a_row/playing_field.rb', line 34

def columns
  @columns
end

#rowsObject

The number of rows of the playing field



37
38
39
# File 'lib/ai_games/four_in_a_row/playing_field.rb', line 37

def rows
  @rows
end

Instance Method Details

#get_cell(row, column) ⇒ Object

Returns the owner of a cell. If the cell has no owner, nil is returned.



45
46
47
# File 'lib/ai_games/four_in_a_row/playing_field.rb', line 45

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.



51
52
53
54
55
56
57
# File 'lib/ai_games/four_in_a_row/playing_field.rb', line 51

def set_cell(row, column, owner)
  current_owner = @fields[row][column]
  return if current_owner == owner

  @fields[row][column] = owner
  notify_observers(row, column, owner)
end