Class: Minesweeper::Board

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/minesweeper/board.rb

Constant Summary collapse

ENTER =
10
STEP =
3
LEFT_RIGHT_PADDING =
2
SPACE =
' '

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(board_params) ⇒ Board

Returns a new instance of Board.



17
18
19
20
21
# File 'lib/minesweeper/board.rb', line 17

def initialize(board_params)
  @board_params = board_params
  @window = Ui.instance.window
  @cells  = []
end

Instance Attribute Details

#board_paramsObject (readonly)

Returns the value of attribute board_params.



15
16
17
# File 'lib/minesweeper/board.rb', line 15

def board_params
  @board_params
end

#cellsObject (readonly)

Returns the value of attribute cells.



15
16
17
# File 'lib/minesweeper/board.rb', line 15

def cells
  @cells
end

#windowObject (readonly)

Returns the value of attribute window.



15
16
17
# File 'lib/minesweeper/board.rb', line 15

def window
  @window
end

Instance Method Details

#bombsObject



211
212
213
# File 'lib/minesweeper/board.rb', line 211

def bombs
  cells.flatten(1).select(&:bomb?)
end

#found_bombsObject



215
216
217
# File 'lib/minesweeper/board.rb', line 215

def found_bombs
  bombs.select(&:marked_as_bomb?)
end

#number_of_cellsObject



219
220
221
# File 'lib/minesweeper/board.rb', line 219

def number_of_cells
  cells.flatten.count
end

#play(stdy = nil, stdx = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/minesweeper/board.rb', line 23

def play(stdy = nil, stdx = nil)
  window.keypad = true
  draw_board
  window.setpos(stdy, stdx) if stdx && stdy
  noecho

  loop do
    ch = window.getch
    case ch
    when KEY_UP
      move_up
    when KEY_DOWN
      move_down
    when KEY_LEFT
      move_left
    when KEY_RIGHT
      move_right
    when ENTER
      open_cell
    when SPACE
      toggle_bomb_flag
    end
    play(cury, curx)
  end
rescue GameWon, GameOver => e
  end_game(e)
  exit
end