Skip to content

Instantly share code, notes, and snippets.

@mauricioaniche
Last active November 26, 2023 17:38
Show Gist options
  • Select an option

  • Save mauricioaniche/dd0b366ad9c4f7ee0cd6 to your computer and use it in GitHub Desktop.

Select an option

Save mauricioaniche/dd0b366ad9c4f7ee0cd6 to your computer and use it in GitHub Desktop.

Revisions

  1. mauricioaniche revised this gist Jan 17, 2015. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions forca.c
    Original file line number Diff line number Diff line change
    @@ -99,9 +99,8 @@ void escolhepalavra() {
    srand(time(0));
    int randomico = rand() % qtddepalavras;

    for(int i = 0; i < randomico; i++) {
    for(int i = 0; i <= randomico; i++) {
    fscanf(f, "%s", palavrasecreta);
    if(i == randomico) break;
    }

    fclose(f);
  2. mauricioaniche created this gist Jan 17, 2015.
    122 changes: 122 additions & 0 deletions forca.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,122 @@
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #include "forca.h"

    char palavrasecreta[20];
    char chutes[26];
    int chutesdados = 0;

    int enforcou() {

    int erros = 0;

    for(int i = 0; i < chutesdados; i++) {

    int existe = 0;

    for(int j = 0; j < strlen(palavrasecreta); j++) {
    if(chutes[i] == palavrasecreta[j]) {
    existe = 1;
    break;
    }
    }

    if(!existe) erros++;
    }

    return erros >= 5;
    }

    int ganhou() {
    for(int i = 0; i < strlen(palavrasecreta); i++) {
    if(!jachutou(palavrasecreta[i])) {
    return 0;
    }
    }

    return 1;
    }


    void abertura() {
    printf("/****************/\n");
    printf("/ Jogo de Forca */\n");
    printf("/****************/\n\n");
    }

    void chuta() {
    char chute;
    printf("Qual letra? ");
    scanf(" %c", &chute);

    chutes[chutesdados] = chute;
    chutesdados++;
    }

    int jachutou(char letra) {
    int achou = 0;
    for(int j = 0; j < chutesdados; j++) {
    if(chutes[j] == letra) {
    achou = 1;
    break;
    }
    }

    return achou;
    }

    void desenhaforca() {

    printf("Você já deu %d chutes\n", chutesdados);

    for(int i = 0; i < strlen(palavrasecreta); i++) {

    if(jachutou(palavrasecreta[i])) {
    printf("%c ", palavrasecreta[i]);
    } else {
    printf("_ ");
    }

    }
    printf("\n");

    }

    void escolhepalavra() {
    FILE* f;

    f = fopen("palavras.txt", "r");
    if(f == 0) {
    printf("Banco de dados de palavras não disponível\n\n");
    exit(1);
    }

    int qtddepalavras;
    fscanf(f, "%d", &qtddepalavras);

    srand(time(0));
    int randomico = rand() % qtddepalavras;

    for(int i = 0; i < randomico; i++) {
    fscanf(f, "%s", palavrasecreta);
    if(i == randomico) break;
    }

    fclose(f);
    }

    int main() {

    abertura();
    escolhepalavra();

    do {

    desenhaforca();
    chuta();

    } while (!ganhou() && !enforcou());

    }