Guest User

Untitled

a guest
Apr 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.16 KB | None | 0 0
  1. #include <cstring>
  2. #include <iostream>
  3. #include <cstdlib>
  4. using namespace std;
  5.  
  6. class IMPICCATO
  7. {
  8. private:
  9. char* parola;
  10. int dimParola;
  11.  
  12. int maxTent;
  13. int maxErr;
  14. char tent[10];
  15. int dimTent;
  16. int dimTentSbagliati;
  17.  
  18. int stato; //1 vittoria, 0 svolgimento, -1 sconfitta
  19.  
  20. cout<<"Inserisci parola";
  21. fflush stdin;
  22. gets (Pparola);
  23.  
  24. public:
  25. /*
  26. Costruttore
  27. */
  28. IMPICCATO(char* Pparola)
  29. {
  30. dimParola=strlen(Pparola);
  31. parola=Pparola;
  32.  
  33. maxErr=10;
  34. maxTent=20;
  35. dimTent=0;
  36. dimTentSbagliati=0;
  37. stato=0;
  38. }
  39.  
  40. /*
  41. ....
  42. */
  43. void separatore()
  44. {
  45. cout<<endl<<"______________________________"<<endl;
  46. }
  47.  
  48. /*
  49. visualizza le lettere trovate della parola nascosta e visualizza con dei
  50. trattini le lettere ancora nascoste
  51. */
  52. void visualizza()
  53. {
  54. cout<<"Numero tentativi: "<<maxTent-dimTent<<endl;
  55. cout<<"Errori: "<<dimTentSbagliati<<endl;
  56. visualizza_impiccato();
  57. if (stato==1) //vittoria
  58. {
  59. cout<<"***Hai vinto***"<<endl;
  60. separatore();
  61. cout<<parola<<endl;
  62. separatore();
  63.  
  64. system("color 2"); //verde
  65. }
  66. else if(stato==-1) //sconfitta
  67. {
  68. cout<<"***Hai perso***"<<endl;
  69. separatore();
  70. cout<<parola<<endl;
  71. separatore();
  72.  
  73. system("color c"); //rosso
  74. }else
  75. {
  76. separatore();
  77. for(int i=0; i<dimParola;i++)
  78. {
  79. bool trovato=false;
  80. for (int j=0;j<dimTent; j++)
  81. {
  82. if(parola[i]==tent[j])
  83. trovato=true;
  84. }
  85.  
  86. if(trovato==false)
  87. cout<<"-";
  88. else
  89. cout<<parola[i];
  90.  
  91. }
  92. separatore();
  93. }
  94. }
  95.  
  96. /*
  97. prendo la lettera e la inserisco nel vettore dei tentativi
  98.  
  99. return false= se l'inserimento non è avvenuro
  100. return true= se l'inseramento è completato
  101. */
  102. bool inserisciTentativo(char lettera)
  103. {
  104. if (stato==0)
  105. {
  106. if (dimTent<maxTent || dimTentSbagliati<maxErr)
  107. {
  108. if(inserimentoCompletato()==false) //controllo se non ho vinto
  109. { //e posso aggiungere lettere
  110. tent[dimTent]=lettera;
  111. dimTent ++;
  112.  
  113. bool trovato=false;
  114. for(int k=0; k<dimParola;k++)
  115. if(lettera==parola[k])
  116. trovato=true;
  117.  
  118. if(trovato==false)
  119. dimTentSbagliati++;
  120.  
  121. inserimentoCompletato();
  122. }else
  123. {
  124. cout<<endl<<"Non posso piu inserire lettere"<<endl;
  125. return false;
  126. }
  127. }else
  128. {
  129. cout<<endl<<"Tentativi massimi raggiunti"<<endl;
  130. return false;
  131. }
  132. }else
  133. {
  134. cout<<endl<<"***GAME OVER***"<<endl;
  135. return false;
  136. }
  137. }
  138.  
  139. /*
  140. le lettere della parola inserita devono essere contenute almeno
  141. una volta nel vettore dei tentativi, e mi ritorna se tutte le lettere
  142. della parola sono contenute nel vettore tentativi
  143.  
  144. return true se vinto
  145. return false se posso ancora inserire le lettere
  146. */
  147. bool inserimentoCompletato()
  148. {
  149. for (int i=0; i<dimParola; i++)
  150. {
  151. bool trovato=false;
  152. for(int j=0; j<dimTent;j++)
  153. {
  154. if(parola[i]==tent[j])
  155. trovato=true; // ho trovato almeno una corrispondenza
  156. }
  157.  
  158. if(trovato==false)
  159. return false; // non ho trovato nessuna corripsondenza
  160. // il gioco nn è finito perchè devo inserire altre lettere
  161. }
  162. stato=1;
  163. return true;
  164. //nel vettore tentativi ci sono tutte le lettere della parola
  165. // quindi l'inseriemnto è completato
  166. }
  167.  
  168. bool indovinaParola(char* parolaIndovina)
  169. {
  170. if(strcmp(parolaIndovina, parola)==0)
  171. {
  172. cout<<endl<<"Parola indovinata"<<endl;
  173. stato=1;
  174. return true;
  175. }
  176. else
  177. {
  178. cout<<endl<<"parola non indovinata"<<endl;
  179. stato=-1;
  180. return false;
  181. }
  182. }
  183.  
  184.  
  185. int getStato()
  186. {
  187. return stato;
  188. }
  189.  
  190. void visualizza_impiccato()
  191. {
  192. switch(dimTentSbagliati)
  193. {
  194. case 1:
  195. cout<<""<<endl;
  196. cout<<""<<endl;
  197. cout<<""<<endl;
  198. cout<<""<<endl;
  199. cout<<""<<endl;
  200. cout<<""<<endl;
  201. cout<<"____"<<endl;
  202. break;
  203. case 2:
  204. cout<<" |"<<endl;
  205. cout<<"|"<<endl;
  206. cout<<"|"<<endl;
  207. cout<<"|"<<endl;
  208. cout<<"|"<<endl;
  209. cout<<"|"<<endl;
  210. cout<<"____"<<endl;
  211. break;
  212. case 3:
  213. cout<<" ---------"<endl;
  214. cout<<" |"<<endl;
  215. cout<<"|"<<endl;
  216. cout<<"|"<<endl;
  217. cout<<"|"<<endl;
  218. cout<<"|"<<endl;
  219. cout<<"|"<<endl;
  220. cout<<"____"<<endl;
  221. break;
  222. case 4:
  223.  
  224. cout<<"|"<<endl;
  225. cout<<"|"<<endl;
  226. cout<<"|"<<endl;
  227. cout<<" ---------"<endl;
  228. cout<<" |"<<endl;
  229. cout<<"|"<<endl;
  230. cout<<"|"<<endl;
  231. cout<<"|"<<endl;
  232. cout<<"|"<<endl;
  233. cout<<"|"<<endl;
  234. cout<<"____"<<endl;
  235. break;
  236. case 5:
  237. cout<<"()";
  238. cout<<"|"<<endl;
  239. cout<<"|"<<endl;
  240. cout<<"|"<<endl;
  241. cout<<" ---------"<endl;
  242. cout<<" |"<<endl;
  243. cout<<"|"<<endl;
  244. cout<<"|"<<endl;
  245. cout<<"|"<<endl;
  246. cout<<"|"<<endl;
  247. cout<<"|"<<endl;
  248. cout<<"____"<<endl;
  249. break;
  250. case 6:
  251. cout<<"|"<<endl;
  252. cout<<"|"<<endl;
  253. cout<<"()";
  254. cout<<"|"<<endl;
  255. cout<<"|"<<endl;
  256. cout<<"|"<<endl;
  257. cout<<" ---------"<endl;
  258. cout<<" |"<<endl;
  259. cout<<"|"<<endl;
  260. cout<<"|"<<endl;
  261. cout<<"|"<<endl;
  262. cout<<"|"<<endl;
  263. cout<<"|"<<endl;
  264. cout<<"____"<<endl;
  265. break;
  266. case 7:
  267. cout<<"\";
  268. cout<<"|"<<endl;
  269. cout<<"|"<<endl;
  270. cout<<"()";
  271. cout<<"|"<<endl;
  272. cout<<"|"<<endl;
  273. cout<<"|"<<endl;
  274. cout<<" ---------"<endl;
  275. cout<<" |"<<endl;
  276. cout<<"|"<<endl;
  277. cout<<"|"<<endl;
  278. cout<<"|"<<endl;
  279. cout<<"|"<<endl;
  280. cout<<"|"<<endl;
  281. cout<<"____"<<endl;
  282. break;
  283.  
  284.  
  285. }
  286.  
  287.  
  288. }
  289. };
Add Comment
Please, Sign In to add comment