Class: MineField::Point

Inherits:
Object
  • Object
show all
Defined in:
lib/mine_field/point.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(map, x, y, content) ⇒ Point



6
7
8
9
10
11
12
# File 'lib/mine_field/point.rb', line 6

def initialize(map, x, y, content)
  @coordinate_x = x
  @coordinate_y = y
  @content = content
  @opened = false
  @map = map
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



3
4
5
# File 'lib/mine_field/point.rb', line 3

def content
  @content
end

#coordinate_xObject (readonly)

Returns the value of attribute coordinate_x.



3
4
5
# File 'lib/mine_field/point.rb', line 3

def coordinate_x
  @coordinate_x
end

#coordinate_yObject (readonly)

Returns the value of attribute coordinate_y.



3
4
5
# File 'lib/mine_field/point.rb', line 3

def coordinate_y
  @coordinate_y
end

#openedObject

Returns the value of attribute opened.



4
5
6
# File 'lib/mine_field/point.rb', line 4

def opened
  @opened
end

Instance Method Details

#neighborsObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mine_field/point.rb', line 14

def neighbors
  items = []

  ((@coordinate_x - 1)..(@coordinate_x + 1)).each do |x|
    ((@coordinate_y - 1)..(@coordinate_y + 1)).each do |y|
      point = @map.get_point(x, y)
      items.push(point) unless point.nil?
    end
  end

  items
end

#to_sObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mine_field/point.rb', line 27

def to_s
  if @opened
    if @content == MINE_CHARACTER
      MINE_CHARACTER
    else
      neighbors.select {|p| p.content == MINE_CHARACTER}.count.to_s
    end
  else
    NOT_OPENED_CHARACTER
  end
end