Sha256: 37a13cf2624daaf03ce764eea4b7f97078fedf6c4b53bab8b3e6eacf4062b561

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

require_relative 'messages'

module Codebreaker
  class ConsoleApp
    def initialize
      @game = Game.new
    end

    def run
      Messages.logo
      Messages.welcome

      loop do
        Messages.run
        print '> '

        case gets.chomp
        when '1' then play
        when '2' then puts high_scores
        when '0'
          Messages.bye
          break
        else Messages.wrong_option
        end
      end
    end

    private

    def play
      @game.start
      Messages.play

      until @game.finished?
        print '>>> '
        input = gets.chomp

        case input
        when '1' then puts @game.use_hint, "\n"
        when '0' then return
        else check_guess(input)
        end
      end

      if @game.won?
        Messages.win(@game.answer, @game.tries)
        save_score
      else
        Messages.lose(@game.answer)
      end
    end

    def high_scores
      scores = @game.high_scores

      return 'No scores' if scores.empty?

      scores.map
            .with_index { |player, i| format("%2i #{player.formatted}", i + 1) }
            .unshift(format('%2s %10s %5s', '', 'Name', 'Tries'))
    end

    def check_guess(input)
      puts @game.check_guess(input)
      Messages.tries_left(@game.tries_left)
    rescue ArgumentError => ex
      puts ex.message, "\n"
    end

    def save_score
      print 'Do you want to save your score? y/[n]: '

      return unless gets.chomp.casecmp('y').zero?

      begin
        print 'Type your name: '
        @game.save_score(gets.chomp)
        puts 'Your score was saved'
      rescue ArgumentError => ex
        puts ex.message, "\n"
        retry
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ep-codebreaker-0.1.2 lib/ep-codebreaker/console_app.rb
ep-codebreaker-0.1.1 lib/ep-codebreaker/console_app.rb