Class: Minesweeper::Playfield
- Inherits:
-
Object
- Object
- Minesweeper::Playfield
- Defined in:
- lib/minesweeper/playfield.rb
Instance Method Summary collapse
- #all_squares_revealed? ⇒ Boolean
- #display ⇒ Object
- #display_with_mines ⇒ Object
- #has_mine?(y, x) ⇒ Boolean
-
#initialize(options = {}) ⇒ Playfield
constructor
A new instance of Playfield.
- #mines_around(y, x) ⇒ Object
- #reveal_adjacent(y, x) ⇒ Object
- #reveal_square(y, x) ⇒ Object
- #within_boundaries?(y, x) ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ Playfield
Returns a new instance of Playfield.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/minesweeper/playfield.rb', line 7 def initialize( = {}) @display_set = [:display_set] @mine_number = [:mine_number] @rows = [:rows] @cols = [:cols] @revealed_squares = Set.new @mines = Set.new @playfield = Array.new(@rows) { Array.new(@cols) } until [:mine_number].zero? mine_position = [rand(@rows), rand(@cols)] unless @mines.include?(mine_position) @mines << mine_position [:mine_number] -= 1 end end end |
Instance Method Details
#all_squares_revealed? ⇒ Boolean
67 68 69 |
# File 'lib/minesweeper/playfield.rb', line 67 def all_squares_revealed? @playfield.flatten.select(&:nil?).size == @mine_number end |
#display ⇒ Object
26 27 28 29 30 |
# File 'lib/minesweeper/playfield.rb', line 26 def display @playfield.each do |row| puts row.map { |content| Square.new(@display_set).display(content) }.join end end |
#display_with_mines ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/minesweeper/playfield.rb', line 32 def display_with_mines @mines.each do |mine| y, x = mine @playfield[y][x] = true end display end |
#has_mine?(y, x) ⇒ Boolean
44 45 46 |
# File 'lib/minesweeper/playfield.rb', line 44 def has_mine?(y, x) @mines.include?([y, x]) end |
#mines_around(y, x) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/minesweeper/playfield.rb', line 48 def mines_around(y, x) count = 0 (-1).upto(1).each do |dx| (-1).upto(1).each do |dy| next if dx.zero? && dy.zero? next if x + dx < 0 next if y + dy < 0 next if x + dx >= @cols next if y + dy >= @rows if has_mine?(y + dy, x + dx) count += 1 end end end count end |
#reveal_adjacent(y, x) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/minesweeper/playfield.rb', line 71 def reveal_adjacent(y, x) (-1).upto(1).each do |dx| (-1).upto(1).each do |dy| next if dx.zero? && dy.zero? next if x + dx < 0 next if y + dy < 0 next if x + dx >= @cols next if y + dy >= @rows reveal_square(y + dy, x + dx) end end end |
#reveal_square(y, x) ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/minesweeper/playfield.rb', line 85 def reveal_square(y, x) number_mines_around = mines_around(y, x) @playfield[y][x] = number_mines_around if number_mines_around.zero? && !@revealed_squares.include?([y, x]) @revealed_squares << [y, x] reveal_adjacent(y, x) end end |
#within_boundaries?(y, x) ⇒ Boolean
40 41 42 |
# File 'lib/minesweeper/playfield.rb', line 40 def within_boundaries?(y, x) y >= 0 && y < @rows && x >= 0 && x < @cols end |