Class: BattleBoats::Coordinate

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row: nil, column: nil) ⇒ Coordinate

Returns a new instance of Coordinate.


13
14
15
16
# File 'lib/battle_boats/coordinate.rb', line 13

def initialize(row: nil, column: nil)
  @row = row
  @column = column
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column


11
12
13
# File 'lib/battle_boats/coordinate.rb', line 11

def column
  @column
end

#rowObject (readonly)

Returns the value of attribute row


11
12
13
# File 'lib/battle_boats/coordinate.rb', line 11

def row
  @row
end

Class Method Details

.random(row:, column:) ⇒ Object


4
5
6
7
8
# File 'lib/battle_boats/coordinate.rb', line 4

def random(row:, column:)
  row = rand(row)
  column = rand(column)
  new(row: row, column: column)
end

Instance Method Details

#right(offset: 1) ⇒ Object


22
23
24
# File 'lib/battle_boats/coordinate.rb', line 22

def right(offset: 1)
  self.class.new(row: row, column: column + offset)
end

#up(offset: 1) ⇒ Object


18
19
20
# File 'lib/battle_boats/coordinate.rb', line 18

def up(offset: 1)
  self.class.new(row: row - offset, column: column)
end