Class: RavCodebreaker::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/rav_codebreaker/console.rb

Constant Summary collapse

SCORES_FILE_NAME =
'./score.dat'

Instance Method Summary collapse

Constructor Details

#initialize(level = :expert) ⇒ Console

Returns a new instance of Console.



6
7
8
9
# File 'lib/rav_codebreaker/console.rb', line 6

def initialize(level = :expert)
  @score = []
  @game = Game.new(level)
end

Instance Method Details

#again?Boolean

Returns:

  • (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_offerObject



31
32
33
# File 'lib/rav_codebreaker/console.rb', line 31

def get_offer
  @game.offer = gets.chomp
end

#incorrect_messageObject



56
57
58
# File 'lib/rav_codebreaker/console.rb', line 56

def incorrect_message
  "\nincorrect number format, try again, please..."
end

#invitation_messageObject



50
51
52
53
54
# File 'lib/rav_codebreaker/console.rb', line 50

def invitation_message
  "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_fileObject



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

#playObject



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
  welcome_message
  loop do
    puts @game.format_error? ? incorrect_message : invitation_message
    get_offer
    exit if @game.exit?
    show_hint if @game.show_hint?
    next if @game.show_hint? || @game.format_error?
    show_result_message
    show_winner_message if win?
    @game.next_turn 
    show_gameover_message if @game.game_over?
    break if win? || @game.game_over?
  end
  load_scores_from_file
  save_results
  show_results
end

#save_resultsObject



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_fileObject



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_messageObject



64
65
66
# File 'lib/rav_codebreaker/console.rb', line 64

def show_gameover_message
  puts "Sorry, you lose the game :(\nThe secret code was #{@game.secret_code}.".red
end

#show_hintObject



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_messageObject



60
61
62
# File 'lib/rav_codebreaker/console.rb', line 60

def show_result_message
   puts "You result is \"#{@game.decode_offer}\"!".green
end

#show_resultsObject



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_messageObject



68
69
70
# File 'lib/rav_codebreaker/console.rb', line 68

def show_winner_message
  puts 'We congratulate you on your victory!!!'.green
end

#welcome_messageObject



44
45
46
47
48
# File 'lib/rav_codebreaker/console.rb', line 44

def welcome_message
  puts '='*80
  puts 'Welcome to play the CodeBreaker Game!!!'.green
  puts '='*80
end

#win?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/rav_codebreaker/console.rb', line 35

def win?
  @game.win?
end