Sha256: d2bfcb75854437eae97e33e5c896a7af2f7016adb4ac97f7faa5bab9e3206eb3

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

require_relative 'game'

module Codebreaker

  class Console

    def start_game
      @game = Codebreaker::Game.new
      puts "Welcome, let's play"
      puts "Enter 4 digits from 1 to 6 to guess the secret code"
      puts "Type 'h' to find out 1 number of the secret code"
      attempt
    end

    def attempt
      answer = gets.chomp

      if /^[1-6]*$/ === answer
        guess(answer)
      elsif answer == "h"
        hint
      else
        puts "Enter please only 4 digits"
        attempt
      end
      rescue
        puts "Enter please only 4 digits size"
        attempt
    end

    def guess(answer)
      if @game.attempts > 1
        res = @game.guess_code(answer)
          if res == ["+", "+", "+", "+"]
            p res
            puts "Huraaah"
            game_over
          else
            p res
            puts "Left attempts: #{@game.attempts}"
            puts "-------------------"
            attempt
          end
      else
        res = @game.guess_code(answer)
          if res == ["+", "+", "+", "+"]
            p res
            puts "Huraaah"
            game_over
          else
            p res
            game_over
          end
      end
    end

    def hint
      if @game.hints > 0
        hint = @game.hint
        p hint
        attempt
      else
        puts "There is no hints avaliable"
        attempt
      end
    end

    def game_over
      puts "Game over"
      play_again
    end

    def play_again
      puts "Do you want to play again? Press y/n"
      answer = gets.chomp
      start_game if answer == "y"
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
game-codebreaker-0.1.0 lib/codebreaker/console.rb