Start and Endtime; Timediff on game end #15

This commit is contained in:
2017-05-07 14:38:14 +02:00
parent 98d42c4f6a
commit 4b0c611a60
2 changed files with 14 additions and 6 deletions

View File

@@ -5,20 +5,17 @@
/* Linux ncurses include */
#include <ncurses.h>
#endif
#include "hangman.h"
#include "hangman.h" //includes time.h
#include <stdlib.h> //atexit
#include <string.h>
#include <ctype.h> //tolower
#include <getopt.h>
#include <time.h> //time(null)
#include "prng.h"
#include <unistd.h> //usleep
#include "figures.h"
int main(int argc, char **argv) {
const char *short_options = "w:hf:ct";
@@ -32,7 +29,6 @@ int main(int argc, char **argv) {
int c, startscr = 1; /* Show startscreen by default */
char filename[255];
filename[0] = '\0';
//while ( (c = getopt_long(argc, argv, short_options, long_options, NULL)) != -1 )
/* Initialization */
initscr();
@@ -95,6 +91,7 @@ int main(int argc, char **argv) {
void startGame(game_state *gs) {
noecho(); /* Don't show the player input */
time(&gs->startTime);
while (checkWin(gs)) {
updateScreen(gs);
playerInput(gs);
@@ -317,6 +314,7 @@ int checkWin(game_state *gs) {
return 1;
} else {
/* game end: decide if game is won or lost */
time(&gs->endTime);
printGameStats(gs);
return 0;
}
@@ -325,6 +323,9 @@ int checkWin(game_state *gs) {
void printGameStats(game_state *gs) {
//clear();
int i;
double diff;
diff = difftime(gs->endTime, gs->startTime);
for (i = 0; i < gs->maxx; i++) {
mvprintw(gs->centery, i, " ");
mvprintw(gs->centery + 1, i, " ");
@@ -343,7 +344,7 @@ void printGameStats(game_state *gs) {
result = (float)((gs->guesses - gs->moves) / 1);
}
sprintf(message, "Wrong guesses: %i, right/wrong ratio: %.2f", gs->moves, result);
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);
}