From 7c11e14c94e402ded0ee5c207eb918c2d2487773 Mon Sep 17 00:00:00 2001 From: Aidan Haas <94150901+ahaas25@users.noreply.github.com> Date: Wed, 9 Nov 2022 18:23:54 -0500 Subject: [PATCH] Fixed random function, add words to UI --- debugger.c | 2 ++ typing_test.c | 67 ++++++++++++++++++++++++++++++++++----------------- utilities.c | 17 ++++++++++--- utilities.h | 1 + 4 files changed, 62 insertions(+), 25 deletions(-) diff --git a/debugger.c b/debugger.c index b9dfcbb..402d72b 100644 --- a/debugger.c +++ b/debugger.c @@ -3,9 +3,11 @@ #include #include #include +#include #include "utilities.h" #include "typing_test.h" + int main() { FILE *words_file; words_file = fopen("words.txt", "r"); diff --git a/typing_test.c b/typing_test.c index 6dde791..1ffc8eb 100644 --- a/typing_test.c +++ b/typing_test.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "typing_test.h" #include "utilities.h" @@ -45,19 +46,11 @@ void print_centered_text_menu(WINDOW *win, int row, int target, char str[][MAX_S } void typing_ui(WINDOW *win, int level, int mode, Word_array *word_array) { - int run = 1, ch, i, words, win_x = win->_maxx; + int run = 1, ch, i, words, win_x = win->_maxx, new_test = 1; char str[1024]; + Word_array *prompt = NULL; - clear(); - if (mode == 0) { - strcat(str, "Timed Test - "); - strcat(str, TIMED_MODES_STRING[level]); - print_centered_text(stdscr, 0, str); - } else { - strcat(str, "Word Test - "); - strcat(str, WORD_MODES_STRING[level]); - print_centered_text(stdscr, 0, str); - } + printw("\nPrompt: \n"); for (i = 0; i < words; i++) { /* Will print the words centered in the screen */ @@ -68,22 +61,52 @@ void typing_ui(WINDOW *win, int level, int mode, Word_array *word_array) { /* For now prints placeholder text for the purposes of testing */ - print_centered_text(win, 3, "The quick brown fox jumps over the lazy dog"); - print_centered_text(win, 4, "Line 2"); - print_centered_text(win, 5, "Line 3"); - - printw("%d", win_x); - - move(7, (win_x / 8)); - + /* Make "Print words function for simplicity" */ while (run) { + if (new_test == 1) { + clear(); + + str[0] = '\0'; + + if (mode == 0) { + strcat(str, "Timed Test - "); + strcat(str, TIMED_MODES_STRING[level]); + print_centered_text(stdscr, 0, str); + } else { + strcat(str, "Word Test - "); + strcat(str, WORD_MODES_STRING[level]); + print_centered_text(stdscr, 0, str); + } + + printw("\nPrompt:\n"); /* Temp */ + + if (prompt == NULL) { + prompt = malloc(sizeof(Word_array)); + } else { + clear_word_array(prompt); + } + + generate_words(10, word_array, prompt); + + new_test = 0; + for (i = 0; i < prompt->number_of_words; i++) { + printw("%d: %s ", i, prompt->words[i].text); + } + } + ch = getch(); - printw("%c", ch); + if (isalpha(ch)) { + printw("%c", ch); + } + if (ch == ' ') { printw("RESET TEST"); + new_test = 1; } } + + free(prompt); } /* Main function. Creates main menu */ @@ -93,6 +116,7 @@ int main() { int cursor_x = 0, cursor_y = 0, run = 1; int ch, key; + word_array = malloc(sizeof(Word_array)); initscr(); cbreak(); keypad(stdscr, TRUE); @@ -175,14 +199,13 @@ int main() { if (cursor_x == 0 && cursor_y == 2) { run = 0; } else if (cursor_y == 1 || cursor_y == 0) { - typing_ui(stdscr, cursor_x, cursor_y, &word_array); + typing_ui(stdscr, cursor_x, cursor_y, word_array); } } } /* Exiting */ - free(word_array); refresh(); endwin(); diff --git a/utilities.c b/utilities.c index 119af9e..268c215 100644 --- a/utilities.c +++ b/utilities.c @@ -1,9 +1,20 @@ #include "utilities.h" -#include +#include + +/* Clears word array given in parameters */ +void clear_word_array(Word_array *array) { + int i; + for (i = 0; i < array->number_of_words; i++) { + free(array->words[i].text); + } + array->number_of_words = 0; +} int generate_random(int lower, int upper, int c) { - time_t x; - srand(x); + struct timeval te; + gettimeofday(&te, NULL); + long long milliseconds = te.tv_sec * 1000LL + te.tv_usec / 1000; + srand(milliseconds + c); return (rand() % (upper - lower + 1)) + lower; } diff --git a/utilities.h b/utilities.h index 917aaf7..db55d59 100644 --- a/utilities.h +++ b/utilities.h @@ -15,5 +15,6 @@ typedef struct { Word *words; } Word_array; +void clear_word_array(Word_array *array); int generate_words(int num_words, Word_array *words, Word_array *to_return); int parse_words_file(FILE *words_file, Word_array *words); \ No newline at end of file