Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstring>
- #include <iostream>
- #include <cstdlib>
- using namespace std;
- class IMPICCATO
- {
- private:
- char* parola;
- int dimParola;
- int maxTent;
- int maxErr;
- char tent[10];
- int dimTent;
- int dimTentSbagliati;
- int stato; //1 vittoria, 0 svolgimento, -1 sconfitta
- cout<<"Inserisci parola";
- fflush stdin;
- gets (Pparola);
- public:
- /*
- Costruttore
- */
- IMPICCATO(char* Pparola)
- {
- dimParola=strlen(Pparola);
- parola=Pparola;
- maxErr=10;
- maxTent=20;
- dimTent=0;
- dimTentSbagliati=0;
- stato=0;
- }
- /*
- ....
- */
- void separatore()
- {
- cout<<endl<<"______________________________"<<endl;
- }
- /*
- visualizza le lettere trovate della parola nascosta e visualizza con dei
- trattini le lettere ancora nascoste
- */
- void visualizza()
- {
- cout<<"Numero tentativi: "<<maxTent-dimTent<<endl;
- cout<<"Errori: "<<dimTentSbagliati<<endl;
- visualizza_impiccato();
- if (stato==1) //vittoria
- {
- cout<<"***Hai vinto***"<<endl;
- separatore();
- cout<<parola<<endl;
- separatore();
- system("color 2"); //verde
- }
- else if(stato==-1) //sconfitta
- {
- cout<<"***Hai perso***"<<endl;
- separatore();
- cout<<parola<<endl;
- separatore();
- system("color c"); //rosso
- }else
- {
- separatore();
- for(int i=0; i<dimParola;i++)
- {
- bool trovato=false;
- for (int j=0;j<dimTent; j++)
- {
- if(parola[i]==tent[j])
- trovato=true;
- }
- if(trovato==false)
- cout<<"-";
- else
- cout<<parola[i];
- }
- separatore();
- }
- }
- /*
- prendo la lettera e la inserisco nel vettore dei tentativi
- return false= se l'inserimento non è avvenuro
- return true= se l'inseramento è completato
- */
- bool inserisciTentativo(char lettera)
- {
- if (stato==0)
- {
- if (dimTent<maxTent || dimTentSbagliati<maxErr)
- {
- if(inserimentoCompletato()==false) //controllo se non ho vinto
- { //e posso aggiungere lettere
- tent[dimTent]=lettera;
- dimTent ++;
- bool trovato=false;
- for(int k=0; k<dimParola;k++)
- if(lettera==parola[k])
- trovato=true;
- if(trovato==false)
- dimTentSbagliati++;
- inserimentoCompletato();
- }else
- {
- cout<<endl<<"Non posso piu inserire lettere"<<endl;
- return false;
- }
- }else
- {
- cout<<endl<<"Tentativi massimi raggiunti"<<endl;
- return false;
- }
- }else
- {
- cout<<endl<<"***GAME OVER***"<<endl;
- return false;
- }
- }
- /*
- le lettere della parola inserita devono essere contenute almeno
- una volta nel vettore dei tentativi, e mi ritorna se tutte le lettere
- della parola sono contenute nel vettore tentativi
- return true se vinto
- return false se posso ancora inserire le lettere
- */
- bool inserimentoCompletato()
- {
- for (int i=0; i<dimParola; i++)
- {
- bool trovato=false;
- for(int j=0; j<dimTent;j++)
- {
- if(parola[i]==tent[j])
- trovato=true; // ho trovato almeno una corrispondenza
- }
- if(trovato==false)
- return false; // non ho trovato nessuna corripsondenza
- // il gioco nn è finito perchè devo inserire altre lettere
- }
- stato=1;
- return true;
- //nel vettore tentativi ci sono tutte le lettere della parola
- // quindi l'inseriemnto è completato
- }
- bool indovinaParola(char* parolaIndovina)
- {
- if(strcmp(parolaIndovina, parola)==0)
- {
- cout<<endl<<"Parola indovinata"<<endl;
- stato=1;
- return true;
- }
- else
- {
- cout<<endl<<"parola non indovinata"<<endl;
- stato=-1;
- return false;
- }
- }
- int getStato()
- {
- return stato;
- }
- void visualizza_impiccato()
- {
- switch(dimTentSbagliati)
- {
- case 1:
- cout<<""<<endl;
- cout<<""<<endl;
- cout<<""<<endl;
- cout<<""<<endl;
- cout<<""<<endl;
- cout<<""<<endl;
- cout<<"____"<<endl;
- break;
- case 2:
- cout<<" |"<<endl;
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<"____"<<endl;
- break;
- case 3:
- cout<<" ---------"<endl;
- cout<<" |"<<endl;
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<"____"<<endl;
- break;
- case 4:
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<" ---------"<endl;
- cout<<" |"<<endl;
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<"____"<<endl;
- break;
- case 5:
- cout<<"()";
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<" ---------"<endl;
- cout<<" |"<<endl;
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<"____"<<endl;
- break;
- case 6:
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<"()";
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<" ---------"<endl;
- cout<<" |"<<endl;
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<"____"<<endl;
- break;
- case 7:
- cout<<"\";
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<"()";
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<" ---------"<endl;
- cout<<" |"<<endl;
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<"|"<<endl;
- cout<<"____"<<endl;
- break;
- }
- }
- };
Add Comment
Please, Sign In to add comment