Class: RavCodebreaker::Console
- Inherits:
-
Object
- Object
- RavCodebreaker::Console
- Defined in:
- lib/rav_codebreaker/console.rb
Constant Summary collapse
- SCORES_FILE_NAME =
'./score.dat'
Instance Method Summary collapse
- #again? ⇒ Boolean
- #get_offer ⇒ Object
- #incorrect_message ⇒ Object
-
#initialize(level = :expert) ⇒ Console
constructor
A new instance of Console.
- #invitation_message ⇒ Object
- #load_scores_from_file ⇒ Object
- #play ⇒ Object
- #save_results ⇒ Object
- #save_scores_to_file ⇒ Object
- #show_gameover_message ⇒ Object
- #show_hint ⇒ Object
- #show_result_message ⇒ Object
- #show_results ⇒ Object
- #show_winner_message ⇒ Object
- #welcome_message ⇒ Object
- #win? ⇒ Boolean
Constructor Details
Instance Method Details
#again? ⇒ Boolean
39 40 41 42 |
# File 'lib/rav_codebreaker/console.rb', line 39 def again? puts 'Do you want to play again (Y or N)'.bold gets =~ /y|Y/ end |
#get_offer ⇒ Object
31 32 33 |
# File 'lib/rav_codebreaker/console.rb', line 31 def get_offer @game.offer = gets.chomp end |
#incorrect_message ⇒ Object
56 57 58 |
# File 'lib/rav_codebreaker/console.rb', line 56 def "\nincorrect number format, try again, please..." end |
#invitation_message ⇒ Object
50 51 52 53 54 |
# File 'lib/rav_codebreaker/console.rb', line 50 def "Try to guess the secret code!\n"+ "You have #{@game.turns_left} attempts and #{@game.hints_left} hints. Good luck!\n" + "Enter you four numbers code (from 1 to 6), please (or Q - for exit, H - for hint):" end |
#load_scores_from_file ⇒ Object
81 82 83 84 85 86 |
# File 'lib/rav_codebreaker/console.rb', line 81 def load_scores_from_file return unless File.exist? SCORES_FILE_NAME File.open(SCORES_FILE_NAME) do |file| @score = Marshal.load(file) end end |
#play ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/rav_codebreaker/console.rb', line 11 def play @game.start loop do puts @game.format_error? ? : get_offer exit if @game.exit? show_hint if @game.show_hint? next if @game.show_hint? || @game.format_error? if win? @game.next_turn if @game.game_over? break if win? || @game.game_over? end load_scores_from_file save_results show_results end |
#save_results ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/rav_codebreaker/console.rb', line 108 def save_results puts '"Do you want to save your results (Y/N)?' return if gets !~ /y|Y/ player = {} begin puts 'Enter your name, please...' player[:name] = gets.chomp end while player[:name].empty? player[:level] = @game.level player[:turns] = Game::TURNS_COUNT[@game.level] - @game.turns_left player[:hints] = Game::HINTS_COUNT[@game.level] - @game.hints_left @score << player save_scores_to_file end |
#save_scores_to_file ⇒ Object
88 89 90 91 92 |
# File 'lib/rav_codebreaker/console.rb', line 88 def save_scores_to_file File.open(SCORES_FILE_NAME, 'w+') do |file| Marshal.dump(@score, file) end end |
#show_gameover_message ⇒ Object
64 65 66 |
# File 'lib/rav_codebreaker/console.rb', line 64 def puts "Sorry, you lose the game :(\nThe secret code was #{@game.secret_code}.".red end |
#show_hint ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/rav_codebreaker/console.rb', line 72 def show_hint hint = @game.get_hint if hint puts "I exactly know that a number #{hint.first} is at position ##{hint.last} (remember, it starts from 0).\n".green else puts 'Sorry, but you have not any hints :('.red end end |
#show_result_message ⇒ Object
60 61 62 |
# File 'lib/rav_codebreaker/console.rb', line 60 def puts "You result is \"#{@game.decode_offer}\"!".green end |
#show_results ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/rav_codebreaker/console.rb', line 94 def show_results @score.sort_by!{|player| player[:turns]} format_str = "| %02s | %12s | %10s | %5s | %5s |" format_str_length = 50 puts '-' * format_str_length puts format_str % %w(## player\ name game\ level turns hints) puts '-' * format_str_length @score.each_with_index do |player, index| arr = [index + 1] + player.values puts format_str % arr end puts '-' * format_str_length end |
#show_winner_message ⇒ Object
68 69 70 |
# File 'lib/rav_codebreaker/console.rb', line 68 def puts 'We congratulate you on your victory!!!'.green end |
#welcome_message ⇒ Object
44 45 46 47 48 |
# File 'lib/rav_codebreaker/console.rb', line 44 def puts '='*80 puts 'Welcome to play the CodeBreaker Game!!!'.green puts '='*80 end |
#win? ⇒ Boolean
35 36 37 |
# File 'lib/rav_codebreaker/console.rb', line 35 def win? @game.win? end |