Problem solving game in c

1

All good?

I am creating a game of gallows in C and I am facing 2 problems. The first is the defeat condition that I created. Even if the user types in a certain letter, the program gives the answer as right and wrong at the same time. The doll is being marked on the gallows. Do I have to get her out of the loop of repetition?

And the other question I have is how to stop the hit counter, because if the user types a letter more than once, which he already has typed and that exists in the word, the counter continues to be incremented. How could I solve this?

I'm just missing these two points for me to finish the project.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<locale.h>
#include <conio.h>

int erros=0, acerto=0, i;
char pergunta[20];
char resposta[]={'_','_','_','_','_','_','_','_','_','_'};
char letra;
char palavra[]={'_','_','_','_','_','_','_','_','_','_'};
int contador=0;

char cabeca=' ';
char tronco=' ';
char bracesq=' ', bracdir=' ';
char pernesq=' ', perndir=' ';
char cabeca2='ô';
char tronco2='|';
char bracesq2='||', bracdir2='||';
char pernesq2='//', perndir2='\';

void lerpergunta(){//função para ler a pergunta
FILE *f;
f=fopen ("pergunta.foc","r");
char c;
while(1){
    c=fgetc(f);
    if(c==EOF){
        break;
        }
    printf("%c",c);

}
printf("\n");
fclose(f);
}

void menu(){
printf("\n----------- Bem vindo ao Jogo da Forca 2.0-----------\n");
printf("\n\nSeu objetivo e advinhar a palavra que esta escondida"); 

printf("\n\n");
}

void desenho(){
printf("\n\n");
printf("  _______       \n");
printf(" |/      |      \n");
printf(" |       %c\n", cabeca);
printf(" |       %c \n", tronco); 
printf(" |      %c %c   \n",bracesq,bracdir); 
printf(" |      %c %c   \n",pernesq, perndir);
printf(" |              \n");
printf("_|___           \n");
printf("\n\n");
}

void lerResposta(){
FILE *f;
f=fopen ("resposta.foc","r");
char c;
int cont=0;
while(1){
    c=fgetc(f);
    if(c==EOF){
        break;
    }
    resposta[cont] = c;
    cont++;

}
printf("\n");
fclose(f);

printf("\n");
}

void boneco(){
if(erros==1){
    cabeca=cabeca2;
}
if(erros==2){
    tronco=tronco2;
}
if(erros==3){
    bracesq=bracesq2;
}
if(erros==4){
    bracdir=bracdir2;
}
if(erros==5){
    pernesq=pernesq2;
}
if(erros==6){
    perndir=perndir2;
    printf("\nVoce foi enforcado");
}
}

int main(){
setlocale(LC_ALL, "Portuguese");
menu();
lerpergunta();
lerResposta();
while(erros<=6) {
    desenho();
    fflush(stdin);
    printf("\nDigite uma letra:\n");
    letra = getch();
    for (i=0;i<10; i++) {
        if (resposta[i] == letra) {
                printf("\nLetra contem na palavra!\n");
                palavra[i] = resposta[i];
                acertos++;
        }
        else{
            printf("\nResposta errada!");
            erros++;
            boneco();
        }
    }
    for (i=0;i<10; i++) {
            printf("%c ", palavra[i]);
    }
}
return 0;
}
    
asked by anonymous 02.06.2018 / 21:52

1 answer

2

In the scope of main() , declare a variable that signals hit,

int main(int argc, char* argv[]) {
    ...
    char acertouRodada = 0;
    ...
}

In this section, change from:

    if (resposta[i] == letra) {
            printf("\nLetra contem na palavra!\n");
            palavra[i] = resposta[i];
            acertos++;
    }

for

    if (resposta[i] == letra) {
        palavra[i] = resposta[i];
        acertouRodada = 1;
    }

and check, out of for , if there was a hit or an error:

for (i=0;i<10; i++) {
    ....
    if (resposta[i] == letra) {
        ....
    }
    ....
}

// Código novo, abaixo.
if (acertouRodada) {
    printf("\nLetra contem na palavra!\n");
    acertos++;
} else {
    printf("\nResposta errada!");
    erros++;
    boneco();
}
acertouRodada = 0;

Then he stops making a hit and a mistake in the same round.

To solve the problem of repeating letters, declare a query table, in the body of main() ,

int main(int argc, char* argv[]) {
    ....
    char letraUsada[256]; // 256 é a quantidade de símbolos ASCII

    // Inicializa letraUsada.
    for (int i = 0; i < 256; i++) {
        letraUsada[i] = 0;
    }
    ....
}

At the point where there is

    if (resposta[i] == letra) {
        palavra[i] = resposta[i];
        acertouRodada = 1;
    }

Make:

    if (resposta[i] == letra) {
        if (letra >= 'a' && letra <= 'z') {
            letra = (letra - 'a') + 'A';
        }
        if (!letraUsada[letra]) {
            letraUsada[letra] = 1;
            palavra[i] = resposta[i];
            acertouRodada = 1;
        }
    }
    
02.06.2018 / 22:30