Class: CodebreakerArtem::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/codebreaker_artem/cli.rb

Class Method Summary collapse

Class Method Details

.exit?(input) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/codebreaker_artem/cli.rb', line 72

def exit?(input)
  exit if input =~ /^exit$/i
end

.finish_game(code, score) ⇒ Object



55
56
57
58
# File 'lib/codebreaker_artem/cli.rb', line 55

def finish_game(code, score)
  reveal_code(code)
  save_score(score)
end

.guess_prompt(guess_count) ⇒ Object



16
17
18
# File 'lib/codebreaker_artem/cli.rb', line 16

def guess_prompt(guess_count)
  print "Please enter your guess no. #{guess_count + 1}: "
end

.lose(code, score, max_guess_number) ⇒ Object



42
43
44
45
# File 'lib/codebreaker_artem/cli.rb', line 42

def lose(code, score, max_guess_number)
  lose_msg(max_guess_number)
  finish_game(code, score)
end

.lose_msg(max_guess_number) ⇒ Object



51
52
53
# File 'lib/codebreaker_artem/cli.rb', line 51

def lose_msg(max_guess_number)
  puts "\nGAME OVER. You've used all #{max_guess_number} attempts\n"
end

.one_hint_onlyObject



64
65
66
# File 'lib/codebreaker_artem/cli.rb', line 64

def one_hint_only
  puts 'You have only one hint!'
end

.play_againObject



98
99
100
101
# File 'lib/codebreaker_artem/cli.rb', line 98

def play_again
  exit unless yes? { 'Would you like to play one more time? [y/n]: ' }
  true
end

.reveal_code(code) ⇒ Object



60
61
62
# File 'lib/codebreaker_artem/cli.rb', line 60

def reveal_code(code)
  puts "The secret code was #{code}\n"
end

.save_score(score) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/codebreaker_artem/cli.rb', line 76

def save_score(score)
  return false unless yes? { 'Do you want to save your score? [y/n]: ' }
  begin
    Dir.mkdir('./score') unless File.exist?('./score')
    file = File.new('./score/score.txt', 'a')
  rescue
    puts "Can't create file './score/score.txt'"
    return
  end
  write_score_to_file(file, score)
  file.close
end

.show_hint(number = nil, position = nil) ⇒ Object



28
29
30
31
# File 'lib/codebreaker_artem/cli.rb', line 28

def show_hint(number = nil, position = nil)
  return one_hint_only unless number && position
  puts "HINT: Number #{number} is in position #{position + 1}"
end

.show_mark(mark) ⇒ Object



33
34
35
# File 'lib/codebreaker_artem/cli.rb', line 33

def show_mark(mark)
  puts "Mark corresponding your guess: #{mark}"
end

.submit_guessObject



20
21
22
23
24
25
26
# File 'lib/codebreaker_artem/cli.rb', line 20

def submit_guess
  guess = $stdin.gets.chomp
  exit?(guess)
  return :hint if guess =~ /^hint$/i
  return wrong_code_pattern unless Validator.code_valid?(guess)
  guess
end

.welcome_msg(max_guess_number) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/codebreaker_artem/cli.rb', line 6

def welcome_msg(max_guess_number)
  puts %(
    Welcome to the Codebreaker Game!
    New secret code has just been generated
    You have #{max_guess_number} attempts
    Type 'hint' to take a hint
    Type 'exit' to exit the game
  )
end

.win(code, score) ⇒ Object



37
38
39
40
# File 'lib/codebreaker_artem/cli.rb', line 37

def win(code, score)
  win_msg
  finish_game(code, score)
end

.win_msgObject



47
48
49
# File 'lib/codebreaker_artem/cli.rb', line 47

def win_msg
  puts "\nCONGRATULATIONS! YOU'VE WON the Codebreaker game!\n"
end

.write_score_to_file(file, score) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/codebreaker_artem/cli.rb', line 89

def write_score_to_file(file, score)
  print 'Please enter your name: '
  name = $stdin.gets.chomp
  file << "Name: #{name}\n"
  file << "Time: #{Time.now}\n"
  file << "Score: #{score}\n\n"
  puts "Your score was added to a file #{file.path}"
end

.wrong_code_patternObject



68
69
70
# File 'lib/codebreaker_artem/cli.rb', line 68

def wrong_code_pattern
  puts 'Code must contain 4 numbers from 1 to 6. Please try again'
end

.yes?Boolean

Returns:

  • (Boolean)


103
104
105
106
107
108
109
110
111
112
# File 'lib/codebreaker_artem/cli.rb', line 103

def yes?
  print yield if block_given?
  loop do
    answer = $stdin.gets.chomp
    exit?(answer)
    return false if answer =~ /^n/i
    return true if answer =~ /^y/i
    puts "Please enter 'y' or 'n': "
  end
end