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

namespace Hangman
{
    public class Hangman
    {
        int max_lifes;
        int lifes;
        int state;
        string solution;
        char[] word;

        public Hangman()
        {
            max_lifes = 5;
            solution = "";
            state = 0;
        }

        public Hangman(int i)
        {
            max_lifes = i;
        }

        public void newGame(string s)
        {
            word = new char[s.Length];
            for (int i = 0; i < word.Length; i++)
                word[i] = '_';
            lifes = max_lifes;
            solution = s;
            state = 0;
        }

        public bool Try(char c)
        {
            bool p = false;
            for (int i = 0; i < solution.Length; i++)
                if (solution[i] == c)
                {
                    p = true;
                    word[i] = c;
                }
            if (!p)
                lifes--;
            return p;
        }

        public int numLifesLeft()
        {
            return lifes;
        }

        bool equal()
        {
            bool p = true;
            for (int i = 0; i < solution.Length; i++)
            {
                if (solution[i] != word[i])
                    p = false;
            }
            return p;
        }

        public bool endGame()
        {
            if (lifes == 0)
            {
                state = 2;
                return true;
            }
            else if (equal())
            {
                state = 1;
                return true;
            }
            else
                return false;
        }

        public int returnState()
        {
            return state;
        }

        public string returnWord()
        {
            string s = "";
            for (int i = 0; i < word.Length; i++)
                s += word[i];
            return s;
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            Hangman h = new Hangman();
            Console.WriteLine("Unesite rec");
            string s = Console.ReadLine();
            h.newGame(s);
            Console.WriteLine("Broj zivota: {0}", h.numLifesLeft());
            Console.WriteLine(h.returnWord());
            while (!h.endGame())
            {
                char c = Convert.ToChar(Console.ReadLine());
                if (h.Try(c))
                    Console.WriteLine("Pogodak");
                else
                    Console.WriteLine("Promasaj");
                Console.WriteLine("Broj zivota: {0}", h.numLifesLeft());
                Console.WriteLine(h.returnWord());

            }
            if (h.returnState() == 1)
                Console.WriteLine("Pobedio si");
            else
                Console.WriteLine("Izgubio si");
            Console.ReadKey();
        }
    }
}

