Class: SvatokCodebreaker::ConsoleForGame
- Inherits:
-
Object
- Object
- SvatokCodebreaker::ConsoleForGame
- Defined in:
- lib/svatok_codebreaker/console_game.rb
Instance Method Summary collapse
- #answer_valid?(answer) ⇒ Boolean
- #command_exit ⇒ Object
- #command_hint ⇒ Object
- #command_restart ⇒ Object
- #command_save ⇒ Object
- #game_completition ⇒ Object
-
#initialize ⇒ ConsoleForGame
constructor
A new instance of ConsoleForGame.
- #prepare_game ⇒ Object
- #run_game ⇒ Object
Constructor Details
#initialize ⇒ ConsoleForGame
Returns a new instance of ConsoleForGame.
3 4 5 6 7 |
# File 'lib/svatok_codebreaker/console_game.rb', line 3 def initialize @exit_game = false @menu_commands = %w(exit restart hint save) @console_message = GameMessage.new end |
Instance Method Details
#answer_valid?(answer) ⇒ Boolean
37 38 39 |
# File 'lib/svatok_codebreaker/console_game.rb', line 37 def answer_valid?(answer) @game.valid_guess?(answer) || @menu_commands.include?(answer) end |
#command_exit ⇒ Object
41 42 43 44 |
# File 'lib/svatok_codebreaker/console_game.rb', line 41 def command_exit @exit_game = true @console_message.show(:exit_game) end |
#command_hint ⇒ Object
51 52 53 54 |
# File 'lib/svatok_codebreaker/console_game.rb', line 51 def command_hint return @console_message.show(:no_hint) unless @game.hint @game.show_hint end |
#command_restart ⇒ Object
46 47 48 49 |
# File 'lib/svatok_codebreaker/console_game.rb', line 46 def command_restart @game = Game.new(@player_name) @console_message.show(:restart_game) end |
#command_save ⇒ Object
56 57 58 59 60 |
# File 'lib/svatok_codebreaker/console_game.rb', line 56 def command_save return @console_message.show(:not_available) unless @game.end_of_game? @game.save_game @console_message.show(:saved) end |
#game_completition ⇒ Object
32 33 34 35 |
# File 'lib/svatok_codebreaker/console_game.rb', line 32 def game_completition return @console_message.show(:win) if @game.marking_guess == '++++' @console_message.show(:lose, @game.get_game_data) end |
#prepare_game ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/svatok_codebreaker/console_game.rb', line 9 def prepare_game puts @console_message.show(:about) puts @console_message.show(:login) @player_name = gets.chomp puts @console_message.show(:start) run_game end |
#run_game ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/svatok_codebreaker/console_game.rb', line 17 def run_game @game = Game.new(@player_name) until @exit_game answer = gets.chomp begin raise puts @console_message.show(:not_valid_answer) unless answer_valid?(answer) raise puts send(('command_' + answer.downcase).to_sym) if @menu_commands.include?(answer) @game.submit_guess(answer) puts @game.end_of_game? ? game_completition : @console_message.show(:next_step, @game.get_game_data) rescue next end end end |