Fixed random function, add words to UI

This commit is contained in:
Aidan Haas 2022-11-09 18:23:54 -05:00 committed by GitHub
parent a724c75e80
commit 7c11e14c94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 62 additions and 25 deletions

View File

@ -3,9 +3,11 @@
#include <stdlib.h>
#include <string.h>
#include <ncurses.h>
#include <sys/time.h>
#include "utilities.h"
#include "typing_test.h"
int main() {
FILE *words_file;
words_file = fopen("words.txt", "r");

View File

@ -3,6 +3,7 @@
#include <stdlib.h>
#include <string.h>
#include <ncurses.h>
#include <ctype.h>
#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();
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();

View File

@ -1,9 +1,20 @@
#include "utilities.h"
#include <time.h>
#include <sys/time.h>
/* 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;
}

View File

@ -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);