Class: Minesweeprb::Gameboard

Inherits:
Object
  • Object
show all
Includes:
Curses
Defined in:
lib/minesweeprb/gameboard.rb

Constant Summary collapse

RESTART =
['r'].freeze
REVEAL =
[10, KEY_ENTER].freeze
FLAG =
['f', ' '].freeze
BACK =
['q', 27, 127, KEY_BACKSPACE].freeze
MOVE =
{
  KEY_UP => :up,
  KEY_DOWN => :down,
  KEY_LEFT => :left,
  KEY_RIGHT => :right,
  'k' => :up,
  'j' => :down,
  'h' => :left,
  'l' => :right,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(game, theme:) ⇒ Gameboard

Returns a new instance of Gameboard.



27
28
29
30
31
32
# File 'lib/minesweeprb/gameboard.rb', line 27

def initialize(game, theme:)
  @game = game
  @theme = theme
  @active_square_sprite = theme.sprites[:active_square]
  build_color_map
end

Instance Attribute Details

#gameObject (readonly)

Returns the value of attribute game.



25
26
27
# File 'lib/minesweeprb/gameboard.rb', line 25

def game
  @game
end

#game_xObject (readonly)

Returns the value of attribute game_x.



25
26
27
# File 'lib/minesweeprb/gameboard.rb', line 25

def game_x
  @game_x
end

#game_yObject (readonly)

Returns the value of attribute game_y.



25
26
27
# File 'lib/minesweeprb/gameboard.rb', line 25

def game_y
  @game_y
end

#windowsObject (readonly)

Returns the value of attribute windows.



25
26
27
# File 'lib/minesweeprb/gameboard.rb', line 25

def windows
  @windows
end

Instance Method Details

#drawObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/minesweeprb/gameboard.rb', line 54

def draw
  setup_windows
  @timer_thread = Thread.new do
    loop do
      paint_header
      sleep(0.5)
    end
  end

  paint_grid
  paint_grid while process_input(w_grid.getch)
ensure
  @timer_thread&.kill
  windows.each_value do |w|
    w.close
  rescue StandardError
    nil
  end
  @windows = nil
end

#w_debugObject



50
51
52
# File 'lib/minesweeprb/gameboard.rb', line 50

def w_debug
  windows[:debug]
end

#w_gridObject



38
39
40
# File 'lib/minesweeprb/gameboard.rb', line 38

def w_grid
  windows[:grid]
end

#w_headerObject



34
35
36
# File 'lib/minesweeprb/gameboard.rb', line 34

def w_header
  windows[:header]
end

#w_instructionsObject



46
47
48
# File 'lib/minesweeprb/gameboard.rb', line 46

def w_instructions
  windows[:instructions]
end

#w_statusObject



42
43
44
# File 'lib/minesweeprb/gameboard.rb', line 42

def w_status
  windows[:status]
end