Class: C4::Model::Board

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(num_rows, num_columns) ⇒ Board

Returns a new instance of Board.



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

def initialize(num_rows, num_columns)
  @num_rows = num_rows
  @num_columns = num_columns
  @columns = Array.new(num_columns).map { |_| Column.new(num_rows) }
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



9
10
11
# File 'lib/c4/model/board.rb', line 9

def columns
  @columns
end

#num_columnsObject (readonly)

Returns the value of attribute num_columns.



9
10
11
# File 'lib/c4/model/board.rb', line 9

def num_columns
  @num_columns
end

#num_rowsObject (readonly)

Returns the value of attribute num_rows.



9
10
11
# File 'lib/c4/model/board.rb', line 9

def num_rows
  @num_rows
end

Instance Method Details

#diagonalsObject



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

def diagonals
  matrix = Matrix.new(columns.map(&:to_a), num_rows, num_columns)
  matrix.diagonals
end

#full?Boolean

Returns:

  • (Boolean)


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

def full?
  columns.all?(&:full?)
end

#put_stone!(column, mark) ⇒ Object

Raises:



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

def put_stone!(column, mark)
  raise InvalidColumnError, 'Column does not exist!' if column.negative? || column >= columns.size

  columns[column].put!(mark)
end

#to_a(mode = :column_wise) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/c4/model/board.rb', line 29

def to_a(mode = :column_wise)
  column_wise = Matrix.new(columns.map(&:to_a), num_rows, num_columns)
  return column_wise.transpose.matrix if mode == :row_wise # Changes the shape
  return column_wise.transpose.reverse.matrix if mode == :print

  column_wise.matrix
end