From 309d55a064217c7455b199ca37eba5c98807ad56 Mon Sep 17 00:00:00 2001 From: Aidan Haas <94150901+ahaas25@users.noreply.github.com> Date: Fri, 25 Nov 2022 18:02:03 -0500 Subject: [PATCH] Implement calculation of WPM and test time --- typing_test.c | 33 ++++++++++++++++++++++----------- typing_test.h | 13 +++++++++++-- utilities.h | 1 + 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/typing_test.c b/typing_test.c index 59ece42..d0a2818 100644 --- a/typing_test.c +++ b/typing_test.c @@ -1,9 +1,3 @@ -#include -#include -#include -#include -#include -#include #include "typing_test.h" #include "utilities.h" @@ -78,12 +72,13 @@ void print_typing_prompt(WINDOW *win, Word_array *prompt, char *prompt_string, } void typing_ui(WINDOW *win, int level, int mode, Word_array *word_array) { - int run = 1, ch, i, new_test = 1, user_input_length; + int run = 1, ch, i, new_test = 1, user_input_length, start_timer; + double test_time, wpm; char str[1024], *user_input = NULL; char *prompt_string = NULL; /* Full prompt string */ Word_array *prompt = NULL; Word *prompt_lines; /* Array of lines for displaying */ - + struct timeval timer_start, timer_stop; while (run) { if (new_test == 1) { @@ -136,33 +131,50 @@ void typing_ui(WINDOW *win, int level, int mode, Word_array *word_array) { printf("%s", user_input); print_typing_prompt(win, prompt, prompt_string, user_input); + start_timer = 1; /* Flag timer as ready to be started */ } ch = getch(); if (isalnum(ch) || ch == ' ') { /* Ensures user does not type outside of character limit */ + if (start_timer) { + gettimeofday(&timer_start, NULL); + start_timer = 0; + } if (user_input_length < prompt->num_characters) { user_input[user_input_length] = ch; user_input_length++; user_input[user_input_length] = '\0'; move(0, 0); - printw("Size: %d", strlen(user_input)); } } else if (ch == KEY_BACKSPACE) { if (user_input_length > 0) { user_input_length--; user_input[user_input_length] = '\0'; move(0, 0); - printw("Size: %d", strlen(user_input)); } } else if (ch == ' ') { new_test = 1; + start_timer = 1; } user_input_length = strlen(user_input); if (user_input_length == strlen(prompt_string) && user_input[user_input_length - 1] == prompt_string[user_input_length - 1]) { + gettimeofday(&timer_stop, NULL); + /* Convert time stamps to miliseconds */ + long long time_start = timer_start.tv_sec * 1000LL + timer_start.tv_usec / 1000; + long long time_stop = timer_stop.tv_sec * 1000LL + timer_stop.tv_usec / 1000; + + /* Subtract starting time from ending time to get elapsed time */ + test_time = time_stop - time_start; + test_time /= 1000; /* Convert time into seconds */ + + /* WPM = (Total Chars / 5) /(Total Mins) */ + wpm = ((double) user_input_length / 5) / (test_time / 60); clear(); move(0, 0); + str[0] = '\0'; /* Reset str */ + printw("Time: %f WPM: %f", test_time, wpm); print_centered_text(win, 0, "Test Complete!"); print_centered_text(win, 2, "WPM: "); print_centered_text(win, 3, "Accuracy: "); @@ -276,7 +288,6 @@ int main() { default: break; } - printw("%d %d", cursor_x, cursor_y); if (ch == '\n') { if (cursor_x == 0 && cursor_y == 2) { run = 0; diff --git a/typing_test.h b/typing_test.h index fb976c0..8f72b24 100644 --- a/typing_test.h +++ b/typing_test.h @@ -1,3 +1,11 @@ +#include +#include +#include +#include +#include +#include +#include + #define NAME "Typing Test" #define MAX_X 5 #define MAX_Y 3 @@ -6,8 +14,9 @@ #define NUM_MISC 3 #define SUCCESS 1 #define FAILURE 0 + char TIMED_MODES_STRING[5][MAX_STRING] = { "5s", "10s", "25s", "30s", "60s" }; int TIMED_MODES[5] = {5, 10, 25, 30, 60}; -char WORD_MODES_STRING[5][MAX_STRING] = { "10", "25", "50", "100", "200" }; -int WORD_MODES[5] = {10, 25, 50, 100, 200}; +char WORD_MODES_STRING[5][MAX_STRING] = { "5", "10", "25", "50", "100" }; +int WORD_MODES[5] = {5, 10, 25, 50, 100}; char MISC_STRING[3][MAX_STRING] = {"Exit", "Stats", "Settings"}; \ No newline at end of file diff --git a/utilities.h b/utilities.h index 2f08c0d..45cc91c 100644 --- a/utilities.h +++ b/utilities.h @@ -1,6 +1,7 @@ #include #include #include +#include #define SUCCESS 1 #define FAILURE 0