Class: Board

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size = 10) ⇒ Board

Returns a new instance of Board.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/minesweeper.rb', line 7

def initialize(size = 10)
  raise "size out of range!" unless size.between?(8, 25)
  @size = size
  @grid = Array.new(size){ Array.new(size){ Space.new } }

  place_mines
  count_nearby_mines

  #must be after mine placement
  @flags_remaining = mine_count
end

Instance Attribute Details

#flags_remainingObject

Returns the value of attribute flags_remaining.



5
6
7
# File 'lib/minesweeper.rb', line 5

def flags_remaining
  @flags_remaining
end

#gridObject (readonly)

Returns the value of attribute grid.



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

def grid
  @grid
end

#sizeObject (readonly)

Returns the value of attribute size.



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

def size
  @size
end

Instance Method Details

#[](index) ⇒ Object

rather than just calling board.grid[col] this passes the grid directly through so you can call board[col]



54
55
56
# File 'lib/minesweeper.rb', line 54

def [](index)
  self.grid[index]
end

#endgame_renderObject

show everything if you lose



45
46
47
48
# File 'lib/minesweeper.rb', line 45

def endgame_render
  self.grid.flatten.each {|space| space.visible = true }
  render
end

#game_lost?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/minesweeper.rb', line 93

def game_lost?
  touched_a_mine? 
end

#mark_clear(x, y) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/minesweeper.rb', line 69

def mark_clear( x, y )
  return false if self[x][y].visible
  self[x][y].visible = true

  if self[x][y].zero_nearby?

    #mark all of them as well
    adjacent_spaces(x, y).each do |coords|
      adj_x, adj_y = coords[0], coords[1]
      mark_clear(adj_x, adj_y)
    end

  end

  true
end

#mine_countObject

this took lots of refactoring!!!



20
21
22
# File 'lib/minesweeper.rb', line 20

def mine_count
  grid.flatten.count { |space| space.mine }
end

#num_adjacent_mines(x, y) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/minesweeper.rb', line 97

def num_adjacent_mines(x, y)
  mine_count = 0

  adjacent_spaces(x, y).each do |coords|
    adj_x, adj_y = coords[0], coords[1]
    mine_count += 1 if self.grid[adj_x][adj_y].mine
  end

  mine_count
end

#place_flag(x, y) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/minesweeper.rb', line 60

def place_flag( x, y )
  return false if flags_remaining <= 0
  return false if self[x][y].visible

  self[x][y].flagged = true
  self.flags_remaining -= 1
  true
end

#renderObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/minesweeper.rb', line 24

def render
  (self.size-1).downto(0) do |y|
    print "#{y}|"
    (0).upto(self.size-1) do |x|
      render_cell(x, y)
      print " "
    end
    puts "|\n"
  end

  print "  "
  print "--" * size
  puts "\n"
  print " "
  0.upto(self.size-1) {|x| print " #{x}"}
  puts "\n"
  puts "Flags remaining: #{flags_remaining}\n"
  nil
end

#victory?Boolean

all non-mine spaces are visible

Returns:

  • (Boolean)


87
88
89
90
# File 'lib/minesweeper.rb', line 87

def victory?
  self.grid.flatten.select {|space| !space.mine }.
                    all? {|space| space.visible }
end