﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp
{
    public class MoreOrLess
    {
        static public int ParseInput(string input)
        {
            int output;
            if(int.TryParse(input, out output))
            {
                if(output >= -1)
                {
                    return output;
                }
            }
            return -2;
        }

        static public bool ShouldContinue(int user_value, int value = 0, int tries = 0)
        {
            if (user_value == -1)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("You are weak.");
                Console.ResetColor();
                return false;
            }
            else if (user_value == value)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Congratulations, you found the number ! :)");
                Console.WriteLine("It took you only {0} tries.", tries);
                Console.ResetColor();
                return false;
            }
            else if (user_value < value)
                Console.WriteLine("It's more than that.");
            else
                Console.WriteLine("It's less than that.");
            return true;
        }

        static public void SingleGame()
        {
            int value = new Random().Next(0, 101);
            string input = "";
            bool run = true;
            int tries = 0;

            Console.WriteLine("Enter a number between 0 and 100, or -1 to give up.");

            do
            {
                tries++;
                input = Console.ReadLine();
                int user_value = ParseInput(input);

                run = ShouldContinue(user_value, value, tries);
            }
            while (run);
        }

        static public void RunGame()
        {
            Console.WriteLine("Welcome to the More Or Less game !");

            string input = "";
            do
            {
                SingleGame();

                do
                {
                    Console.WriteLine("Want to play again ? (Y/N)");
                    input = Console.ReadLine();
                } while (input != "Y" && input != "N");

            } while (input == "Y");

            Console.WriteLine("Thanks for playing ! :)");
        }
    }
}
