class Game
  def initialize
    clear_screen
    start_message
    game_instructions
    key
    continue
    engine
  end

  def clear_screen
    system "clear" or system "cls"
  end

  def start_message
    puts <<-STR
                      _                      _           _
      /\\/\\   __ _ ___| |_ ___ _ __ _ __ ___ (_)_ __   __| |
     /    \\ / _` / __| __/ _ \\ '__| '_ ` _ \\| | '_ \\ / _` |
    / /\\/\\ \\ (_| \\__ \\ ||  __/ |  | | | | | | | | | | (_| |
    \\/    \\/\\__,_|___/\\__\\___|_|  |_| |_| |_|_|_| |_|\\__,_|


    STR
  end

  def game_instructions
    puts "--INSTRUCTIONS--: "
    puts "There are four spots (_ _ _ _). Guess the four random numbers from \
1-8 (ex: 6158). You have 12 tries."
  end

  def key
    puts <<-STR
    + = Correct number & spot
    - = Right number, wrong spot
    [] = Number not in sequence

    STR
  end

  def continue
    puts "Enter any key to continue, or Q to quit."
    user_input = gets.chomp.downcase.strip
    exit if user_input == 'q'
    else
      generate_random
  end

  def generate_random
    answer = []
    4.times do
      answer << rand(1..8)
    end
    @answer = answer.join("")
  end

  def engine
    @number_of_tries = 12
    1.times do
      while @number_of_tries > 0
        print "\nGuess the four random numbers from 1-8 (ex: 6158). You have #{@number_of_tries} tries left. \n>>> "
        input = gets.chomp.strip
        if input =~ /^[1-8]{4}$/
          @number_of_tries -= 1
        else redo
          puts "\n--- Please enter a four-digit number from 1-8 ---"
        engine
        end
        result = parse_input(input)
        if result == "++++"
          puts "You win!"
          exit
        else
          puts "You entered: #{input} "
          puts "Hint(s): #{result}"
        end
      end
      puts "\nGame Over, The correct answer was #{@answer}"
      exit
    end
  end

  def parse_input(input)
    output = []
    input.split('').each_with_index { |i, j|
    if @answer.include?(i)
      if @answer[j] == input[j]
        output << "+"
      else
        output << "-"
      end
    end
  }
    output.join("")
  end

end


Game.new
