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;
}
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.
– Rodrigo Springfield
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 from0
for1
. The next time the user tries to type E, he will see: oops,letraUsada['E']
is equal to0
? And since it is not, the user does not hit the round.– Marcelo Shiniti Uchimura
The
if (letra >= 'a' && letra <= 'z') { ... }
is just a check to make the round letter, in high box.– Marcelo Shiniti Uchimura