From d9f9acc87c3ccd6ccab1404638a847ffc516b3f6 Mon Sep 17 00:00:00 2001 From: Daniel Eisele Date: Wed, 23 Aug 2017 21:15:18 +0200 Subject: [PATCH 01/11] Cleanup --- figures.h | 26 ++++++------- hangman.c | 114 ++++++++++++++++++++++++++---------------------------- hangman.h | 14 +++---- prng.c | 4 +- prng.h | 2 +- 5 files changed, 76 insertions(+), 84 deletions(-) diff --git a/figures.h b/figures.h index 73abbcd..20f6255 100644 --- a/figures.h +++ b/figures.h @@ -1,18 +1,18 @@ const char base_figure[6][8] = {{"======="}, \ - {"I "}, \ - {"I "}, \ - {"I "}, \ - {"I "}, \ - {"I\\ "}}; - + {"I "}, \ + {"I "}, \ + {"I "}, \ + {"I "}, \ + {"I\\ "}}; + const char figure[6][5] = {{" O "}, \ - {" I "}, \ - {"\\I "}, \ - {"\\I/"}, \ - {"/ "}, \ - {"/ \\"}}; - + {" I "}, \ + {"\\I "}, \ + {"\\I/"}, \ + {"/ "}, \ + {"/ \\"}}; + /* This array represents the layers where the strings - of the current stage(array index) should be printed*/ + of the current stage(array index) should be printed */ const int stages[] = {0, 1, 1, 1, 2, 2}; diff --git a/hangman.c b/hangman.c index 0488675..eabfc95 100644 --- a/hangman.c +++ b/hangman.c @@ -1,9 +1,9 @@ #ifdef _WIN32 //For 32 and 64 bits - /* pdcurses include */ - #include + /* pdcurses include */ + #include #else - /* Linux ncurses include */ - #include + /* Linux ncurses include */ + #include #endif #include "hangman.h" //includes time.h #include //atexit @@ -15,9 +15,8 @@ #include "figures.h" - int main(int argc, char **argv) { - + const char *short_options = "w:hf:ct"; struct option long_options[] = { {"word", required_argument, NULL, 'w'}, @@ -25,17 +24,17 @@ int main(int argc, char **argv) { {"file", required_argument, NULL, 'f'}, {"credits", no_argument, NULL, 'c'}, {"troll", no_argument, NULL, 't'} - }; + }; int c, startscr = 1; /* Show startscreen by default */ char filename[255]; filename[0] = '\0'; - + /* Initialization */ initscr(); atexit(quitProgram); game_state *gs; hitfeed *hf; - + gs = malloc(sizeof(game_state)); hf = malloc(sizeof(hitfeed)); //zeroes the whole struct initCoordinates(gs); @@ -47,8 +46,8 @@ int main(int argc, char **argv) { hf->impstreakcounter = 0; keypad(stdscr, FALSE); nonl(); //No new line. Prevents the new line when hitting enter - - curs_set(0); + + curs_set(0); while ( (c = getopt_long(argc, argv, short_options, long_options, NULL)) != -1 ) { switch (c) { case 'w': @@ -80,7 +79,7 @@ int main(int argc, char **argv) { } } InitializePRNG(time(NULL)); /* Initialize random number generator */ - + if (startscr) { showStartScreen(gs); } @@ -178,8 +177,8 @@ void initAlphabet(game_state *gs) { } } gs->alphabet = malloc(sizeof(char) * hasNumber ? ALPHABET : ALPHABET_NUM); - strcpy(gs->alphabet, "abcdefghijklmnopqrstuvwxyz"); - + strcpy(gs->alphabet, "abcdefghijklmnopqrstuvwxyz"); + if (hasNumber) { strcat(gs->alphabet, "1234567890"); } @@ -187,16 +186,16 @@ void initAlphabet(game_state *gs) { void drawAlphabet(game_state *gs, char usedchar) { int start = centerDiff(gs->centerx, gs->alphabet); - int i; - if (usedchar != 0) { /* 0 is used for initialization */ - for (i = 0; i < (int)strlen(gs->alphabet); i++) { - if (gs->alphabet[i] == usedchar) { - gs->alphabet[i] = '_'; - break; - } - } - } - mvprintw(1, start, "%s", gs->alphabet); + int i; + if (usedchar != 0) { /* 0 is used for initialization */ + for (i = 0; i < (int)strlen(gs->alphabet); i++) { + if (gs->alphabet[i] == usedchar) { + gs->alphabet[i] = '_'; + break; + } + } + } + mvprintw(1, start, "%s", gs->alphabet); } void toLowerCase(char *str) { @@ -215,7 +214,7 @@ void drawGuessWord(game_state *gs) { int ycounter = 0; /* counter of the y position on sceen (row) */ int tempstartpos = startpos; /* the calculated start position to draw the characters */ int rows = 1; /* number of rows */ - + if (gs->wordLength * 2 > gs->maxx - LINEBREAK) { /* The word will be longer then the max. linesize with offset. */ rows = 2; @@ -287,13 +286,13 @@ int fillCurrentWord(game_state *gs, char validchar) { break; } } - if (!alreadyUsed) { + if (!alreadyUsed) { for (i = 0; i < gs->wordLength; i++) { if (gs->guessWord[i] == validchar) { gs->currentWord[i] = validchar; } } - drawAlphabet(gs, validchar); + drawAlphabet(gs, validchar); return 1; } else { return 0; @@ -307,7 +306,7 @@ int stackWrongCharacter(game_state *gs, char wrongchar) { alreadyUsed = 1; } } - + if (!alreadyUsed) { gs->wrongCharacters[strlen(gs->wrongCharacters)] = wrongchar; drawAlphabet(gs, wrongchar); @@ -360,16 +359,16 @@ void printGameStats(game_state *gs) { result = (float)((gs->guesses - gs->moves) / gs->moves); } else { result = (float)((gs->guesses - gs->moves) / 1); - } - - sprintf(message, "Wrong guesses: %i, right/wrong ratio: %.2f, time: %.2fsec", gs->moves, result, diff); + } + + sprintf(message, "Wrong guesses: %i, right/wrong ratio: %.2f, time: %.2fsec", gs->moves, result, diff); mvprintw(gs->centery + 1, gs->centerx - (strlen(message) / 2), message); mvprintw(gs->centery + 2, gs->centerx - (gs->wordLength / 2), gs->guessWord); } refresh(); } - + void showHelp(game_state *gs) { char *wordstring = "-w or --word: Enter the word or sentence as an argument"; char *helpstring = "-h or --help: Show this page"; @@ -390,7 +389,7 @@ void readRandomLine(char *file, char *result) { srand(time(NULL)); while (fgets(line, MAXWORDLENGTH, fp) != NULL) { count++; - if ((rand() / (float)RAND_MAX) <= (1.0 / count)) { + if ((rand() / (float)RAND_MAX) <= (1.0 / count)) { strcpy(result, line); } } @@ -403,8 +402,8 @@ void readRandomLine(char *file, char *result) { void trollHitScreen(game_state *gs, hitfeed *hf, int hits) { if (gs->trollEnabled) { char *strings[] = {"Double Hit", "Triple Hit", "Multi Hit", \ - "Ultra Hit", "Monster Hit", "Rampage", \ - "Unstoppable", "Wicked sick", "Godlike"}; + "Ultra Hit", "Monster Hit", "Rampage", \ + "Unstoppable", "Wicked sick", "Godlike"}; int selection[] = {2, 3, 4, 5, 6, 7, 8, 9, 10}; int i, found = 0; for (i = 0; i < 9; i++) { @@ -441,7 +440,7 @@ void addHitToFeed(hitfeed *hf, char *streak, int hit) { hf->besthit = hit; } } - + void printHitFeed(game_state *gs, hitfeed *hf) { if (gs->trollEnabled) { /* print the stats at the bootom right corner */ @@ -465,7 +464,7 @@ void printHitFeed(game_state *gs, hitfeed *hf) { } } } - + void trollHandleImpressive(hitfeed *hf, game_state *gs, int hits) { if (hits >= IMPRESSIVEHIT) { hf->impstreakcounter++; //Increase the counter @@ -484,38 +483,35 @@ void trollHandleImpressive(hitfeed *hf, game_state *gs, int hits) { mvprintw(5, gs->centerx - 5, " "); refresh(); usleep(100000); - } + } hf->impstreakcounter = 0; } } - - - void drawFigure(game_state *gs, int drawNext) { - int row = 6; - int length = 8; - int i; - static int state = 0; - if (!state) { - for (i = 0; i < row; i++) { - mvprintw(i, gs->maxx - length - 1, "%s", base_figure[i]); - } - state++; - } else { - /* Draw the stages */ - if (drawNext) { - mvprintw(stages[state - 1] + 1, gs->maxx - 4, "%s", figure[state - 1]); - state++; - } - } + int row = 6; + int length = 8; + int i; + static int state = 0; + if (!state) { + for (i = 0; i < row; i++) { + mvprintw(i, gs->maxx - length - 1, "%s", base_figure[i]); + } + state++; + } else { + /* Draw the stages */ + if (drawNext) { + mvprintw(stages[state - 1] + 1, gs->maxx - 4, "%s", figure[state - 1]); + state++; + } + } } void animateLineClear(game_state *gs, int line, int offsetMultiplier) { /* sleep and vanish */ int j = 0, k = gs->maxx, usec; usec = ((ANIM_DURATION * 1000000) + (100000 * offsetMultiplier)) / gs->maxx; - + for (j = 0; j <= gs->centerx; j++, k--) { mvprintw(line, j, "!"); mvprintw(line, k, "!"); @@ -525,5 +521,3 @@ void animateLineClear(game_state *gs, int line, int offsetMultiplier) { mvprintw(line, k, " "); } } - - diff --git a/hangman.h b/hangman.h index 7b03004..c26cc66 100644 --- a/hangman.h +++ b/hangman.h @@ -1,18 +1,16 @@ #include - - /* Defined macros */ -#define DEFAULTTRIES 6 +#define DEFAULTTRIES 6 #define MAXWORDLENGTH 200 #define ALPHABET 26 #define ALPHABET_NUM 36 #define ANIM_DURATION 1 #define LINEBREAK 16 //Linebreak offset -#define HITFEEDSLOTS 5 -#define IMPRESSIVESTREAK 3 //reach amount of hit streaks to activate impressive screen -#define IMPRESSIVEHIT 2 +#define HITFEEDSLOTS 5 +#define IMPRESSIVESTREAK 3 //reach amount of hit streaks to activate impressive screen +#define IMPRESSIVEHIT 2 /* Data structures */ typedef struct { @@ -51,14 +49,14 @@ void quitProgram(void); void updateScreen(game_state *gs); void initCoordinates(game_state *gs); void initGuessWord(game_state *gs, char *filename); -void initAlphabet(game_state *gs); +void initAlphabet(game_state *gs); void drawAlphabet(game_state *gs, char usedchar); void drawGuessWord(game_state *gs); int playerInput(game_state *gs, hitfeed *hf); int fillCurrentWord(game_state *gs, char validchar); int stackWrongCharacter(game_state *gs, char wrongchar); int checkWin(game_state *gs); -void printGameStats(game_state *gs); +void printGameStats(game_state *gs); void toLowerCase(char *str); void showStartScreen(game_state *gs); void startGame(game_state *gs, hitfeed *hf); diff --git a/prng.c b/prng.c index 2c9962d..c668a1a 100644 --- a/prng.c +++ b/prng.c @@ -44,7 +44,7 @@ void InitializePRNG(const uint32_t seed) index = N; } -static void Twist() +static void Twist(void) { uint32_t i, x, xA; @@ -64,7 +64,7 @@ static void Twist() } // Obtain a 32-bit random number -uint32_t ExtractU32() +uint32_t ExtractU32(void) { uint32_t y; int i = index; diff --git a/prng.h b/prng.h index 14ba35d..06e6d48 100644 --- a/prng.h +++ b/prng.h @@ -5,5 +5,5 @@ #include void InitializePRNG(const uint32_t seed); -uint32_t ExtractU32(); +uint32_t ExtractU32(void); int getrandom(int low, int high); From ca3347f21d41a7cabddabfb1a0c81aecb65873ef Mon Sep 17 00:00:00 2001 From: Daniel Eisele Date: Wed, 23 Aug 2017 21:48:29 +0200 Subject: [PATCH 02/11] Use platform-independent compiler name; remove debug symbols; add -Wextra --- makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/makefile b/makefile index 1f9bdf0..62ce6ed 100644 --- a/makefile +++ b/makefile @@ -2,8 +2,8 @@ VERSION = 1.0 -CC = gcc -CFLAGS = -Wall -g -O3 -D_REENTRANT -DVERSION=\"$(VERSION)\" +CC = cc +CFLAGS = -Wall -Wextra -O3 -D_REENTRANT -DVERSION=\"$(VERSION)\" #LDFLAGS = -lm -lpthread `gtk-config --cflags` `gtk-config --libs` -lgthread LDFLAGS = -lncurses From c1dc380ba914e9fc336002c6e02b07c1e4114c77 Mon Sep 17 00:00:00 2001 From: Daniel Eisele Date: Wed, 23 Aug 2017 22:06:49 +0200 Subject: [PATCH 03/11] Fix buffer overflow (space for '\0' character missing) --- hangman.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hangman.c b/hangman.c index eabfc95..6863801 100644 --- a/hangman.c +++ b/hangman.c @@ -176,7 +176,7 @@ void initAlphabet(game_state *gs) { break; } } - gs->alphabet = malloc(sizeof(char) * hasNumber ? ALPHABET : ALPHABET_NUM); + gs->alphabet = malloc(sizeof(char) * ((hasNumber ? ALPHABET : ALPHABET_NUM) + 1)); strcpy(gs->alphabet, "abcdefghijklmnopqrstuvwxyz"); if (hasNumber) { @@ -226,7 +226,7 @@ void drawGuessWord(game_state *gs) { } rows++; } - gs->wordRows = rows; //set the number of rows for clearing the screen later + gs->wordRows = rows; //set the number of rows for clearing the screen later tempstartpos = xcounter = gs->centerx - (gs->wordLength / rows); } for (i = startpos; i < startpos + (gs->wordLength * 2); i++) { From 76c6830b893e919ee91d7e91c35bb2c085d3a11d Mon Sep 17 00:00:00 2001 From: Daniel Eisele Date: Wed, 23 Aug 2017 22:08:49 +0200 Subject: [PATCH 04/11] Add coredump files to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 29cf209..c7f9f76 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ *.o /hangman *.md~ +*.core From 2c3376202d18ca9e1dffe664e9fd2e37b8f38aca Mon Sep 17 00:00:00 2001 From: Daniel Eisele Date: Thu, 24 Aug 2017 00:03:06 +0200 Subject: [PATCH 05/11] Removed the malloc calls to prevent memory leak at the end of the program --- hangman.c | 61 ++++++++++++++++++++++--------------------------------- hangman.h | 2 +- 2 files changed, 25 insertions(+), 38 deletions(-) diff --git a/hangman.c b/hangman.c index 6863801..6084f7d 100644 --- a/hangman.c +++ b/hangman.c @@ -27,23 +27,18 @@ int main(int argc, char **argv) { }; int c, startscr = 1; /* Show startscreen by default */ char filename[255]; + game_state gs = {0}; + hitfeed hf = {{{0}}}; // 3 braces are needed to not get a warning + filename[0] = '\0'; /* Initialization */ initscr(); atexit(quitProgram); - game_state *gs; - hitfeed *hf; - gs = malloc(sizeof(game_state)); - hf = malloc(sizeof(hitfeed)); //zeroes the whole struct - initCoordinates(gs); - gs->allowedMoves = DEFAULTTRIES; - gs->moves = 0; - gs->guesses = 0; - gs->trollEnabled = 0; - hf->besthit = 0; - hf->impstreakcounter = 0; + initCoordinates(&gs); + gs.allowedMoves = DEFAULTTRIES; + keypad(stdscr, FALSE); nonl(); //No new line. Prevents the new line when hitting enter @@ -51,12 +46,12 @@ int main(int argc, char **argv) { while ( (c = getopt_long(argc, argv, short_options, long_options, NULL)) != -1 ) { switch (c) { case 'w': - sprintf(gs->guessWord, "%s", optarg); + sprintf(gs.guessWord, "%s", optarg); startscr = 0; break; case 'h': - showHelp(gs); - exit(0); + showHelp(&gs); + return 0; break; case 'f': /* Set filename */ @@ -68,34 +63,32 @@ int main(int argc, char **argv) { break; case 't': /* troll option */ - gs->trollEnabled = 1; + gs.trollEnabled = 1; break; default: - mvprintw(gs->centery, gs->centerx - 9, "Invalid arguments."); + mvprintw(gs.centery, gs.centerx - 9, "Invalid arguments."); getch(); - showHelp(gs); - exit(0); + showHelp(&gs); + return 0; break; } } InitializePRNG(time(NULL)); /* Initialize random number generator */ if (startscr) { - showStartScreen(gs); + showStartScreen(&gs); } /* start game */ - initGuessWord(gs, filename); - initAlphabet(gs); - drawAlphabet(gs, 0); - drawFigure(gs, 0); - startGame(gs, hf); + initGuessWord(&gs, filename); + initAlphabet(&gs); + drawAlphabet(&gs, 0); + drawFigure(&gs, 0); + startGame(&gs, &hf); nl(); //enable newline again while (getch() != 10); clear(); - free(gs); - free(hf); return 0; } @@ -168,19 +161,13 @@ void initGuessWord(game_state *gs, char *filename) { } void initAlphabet(game_state *gs) { - int hasNumber = 0; - int i; - for (i = 0; i < MAXWORDLENGTH; i++) { - if (isdigit(gs->guessWord[i]) > 0) { - hasNumber = 1; - break; - } - } - gs->alphabet = malloc(sizeof(char) * ((hasNumber ? ALPHABET : ALPHABET_NUM) + 1)); strcpy(gs->alphabet, "abcdefghijklmnopqrstuvwxyz"); - if (hasNumber) { - strcat(gs->alphabet, "1234567890"); + for (unsigned int i = 0; i < strlen(gs->guessWord); i++) { + if (isdigit(gs->guessWord[i]) > 0) { + strcat(gs->alphabet, "1234567890"); + break; + } } } diff --git a/hangman.h b/hangman.h index c26cc66..d444826 100644 --- a/hangman.h +++ b/hangman.h @@ -26,7 +26,7 @@ typedef struct { char currentWord[MAXWORDLENGTH]; char wrongCharacters[DEFAULTTRIES]; int trollEnabled; - char *alphabet; + char alphabet[ALPHABET_NUM + 1]; time_t startTime; time_t endTime; int wordRows; From 271c1234e6814e1bf9dd7ed4c6ca71a02161a90d Mon Sep 17 00:00:00 2001 From: Daniel Eisele Date: Thu, 24 Aug 2017 00:06:22 +0200 Subject: [PATCH 06/11] Removed -Wextra --- makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/makefile b/makefile index 62ce6ed..294a9f4 100644 --- a/makefile +++ b/makefile @@ -3,7 +3,7 @@ VERSION = 1.0 CC = cc -CFLAGS = -Wall -Wextra -O3 -D_REENTRANT -DVERSION=\"$(VERSION)\" +CFLAGS = -Wall -O3 -D_REENTRANT -DVERSION=\"$(VERSION)\" #LDFLAGS = -lm -lpthread `gtk-config --cflags` `gtk-config --libs` -lgthread LDFLAGS = -lncurses @@ -11,7 +11,7 @@ OBJ = hangman.o prng.o all: $(OBJ) $(CC) $(CFLAGS) -o hangman $(OBJ) $(LDFLAGS) - + %.o: %.c $(CC) $(CFLAGS) -c $< From 8dac365357e6684783db78061564f0ad4fb5be54 Mon Sep 17 00:00:00 2001 From: Daniel Eisele Date: Thu, 24 Aug 2017 00:18:51 +0200 Subject: [PATCH 07/11] Fixed out ouf bounds array access --- hangman.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/hangman.c b/hangman.c index 6084f7d..eb50a68 100644 --- a/hangman.c +++ b/hangman.c @@ -287,21 +287,18 @@ int fillCurrentWord(game_state *gs, char validchar) { } int stackWrongCharacter(game_state *gs, char wrongchar) { - int i, alreadyUsed = 0; - for (i = 0; i < gs->wordLength; i++) { + for (unsigned i = 0; i < strlen(gs->wrongCharacters); i++) { if (gs->wrongCharacters[i] == wrongchar) { - alreadyUsed = 1; + return 0; // already used } } - if (!alreadyUsed) { - gs->wrongCharacters[strlen(gs->wrongCharacters)] = wrongchar; - drawAlphabet(gs, wrongchar); - drawFigure(gs, 1); - return 1; - } else { - return 0; - } + // not yet used + gs->wrongCharacters[strlen(gs->wrongCharacters)] = wrongchar; + drawAlphabet(gs, wrongchar); + drawFigure(gs, 1); + + return 1; } int checkWin(game_state *gs) { From 6ad0f0d0b8fd81e2a4d3743e48ef97cb39e05ffa Mon Sep 17 00:00:00 2001 From: Daniel Eisele Date: Thu, 24 Aug 2017 00:50:41 +0200 Subject: [PATCH 08/11] Detect if the input file cannot be opened --- hangman.c | 19 +++++++++++++------ hangman.h | 2 +- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/hangman.c b/hangman.c index eb50a68..8009e5b 100644 --- a/hangman.c +++ b/hangman.c @@ -135,14 +135,13 @@ void initGuessWord(game_state *gs, char *filename) { int i; mvprintw(0, 1, "Please enter your word: "); if (strlen(gs->guessWord) == 0) { /* Word can be set by arguments */ - if (strlen(filename) == 0) { + /* If a file argument was specified then try to read a random line from the file, + if this is not successful or if there is no file argument use the manual input. */ + if (strlen(filename) == 0 || readRandomLine(filename, gs->guessWord) != 0) { // readRandomLine is only excecuted when strlen(filename) != 0 /* Manual input */ curs_set(1); getnstr(gs->guessWord, MAXWORDLENGTH - 1); /* Reads the guessWord with a limit */ curs_set(0); - } else { - /* Random line from file */ - readRandomLine(filename, gs->guessWord); } } @@ -366,10 +365,16 @@ int centerDiff(int coordinate, char *str) { return coordinate - (len / 2); /* Integer division */ } -void readRandomLine(char *file, char *result) { - FILE *fp = fopen(file, "r"); +int readRandomLine(char *file, char *result) { + FILE *fp; int count = 0, wordlength = 0; char line[MAXWORDLENGTH]; + + fp = fopen(file, "r"); + if (fp == NULL) { + return 1; + } + srand(time(NULL)); while (fgets(line, MAXWORDLENGTH, fp) != NULL) { count++; @@ -381,6 +386,8 @@ void readRandomLine(char *file, char *result) { wordlength = strlen(result); result[wordlength - 1] = '\0'; fclose(fp); + + return 0; } void trollHitScreen(game_state *gs, hitfeed *hf, int hits) { diff --git a/hangman.h b/hangman.h index d444826..a70c3a1 100644 --- a/hangman.h +++ b/hangman.h @@ -62,7 +62,7 @@ void showStartScreen(game_state *gs); void startGame(game_state *gs, hitfeed *hf); void showHelp(game_state *gs); int centerDiff(int coordinate, char *str); -void readRandomLine(char *file, char *result); +int readRandomLine(char *file, char *result); void trollHitScreen(game_state *gs, hitfeed *hf, int hits); void addHitToFeed(hitfeed *hf, char *streak, int hit); void trollHandleImpressive(hitfeed *hf, game_state *gs, int hits); From 7970094e7394153996c9a33d8c670d42a379195d Mon Sep 17 00:00:00 2001 From: Daniel Eisele Date: Thu, 24 Aug 2017 01:38:07 +0200 Subject: [PATCH 09/11] Fixed right/wrong ratio calculation (ratio was calculated with an integer division); "max" will be printed instead of a number when 0 wrong guesses have been made; decimal places of time removed (time is only measured in seconds) --- hangman.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/hangman.c b/hangman.c index 8009e5b..f65f690 100644 --- a/hangman.c +++ b/hangman.c @@ -315,8 +315,7 @@ int checkWin(game_state *gs) { void printGameStats(game_state *gs) { //clear(); int i, z; - double diff; - diff = difftime(gs->endTime, gs->startTime); + if (!gs->trollEnabled) { for (z = 0; z <= gs->wordRows; z++) { //<= takes the last row too for (i = 0; i < gs->maxx; i++) { @@ -335,16 +334,18 @@ void printGameStats(game_state *gs) { mvprintw(gs->centery + 1, gs->centerx - (gs->wordLength / 2), gs->guessWord); } else { char message[100]; - float result = 0; + 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: ", gs->moves); if (gs->moves != 0) { - result = (float)((gs->guesses - gs->moves) / gs->moves); + sprintf(message + strlen(message), "%.2f", ((double)gs->guesses - (double)gs->moves) / (double)gs->moves); } else { - result = (float)((gs->guesses - gs->moves) / 1); + sprintf(message + strlen(message), "max"); } - sprintf(message, "Wrong guesses: %i, right/wrong ratio: %.2f, time: %.2fsec", gs->moves, result, diff); + sprintf(message + strlen(message), ", time: %llusec", (unsigned long long)(gs->endTime - gs->startTime)); mvprintw(gs->centery + 1, gs->centerx - (strlen(message) / 2), message); mvprintw(gs->centery + 2, gs->centerx - (gs->wordLength / 2), gs->guessWord); From 6b6e78d033a781091556bc42db973c8c01c88152 Mon Sep 17 00:00:00 2001 From: Daniel Eisele Date: Thu, 24 Aug 2017 01:53:46 +0200 Subject: [PATCH 10/11] Cleanup --- makefile_mingw | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/makefile_mingw b/makefile_mingw index 0437390..dc39422 100644 --- a/makefile_mingw +++ b/makefile_mingw @@ -3,7 +3,7 @@ VERSION = 1.0 CC = gcc -CFLAGS = -Wall -Wextra -O3 -D_REENTRANT -DVERSION=\"$(VERSION)\" +CFLAGS = -Wall -Wextra -O3 -D_REENTRANT -DVERSION=\"$(VERSION)\" #LDFLAGS = -lm -lpthread `gtk-config --cflags` `gtk-config --libs` -lgthread LDFLAGS = -lpdcurses @@ -11,7 +11,7 @@ OBJ = hangman.o prng.o all: $(OBJ) $(CC) $(CFLAGS) -o hangman $(OBJ) $(LDFLAGS) - + %.o: %.c $(CC) $(CFLAGS) -c $< From a2564077f93c5a37299f204347382a28990dbcaa Mon Sep 17 00:00:00 2001 From: Daniel Eisele Date: Thu, 24 Aug 2017 18:53:42 +0200 Subject: [PATCH 11/11] Clear "Best:" line before redrawing the hit feed; clear last column of the hit feed --- hangman.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/hangman.c b/hangman.c index f65f690..6ed6dea 100644 --- a/hangman.c +++ b/hangman.c @@ -393,8 +393,8 @@ int readRandomLine(char *file, char *result) { void trollHitScreen(game_state *gs, hitfeed *hf, int hits) { if (gs->trollEnabled) { - char *strings[] = {"Double Hit", "Triple Hit", "Multi Hit", \ - "Ultra Hit", "Monster Hit", "Rampage", \ + char *strings[] = {"Double Hit", "Triple Hit", "Multi Hit", + "Ultra Hit", "Monster Hit", "Rampage", "Unstoppable", "Wicked sick", "Godlike"}; int selection[] = {2, 3, 4, 5, 6, 7, 8, 9, 10}; int i, found = 0; @@ -439,8 +439,8 @@ void printHitFeed(game_state *gs, hitfeed *hf) { int newMaxy = gs->maxy - HITFEEDSLOTS; int newMaxx = gs->maxx - (gs->centerx / 2); int i, z; - for (i = 0; i < HITFEEDSLOTS; i++) { - for (z = 0; z < gs->maxx - newMaxx; z++) { + for (i = -1; i < HITFEEDSLOTS; i++) { + for (z = 0; z <= gs->maxx - newMaxx; z++) { mvprintw(newMaxy + i, newMaxx + z, " "); } }