/*
  Copyright (C) 2013 Alessandro Bugatti (alessandro.bugatti@istruzione.it)

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package giocoimpiccato;

/*! \file
 *  \brief Classe per gestire il gioco dell'impiccato
 *  \author Alessandro Bugatti
 *  \version 0.1
 *  \date  Creazione 25-nov-2013
 *  \date  Ultima modifica 25-nov-2013
 */


public class Impiccato {
    private int contaErrori;///< conta gli errori commessi per indovinare la parola (a 8 perdi)
    private String parolaSegreta///< contiene la parola da indovinare
    private String parola///< la parola dove si inseriranno le lettere scelte dall'utente e gli underscore dove non ci sono i caratteri
    private char lettereUsate[];  ///< Contiene le lettere già usate nel gioco
    private int contaLettereUsate///< Conta le lettere usate già nel gioco
    
    public Impiccato()
    {
        contaErrori=0;
        contaLettereUsate=0;
        lettereUsate new char[26];
        for (int i=0;i<26;i++)
            lettereUsate[i]='\0';
    }
    
    public int inserisciCarattere(char c)
    {
        int flag=0;
        for (int i=0;i<26;i++)
            if (lettereUsate[i]==c)
                return -1;
        //Inserisce la lettera usata nel vettore e incrementa il contatore di lettere
        lettereUsate[contaLettereUsate++]=c;
        for (int i=0;i<parolaSegreta.length();i++)
            if (parolaSegreta.charAt(i)==c)
            {
                char temp[] = parola.toCharArray();
                temp[i] = c;
                parola new String(temp);
                flag=1;
            }
        if (flag==1)
            return 1;
        contaErrori++;
        return 0;
    }
    
    public void stampaStato()
    {
        for (int i=0i<parola.length();i++)
            System.out.print(parola.charAt(i) +  " ");
        System.out.println("\n\n\n");
        for (int i=0;i<contaErrori;i++)
            switch (i)
            {
            case 0:
                System.out.println("-");
                break;
            case 1:
                System.out.println(" |");
                break;
            case 2:
                System.out.println(" O");
                break;
            case 3:
                System.out.print("/");
                break;
            case 4:
                System.out.print("|");
                break;
            case 5:
                System.out.println("\\");
                break;
            case 6:
                System.out.print("/");
                break;
            case 7:
                System.out.println("\\");
            }
        System.out.print("\n");
    }

    public void setParolaSegreta(String s)
    {
        contaErrori=0;
        contaLettereUsate=0;
        parolaSegreta=s;
        parola=parolaSegreta;
        char temp[] = new char[parolaSegreta.length()];
        for (int i=0;i<parolaSegreta.length();i++)
            temp[i] = '_';
        parola new String(temp);
        for (int i=0;i<25;i++)
            lettereUsate[i]='\0';
    }
    
    public int controlloVittoria()
    {
        if (contaErrori==8)
            return 0;
        for (int i=0;i<parolaSegreta.length();i++)
            if (parola.charAt(i)=='_')
                return 2;
        return 1;
    }

    String getParolaSegreta()
    {
        return parolaSegreta;
    }

    void stampaLettereUsate()
    {
        for (int i=0;i<contaLettereUsate;i++)
            System.out.print(lettereUsate[i] + ",");
        System.out.print("\n");
    }
}