class Minesweeper::Console::GameLoop

Constants

PROMPT

Public Class Methods

new(size) click to toggle source
# File lib/minesweeper/console/game_loop.rb, line 14
def initialize(size)
  @row_count = size
  @minefield = Core::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 = Core::Explosives::MineCoordinatesFactory.new(Random.new)
  @mine_layer = Core::Explosives::MineLayer.new(@minefield, mine_generator)
end

Public Instance Methods

print_error(message) click to toggle source
print_victory() click to toggle source
start() click to toggle source
# 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) click to toggle source
# File lib/minesweeper/console/game_loop.rb, line 48
def wrap_with_pretty_lines(message)
  puts '-' * 79, message, '-' * 79
end