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;
}