Hangman game problem in c

Asked

Viewed 375 times

1

all good?

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

And the other question I have is how to stop the hit counter, because if the user type a letter more than once, that it has already typed and exists in the word, the counter keeps being incremented. How could I fix this?

Just these two points left 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;
}

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:

    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 the for, whether there has been a hit or a mistake:

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;

Hence he stops giving hit and error in the same round.

To solve the repetition of letters problem, 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

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

Do:

    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;
        }
    }
  • Thank you very much for your help, Marcelo. I just couldn’t quite understand its logic to not count the letters that were already typed, but otherwise, it worked fine.

  • I used a vector as a dictionary; that is, I used characters as vector index. When a letter, for example, E is typed, letraUsada['E'] will go from 0 for 1. The next time the user tries to type E, he will see: oops, letraUsada['E'] is equal to 0? And since it is not, the user does not hit the round.

  • The if (letra >= 'a' && letra <= 'z') { ... } is just a check to make the round letter, in high box.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.