/*
  Copyright (C) 2008 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.
*/

/*! \file
 *  \brief Gioco dell'impiccato
 *  \author Alessandro Bugatti
 *
 *  \version 0.2
 *  \date  Creazione  22/10/2006
 *  \date  Ultima modifica 15/11/2008
 *
 */

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <vector>
#include <ctime>

#include "giocoimpiccato.h"

using namespace std;

int rand2()
{
    return rand()*0xFFFF+rand();
}

void menu()
{
    cout<<"1:Gioca"<<endl;
    cout<<"0:Exit"<<endl;
}

int main(int argcchar *argv[])
{
    srand(time(NULL));
    giocoImpiccato impiccato;
    char c;
    int scelta,temporaneo;
    fstream words("italian",ios::in); //oggetto con parametro il nome del file
    string temp;
    vector<stringelencoParole;
    if (words.fail()) //nel caso non trovi il file
    {
        cout<<"Non sono riuscito ad aprire il file"<<endl;
        exit(1);
    }
    //le leggiamo e le mettiamo nellla stringa temp
    while (words>>temp)
    {
        bool flag=false;
        //Questo controllo esclude eventuali parole con caratteri strani
        for (int j=0;j<temp.length();j++)
            if (!isalpha(temp.at(j))) flag=true;
        if (flag==false)
            elencoParole.push_back(temp);
    }
    //Introduzione
    cout << "Questo e' il famoso gioco dell'impiccato. Devi cercare di "
    << "Indovinare la parola segreta prima che venga completato"
    << " l'omino dell'impiccato. Attento: le parole comprendono "
    << "anche i verbi in ogni declinazione possibile. Buon gioco." << endl;
    while (1)
    {
        menu();
        cin>>scelta;
        //Pulisce lo schermo per rendere il gioco più "carino"
        if (system("cls")) system("clear");
        switch (scelta)
        {
        case 1:
            impiccato.setParolaSegreta(elencoParole[rand2()%elencoParole.size()]);
            for (;;)
            {
                impiccato.stampaStato();
                cout << endl << "Lettere già usate:" << endl;
                impiccato.stampaLettereUsate();
                cout<<"Inserisci carattere:"<<endl;
                cin>>c;
                if (system("cls")) system("clear");
                temporaneo=impiccato.inserisciCarattere(c);
                if (temporaneo==0)
                    cout<<"La lettera non e' presente nella parola segreta"<<endl;
                else
                    if (temporaneo==-1)
                        cout<<"Questa lettera l'hai gia' scritta!!!"<<endl;
                if (impiccato.controlloVittoria()==1)
                {
                    cout<<"BRAVO HAI VINTO...."<<endl;
                    cout<<"La parola segreta era "<<impiccato.getParolaSegreta()<<endl;
                    break;
                }
                if (!impiccato.controlloVittoria())
                {
                    if (system("cls")) system("clear");
                    cout<<"HAI PERSO...."<<endl;
                    cout<<"La parola segreta era "<<impiccato.getParolaSegreta()<<endl;
                    break;
                }
            }
            break;
        default:
            exit(0);
        }
    }
}