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.



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

def initialize(board_params)
  @board_params = board_params
  @ui = Minesweeper::Ui
  @cells = []
end

Instance Attribute Details

#board_paramsObject (readonly)

Returns the value of attribute board_params.



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

def board_params
  @board_params
end

#cellsObject (readonly)

Returns the value of attribute cells.



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

def cells
  @cells
end

#uiObject (readonly)

Returns the value of attribute ui.



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

def ui
  @ui
end

Instance Method Details

#bombsObject



209
210
211
# File 'lib/minesweeper/board.rb', line 209

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

#found_bombsObject



213
214
215
# File 'lib/minesweeper/board.rb', line 213

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

#number_of_cellsObject



217
218
219
# File 'lib/minesweeper/board.rb', line 217

def number_of_cells
  cells.flatten.count
end

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



22
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
# File 'lib/minesweeper/board.rb', line 22

def play(stdy = nil, stdx = nil)
  draw_board
  ui.setpos(stdy, stdx) if stdx && stdy
  noecho

  loop do
    ch = ui.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(ui.cury, ui.curx)
  end
rescue GameWon, GameOver => e
  end_game(e)
  exit
end