Start and Endtime; Timediff on game end #15
This commit is contained in:
13
hangman.c
13
hangman.c
@@ -5,20 +5,17 @@
|
|||||||
/* Linux ncurses include */
|
/* Linux ncurses include */
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
#endif
|
#endif
|
||||||
#include "hangman.h"
|
#include "hangman.h" //includes time.h
|
||||||
#include <stdlib.h> //atexit
|
#include <stdlib.h> //atexit
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h> //tolower
|
#include <ctype.h> //tolower
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
#include <time.h> //time(null)
|
|
||||||
#include "prng.h"
|
#include "prng.h"
|
||||||
#include <unistd.h> //usleep
|
#include <unistd.h> //usleep
|
||||||
#include "figures.h"
|
#include "figures.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
|
|
||||||
const char *short_options = "w:hf:ct";
|
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 */
|
int c, startscr = 1; /* Show startscreen by default */
|
||||||
char filename[255];
|
char filename[255];
|
||||||
filename[0] = '\0';
|
filename[0] = '\0';
|
||||||
//while ( (c = getopt_long(argc, argv, short_options, long_options, NULL)) != -1 )
|
|
||||||
|
|
||||||
/* Initialization */
|
/* Initialization */
|
||||||
initscr();
|
initscr();
|
||||||
@@ -95,6 +91,7 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
void startGame(game_state *gs) {
|
void startGame(game_state *gs) {
|
||||||
noecho(); /* Don't show the player input */
|
noecho(); /* Don't show the player input */
|
||||||
|
time(&gs->startTime);
|
||||||
while (checkWin(gs)) {
|
while (checkWin(gs)) {
|
||||||
updateScreen(gs);
|
updateScreen(gs);
|
||||||
playerInput(gs);
|
playerInput(gs);
|
||||||
@@ -317,6 +314,7 @@ int checkWin(game_state *gs) {
|
|||||||
return 1;
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
/* game end: decide if game is won or lost */
|
/* game end: decide if game is won or lost */
|
||||||
|
time(&gs->endTime);
|
||||||
printGameStats(gs);
|
printGameStats(gs);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -325,6 +323,9 @@ int checkWin(game_state *gs) {
|
|||||||
void printGameStats(game_state *gs) {
|
void printGameStats(game_state *gs) {
|
||||||
//clear();
|
//clear();
|
||||||
int i;
|
int i;
|
||||||
|
double diff;
|
||||||
|
diff = difftime(gs->endTime, gs->startTime);
|
||||||
|
|
||||||
for (i = 0; i < gs->maxx; i++) {
|
for (i = 0; i < gs->maxx; i++) {
|
||||||
mvprintw(gs->centery, i, " ");
|
mvprintw(gs->centery, i, " ");
|
||||||
mvprintw(gs->centery + 1, i, " ");
|
mvprintw(gs->centery + 1, i, " ");
|
||||||
@@ -343,7 +344,7 @@ void printGameStats(game_state *gs) {
|
|||||||
result = (float)((gs->guesses - gs->moves) / 1);
|
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);
|
mvprintw(gs->centery + 1, gs->centerx - (strlen(message) / 2), message);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -1,3 +1,8 @@
|
|||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* Defined macros */
|
/* Defined macros */
|
||||||
#define DEFAULTTRIES 6
|
#define DEFAULTTRIES 6
|
||||||
#define MAXWORDLENGTH 100
|
#define MAXWORDLENGTH 100
|
||||||
@@ -21,6 +26,8 @@ typedef struct {
|
|||||||
char wrongCharacters[DEFAULTTRIES];
|
char wrongCharacters[DEFAULTTRIES];
|
||||||
int trollEnabled;
|
int trollEnabled;
|
||||||
char *alphabet;
|
char *alphabet;
|
||||||
|
time_t startTime;
|
||||||
|
time_t endTime;
|
||||||
} game_state;
|
} game_state;
|
||||||
|
|
||||||
typedef struct word {
|
typedef struct word {
|
||||||
|
Reference in New Issue
Block a user