Skip to content

Commit

Permalink
Adds handling of RangeErrors and MinefieldSolvedErrors in the game loop.
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienatplumbee committed Jun 10, 2015
1 parent 5dc52cb commit 8dd1a8c
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions lib/minesweeper/console/game_loop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,34 @@ 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
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)
print_error(e.message)
rescue Minesweeper::Explosives::ExplosionError => e
print_error(e)
print_error(e.message)
exit
rescue MinefieldSolvedError
print_victory
exit
end
end
end

def print_error(e)
puts '-' * 79
puts Rainbow(e.message).yellow.bright
puts '-' * 79
def print_error(message)
wrap_with_pretty_lines Rainbow(message).yellow.bright
end

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

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

0 comments on commit 8dd1a8c

Please sign in to comment.