From 99c5a7944ae8182f07e8466f8dc8ed0ba45d7d4a Mon Sep 17 00:00:00 2001 From: structix Date: Wed, 3 May 2017 21:31:51 +0200 Subject: [PATCH] #7 use last word in case the hits are higher --- hangman.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/hangman.c b/hangman.c index 13059e3..196f379 100644 --- a/hangman.c +++ b/hangman.c @@ -258,7 +258,7 @@ void printGameStats(game_state *gs) { sprintf(message, "Game won! Total guesses: %i", gs->guesses); mvprintw(gs->centery, gs->centerx - (strlen(message) / 2), message); sprintf(message, "Wrong guesses: %i, right/wrong ratio: %.2f", gs->moves, (float)(gs->guesses - gs->moves) / gs->moves); - mvprintw(gs->centery, gs->centerx - (strlen(message) / 2), message); + mvprintw(gs->centery + 1, gs->centerx - (strlen(message) / 2), message); } refresh(); @@ -281,15 +281,19 @@ void readRandomLine(char *file, char *result) { long totalLength = 0, rand = 0; FILE *fp = fopen(file, "r"); char c; - + int wordlength = 0; + while ((c = fgetc(fp)) != EOF) { if (c == '\n') ++totalLength; } rand = getrandom(0, totalLength); - fseek(fp, rand, SEEK_SET); + fseek(fp, -rand, SEEK_END); fgets(result, MAXWORDLENGTH, fp); + /* remove \n at the end of the line */ + wordlength = strlen(result); + result[wordlength - 1] = '\0'; fclose(fp); } @@ -307,6 +311,10 @@ void trollHitScreen(game_state *gs, int hits) { break; } } + if (hits > 10) { + found = 1; + i = 8; + } if (found) { flash(); /* flash the screen */ mvprintw(5, centerDiff(gs->centerx, strings[i]), strings[i]);