1
resposta

Apenas deixando o código aqui :)

#include <stdio.h>
#include <string.h>
#include <ctype.h>

//Globals
char secret_word[20];
char guesses[26];
int attempts = 0;

//Functions
void intro(){
    printf("\n\n");
    printf("*************************************************** \n");
    printf("  _                                                    \n");
    printf(" | |                                                \n");
    printf(" | |__   __ _ _ __   __ _ _ __ ___   __ _ _ __      \n");
    printf(" | '_ \\ / _` | '_ \\ / _` | '_ ` _ \\ / _` | '_ \\ \n");
    printf(" | | | | (_| | | | | (_| | | | | | | (_| | | | |    \n");
    printf(" |_| |_|\\__,_|_| |_|\\__, |_| |_| |_|\\__,_|_| |_| \n");
    printf("                     __/ |                          \n");
    printf("                    |___/                           \n");
    printf("*************************************************** \n");
    printf("\n");
}

void shot(){
    char guess;

    printf("\nChoose a letter: ");
    fflush(stdin);
    scanf("%c", &guess);

    guesses[attempts] = toupper(guess);
    attempts++;
}

int shotchecked(char letter){
    int found = 0;

    for (int j = 0; j < attempts; j++) {
        if (guesses[j] == letter) {
            found = 1;
            break;
        }   
    }
    return found;
}

void wordchoice(){
    sprintf(secret_word, "MELANCIA");
}

void drawgallows(){

    for (int i = 0; i < strlen(secret_word); i++) {
        if (shotchecked(secret_word[i])){
            printf("%c ", secret_word[i]);
        } else {
            printf("_ ");
        }
    }
    printf("\n(You already had %d attempts)\n", attempts);
    printf("\n");
}

int main(){

    int hit = 0;
    int hanged = 0;

    intro();
    wordchoice();

    printf("SECRET WORD: ");

    do {
        drawgallows();
        shot();        
    } while (!hit && !hanged);
}
1 resposta

Olá, Bárbara! Tudo bem?

Obrigada por compartilhar o código com a gente. Com certeza está ajudando outros alunos!

Abraços!

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software