#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#define MAX_TRIES 6
// Lista de cuvinte pentru joc
const char * cuvinte[ ] = { "calculator" , "programare" , "joc" , "variabila" , "functie" , "algoritm" , "structura" } ;
int numar_cuvinte = sizeof ( cuvinte) / sizeof ( cuvinte[ 0 ] ) ;
void afiseaza_progres( char * cuvant_ghicit, int lungime) {
for ( int i = 0 ; i < lungime; i++ ) {
printf ( "%c " , cuvant_ghicit
[ i
] ) ; }
}
int main( ) {
// Inițializează generatorul de numere aleatorii
const char * cuvant_secret
= cuvinte
[ rand ( ) % numar_cuvinte
] ; // Alege un cuvânt aleatoriu int lungime
= strlen ( cuvant_secret
) ;
char cuvant_ghicit[ lungime + 1 ] ;
for ( int i = 0 ; i < lungime; i++ ) {
cuvant_ghicit[ i] = '_' ; // Inițial, toate literele sunt neghicite
}
cuvant_ghicit[ lungime] = '\0 ' ;
int incercari_ramase = MAX_TRIES;
int litere_ramase = lungime;
char litera;
int gasit;
printf ( "Bine ai venit la jocul de Spânzurătoarea!\n " ) ; printf ( "Trebuie să ghicești cuvântul: " ) ; afiseaza_progres( cuvant_ghicit, lungime) ;
printf ( "Ai %d încercări.\n " , MAX_TRIES
) ;
// Buclează până când utilizatorul epuizează încercările sau ghicește cuvântul
while ( incercari_ramase > 0 && litere_ramase > 0 ) {
gasit = 0 ;
litera
= tolower ( litera
) ; // Convertește litera la minusculă pentru consistență
// Verifică dacă litera este în cuvânt
for ( int i = 0 ; i < lungime; i++ ) {
if ( cuvant_secret[ i] == litera && cuvant_ghicit[ i] == '_' ) {
cuvant_ghicit[ i] = litera;
litere_ramase--;
gasit = 1 ;
}
}
if ( ! gasit) {
incercari_ramase--;
printf ( "Litera nu este în cuvânt. Încercări rămase: %d\n " , incercari_ramase
) ; } else {
printf ( "Ai ghicit o literă!\n " ) ; }
afiseaza_progres( cuvant_ghicit, lungime) ;
}
// Afișează rezultatul final
if ( litere_ramase == 0 ) {
printf ( "Felicitări! Ai ghicit cuvântul: %s\n " , cuvant_secret
) ; } else {
printf ( "Ai pierdut! Cuvântul era: %s\n " , cuvant_secret
) ; }
return 0 ;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgojaW5jbHVkZSA8dGltZS5oPgojaW5jbHVkZSA8Y3R5cGUuaD4KCiNkZWZpbmUgTUFYX1RSSUVTIDYKCi8vIExpc3RhIGRlIGN1dmludGUgcGVudHJ1IGpvYwpjb25zdCBjaGFyICpjdXZpbnRlW10gPSB7ImNhbGN1bGF0b3IiLCAicHJvZ3JhbWFyZSIsICJqb2MiLCAidmFyaWFiaWxhIiwgImZ1bmN0aWUiLCAiYWxnb3JpdG0iLCAic3RydWN0dXJhIn07CmludCBudW1hcl9jdXZpbnRlID0gc2l6ZW9mKGN1dmludGUpIC8gc2l6ZW9mKGN1dmludGVbMF0pOwoKdm9pZCBhZmlzZWF6YV9wcm9ncmVzKGNoYXIgKmN1dmFudF9naGljaXQsIGludCBsdW5naW1lKSB7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IGx1bmdpbWU7IGkrKykgewogICAgICAgIHByaW50ZigiJWMgIiwgY3V2YW50X2doaWNpdFtpXSk7CiAgICB9CiAgICBwcmludGYoIlxuIik7Cn0KCmludCBtYWluKCkgewogICAgLy8gSW5pyJtpYWxpemVhesSDIGdlbmVyYXRvcnVsIGRlIG51bWVyZSBhbGVhdG9yaWkKICAgIHNyYW5kKHRpbWUoMCkpOwogICAgY29uc3QgY2hhciAqY3V2YW50X3NlY3JldCA9IGN1dmludGVbcmFuZCgpICUgbnVtYXJfY3V2aW50ZV07ICAvLyBBbGVnZSB1biBjdXbDom50IGFsZWF0b3JpdQogICAgaW50IGx1bmdpbWUgPSBzdHJsZW4oY3V2YW50X3NlY3JldCk7CgogICAgY2hhciBjdXZhbnRfZ2hpY2l0W2x1bmdpbWUgKyAxXTsKICAgIGZvciAoaW50I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