Class: Minesweeper::Console::GameLoop
- Inherits:
-
Object
- Object
- Minesweeper::Console::GameLoop
- 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
-
#initialize(size) ⇒ GameLoop
constructor
A new instance of GameLoop.
- #print_error(message) ⇒ Object
- #print_victory ⇒ Object
- #start ⇒ Object
- #wrap_with_pretty_lines(message) ⇒ Object
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
#print_error(message) ⇒ Object
44 45 46 |
# File 'lib/minesweeper/console/game_loop.rb', line 44 def print_error() wrap_with_pretty_lines Rainbow().yellow.bright end |
#print_victory ⇒ Object
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 |
#start ⇒ Object
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.) rescue Minesweeper::Explosives::ExplosionError => e print_error(e.) 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() puts '-' * 79, , '-' * 79 end |