Class: MineField::Map

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMap



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

def initialize
  print 'Matrix Boyunuz: '

  @size = gets.to_i - 1
  @points = []
  @print_size = @size.to_s.length + 1

  create_points
end

Instance Attribute Details

#pointsObject (readonly)

Returns the value of attribute points.



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

def points
  @points
end

#sizeObject (readonly)

Returns the value of attribute size.



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

def size
  @size
end

Instance Method Details

#create_pointsObject



15
16
17
18
# File 'lib/mine_field/map.rb', line 15

def create_points
  mines_points
  other_points
end

#drawObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mine_field/map.rb', line 20

def draw
  puts `clear`
  header = '| ' + ' ' * (@print_size + 3) + (0..@size).to_a.map {|i| i.to_s.ljust(@print_size)}.join + " |\n"
  seperator = '-' * (header.length - 1) + "\n"

  (0..@size).each do |y|
    if y.zero?
      print seperator
      print header
      print seperator
    end

    print "| #{y.to_s.rjust(@print_size)} | "

    (0..@size).each do |x|
      point = get_point(x, y)

      print "#{point.to_s.ljust(@print_size)}"
      print ' |' if x == @size
    end

    puts ''
  end

  puts seperator
end

#get_point(x, y) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/mine_field/map.rb', line 47

def get_point(x, y)
  @points.each do |point|
    return point if point.coordinate_x == x && point.coordinate_y == y
  end

  nil
end

#visible!Object



55
56
57
# File 'lib/mine_field/map.rb', line 55

def visible!
  @points.select {|p| p.opened == false}.each {|p| p.opened = true}
end