class Controller

Public Instance Methods

do_when_string(string) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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