Class: Minesweeper::Console::GameLoop

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

Constant Summary collapse

PROMPT =
"(R)eveal, (F)lag, (U)nflag <x> <y>\n>"

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ GameLoop

Returns a new instance of GameLoop.



14
15
16
17
18
19
20
21
# File 'lib/minesweeper/console/game_loop.rb', line 14

def initialize(size)
  @row_count = size
  @minefield = Minefield.new(@row_count)
  @pretty_printer = Console::PrettyPrinter::MinefieldPrettyPrinter.new(@minefield, Console::PrettyPrinter::Theme::DefaultTheme.new(Rainbow.new))
  @command_parser = Console::Parser::CommandParser.new(@minefield)
  mine_generator = Explosives::MineCoordinatesFactory.new(Random.new)
  @mine_layer = Explosives::MineLayer.new(@minefield, mine_generator)
end

Instance Method Details



44
45
46
# File 'lib/minesweeper/console/game_loop.rb', line 44

def print_error(message)
  wrap_with_pretty_lines Rainbow(message).yellow.bright
end


52
53
54
# File 'lib/minesweeper/console/game_loop.rb', line 52

def print_victory
  wrap_with_pretty_lines Rainbow('Congratulations! You flagged all mines.').green.bright
end

#startObject



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

def start
  @mine_layer.lay(@row_count)
  loop do
    begin
      puts @pretty_printer.print
      user_input = Readline.readline(PROMPT, true)
      @command_parser.parse(user_input).execute
    rescue RangeError => e
      print_error('Please type coordinates within the minefield range.')
    rescue Parser::UnsupportedCommandError, Parser::InvalidCommandParametersError => e
      print_error(e.message)
    rescue Minesweeper::Explosives::ExplosionError => e
      print_error(e.message)
      exit
    rescue MinefieldSolvedError
      print_victory
      exit
    end
  end
end

#wrap_with_pretty_lines(message) ⇒ Object



48
49
50
# File 'lib/minesweeper/console/game_loop.rb', line 48

def wrap_with_pretty_lines(message)
  puts '-' * 79, message, '-' * 79
end