Last active
November 26, 2023 17:38
-
-
Save mauricioaniche/dd0b366ad9c4f7ee0cd6 to your computer and use it in GitHub Desktop.
Revisions
-
mauricioaniche revised this gist
Jan 17, 2015 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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++) { fscanf(f, "%s", palavrasecreta); } fclose(f); -
mauricioaniche created this gist
Jan 17, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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()); }