Class: Controller
- Inherits:
-
Object
- Object
- Controller
- Includes:
- Helper
- Defined in:
- lib/codebreaker/controller.rb
Instance Method Summary collapse
- #do_when_string(string) ⇒ Object
- #process_reply(game_answer) ⇒ Object
- #save ⇒ Object
- #start ⇒ Object
- #treatment_to_user ⇒ Object
Methods included from Helper
Instance Method Details
#do_when_string(string) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/codebreaker/controller.rb', line 40 def do_when_string(string) case string when /[\d]/ @ui.show_plus_minus(string) when 'no_hint' @ui.no_hint else @ui.show_hint(string) end end |
#process_reply(game_answer) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/codebreaker/controller.rb', line 30 def process_reply(game_answer) case game_answer when String do_when_string(game_answer) when Symbol game_answer == :won ? @ui.congratulations : @ui.sympathy(@game.secret_code) end game_answer end |
#save ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/codebreaker/controller.rb', line 51 def save if @ui.save? name = @ui.set_name File.open('data/data.txt', 'a') do |file| file.write("==========\nDate: #{Time.now}\n") file.write("Player: #{name}\n") file.write("Game: #{@game.won}\n") file.write("Spent attempts: #{@game.attempts_spent}\n") end end end |
#start ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/codebreaker/controller.rb', line 7 def start loop do @game = Codebreaker::Game.new @ui = Ui.new @game.attempts_left = @ui.set_attempts(matcher: '^[\d]+$', message: 'a number ') - 1 treatment_to_user save play_again = @ui.play_again(matcher: '^(y|n)$', message: "'y' or 'n' ") break if play_again == 'n' end end |
#treatment_to_user ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/codebreaker/controller.rb', line 19 def treatment_to_user loop do user_input = @ui.user_input( matcher: '(hint|^[1-6]{4}$)', message: "'hint' or 4 numbers" ) reply = process_reply(@game.reply(user_input)) break if [:won, :lose].include? reply end end |