/*
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 Classe per gestire il gioco dell'impiccato
* \author Alessandro Bugatti
*
* \version 0.2
* \date Creazione 22/10/2006
* \date Ultima modifica 15/11/2008
*
*/
#include "giocoimpiccato.h" // class's header file
giocoImpiccato::giocoImpiccato()
{
contaErrori=0;
contaLettereUsate=0;
for (int i=0;i<26;i++)
lettereUsate[i]='\0';
}
int giocoImpiccato::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.size();i++)
if (parolaSegreta.at(i)==c)
{
parola.at(i)=c;
flag=1;
}
if (flag==1)
return 1;
contaErrori++;
return 0;
}
void giocoImpiccato::stampaStato()const
{
for (int i=0; i<parola.size();i++)
cout<<parola.at(i)<< ' ';
cout << endl << endl << endl;
for (int i=0;i<contaErrori;i++)
switch (i)
{
case 0:
cout<<"-"<<endl;
break;
case 1:
cout<<" |"<<endl;
break;
case 2:
cout<<" O"<<endl;
break;
case 3:
cout<<"/";
break;
case 4:
cout<<"|";
break;
case 5:
cout<<"\\"<<endl;
break;
case 6:
cout<<"/";
break;
case 7:
cout<<"\\";
}
cout<<endl;
}
void giocoImpiccato::setParolaSegreta(const string& s)
{
contaErrori=0;
contaLettereUsate=0;
parolaSegreta=s;
parola=parolaSegreta;
for (int i=0;i<parolaSegreta.size();i++)
parola.at(i)='_';
for (int i=0;i<25;i++)
lettereUsate[i]='\0';
}
int giocoImpiccato::controlloVittoria()const
{
if (contaErrori==8)
return 0;
for (int i=0;i<parolaSegreta.size();i++)
if (parola.at(i)=='_')
return 2;
return 1;
}
string giocoImpiccato::getParolaSegreta()const
{
return parolaSegreta;
}
void giocoImpiccato::stampaLettereUsate()const
{
for (int i=0;i<contaLettereUsate;i++)
cout<<lettereUsate[i]<<",";
cout<<endl;
}