class CodebreakerArtem::CLI
Public Class Methods
exit?(input)
click to toggle source
# File lib/codebreaker_artem/cli.rb, line 72 def exit?(input) exit if input =~ /^exit$/i end
finish_game(code, score)
click to toggle source
# File lib/codebreaker_artem/cli.rb, line 55 def finish_game(code, score) reveal_code(code) save_score(score) end
guess_prompt(guess_count)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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_only()
click to toggle source
# File lib/codebreaker_artem/cli.rb, line 64 def one_hint_only puts 'You have only one hint!' end
play_again()
click to toggle source
# 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)
click to toggle source
# File lib/codebreaker_artem/cli.rb, line 60 def reveal_code(code) puts "The secret code was #{code}\n" end
save_score(score)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/codebreaker_artem/cli.rb, line 33 def show_mark(mark) puts "Mark corresponding your guess: #{mark}" end
submit_guess()
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/codebreaker_artem/cli.rb, line 37 def win(code, score) win_msg finish_game(code, score) end
win_msg()
click to toggle source
# 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)
click to toggle source
# 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_pattern()
click to toggle source
# 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?() { || ... }
click to toggle source
# 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