Implemented parse_words_file and generate_random
Program now ready for UI typing test to be implemented. Updated Makefile to reflect changes in project. Created debugger.c to more easily work on utilities.
This commit is contained in:
parent
66cd9d46d6
commit
9aef889343
4
Makefile
4
Makefile
@ -7,8 +7,8 @@ LDLIBS = -lncurses
|
||||
all: typing_test debugger
|
||||
|
||||
# Create executables
|
||||
typing_test: typing_test.o string_helpers.o
|
||||
$(CC) -o typing_test typing_test.o string_helpers.o $(LDLIBS)
|
||||
typing_test: typing_test.o utilities.o
|
||||
$(CC) -o typing_test typing_test.o utilities.o $(LDLIBS)
|
||||
|
||||
debugger: debugger.o utilities.o
|
||||
$(CC) -o debugger debugger.o utilities.o
|
||||
|
26
debugger.c
26
debugger.c
@ -7,9 +7,25 @@
|
||||
#include "typing_test.h"
|
||||
|
||||
int main() {
|
||||
FILE *words;
|
||||
|
||||
words = fopen("words.txt", "r");
|
||||
Word_array word_array;
|
||||
parse_words_file(words, &word_array);
|
||||
FILE *words_file;
|
||||
words_file = fopen("words.txt", "r");
|
||||
Word_array word_array, return_words;
|
||||
int i, number_of_words;
|
||||
/* Test parse words file and generate words */
|
||||
parse_words_file(words_file, &word_array);
|
||||
number_of_words = word_array.number_of_words;
|
||||
for (i = 0; i < number_of_words; i++) {
|
||||
printf("Word List: %s %d\n", word_array.words[i].text, word_array.words[i].length);
|
||||
}
|
||||
|
||||
printf("Number of words in list: %d\n", word_array.number_of_words);
|
||||
|
||||
int words_to_generate = 10;
|
||||
printf("\nGenerating %d Words\n", words_to_generate);
|
||||
|
||||
generate_words(words_to_generate, &word_array, &return_words);
|
||||
for (i = 0; i < words_to_generate; i++) {
|
||||
printf("Generated Word: %s %d\n", return_words.words[i].text, return_words.words[i].length);
|
||||
}
|
||||
|
||||
}
|
@ -44,7 +44,7 @@ void print_centered_text_menu(WINDOW *win, int row, int target, char str[][MAX_S
|
||||
}
|
||||
}
|
||||
|
||||
void typing_ui(WINDOW *win, int level, int mode, FILE *word_file) {
|
||||
void typing_ui(WINDOW *win, int level, int mode, Word_array *word_array) {
|
||||
int run = 1, ch, i, words, win_x = win->_maxx;
|
||||
char str[1024];
|
||||
|
||||
@ -88,7 +88,8 @@ void typing_ui(WINDOW *win, int level, int mode, FILE *word_file) {
|
||||
|
||||
/* Main function. Creates main menu */
|
||||
int main() {
|
||||
FILE *words;
|
||||
FILE *words_file;
|
||||
Word_array *word_array;
|
||||
int cursor_x = 0, cursor_y = 0, run = 1;
|
||||
int ch, key;
|
||||
|
||||
@ -97,7 +98,7 @@ int main() {
|
||||
keypad(stdscr, TRUE);
|
||||
noecho();
|
||||
|
||||
words = fopen("words.txt", "r");
|
||||
words_file = fopen("words.txt", "r");
|
||||
|
||||
if (has_colors() == FALSE) {
|
||||
print_centered_text(stdscr, 4, "Your terminal does not support color");
|
||||
@ -107,7 +108,7 @@ int main() {
|
||||
start_color();
|
||||
}
|
||||
|
||||
if (words == NULL) {
|
||||
if (words_file == NULL) {
|
||||
run = 0;
|
||||
|
||||
init_color(COLOR_WHITE, 255, 255, 0);
|
||||
@ -118,6 +119,8 @@ int main() {
|
||||
print_centered_text(stdscr, 6, "Press any key to exit.\n");
|
||||
attroff(COLOR_PAIR(0));
|
||||
ch = getch();
|
||||
} else {
|
||||
parse_words_file(words_file, word_array);
|
||||
}
|
||||
|
||||
while (run) {
|
||||
@ -172,12 +175,15 @@ 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, words);
|
||||
typing_ui(stdscr, cursor_x, cursor_y, &word_array);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* Exiting */
|
||||
free(word_array);
|
||||
|
||||
refresh();
|
||||
endwin();
|
||||
|
||||
|
41
utilities.c
41
utilities.c
@ -1,14 +1,27 @@
|
||||
#include "utilities.h"
|
||||
#include <time.h>
|
||||
|
||||
int generate_random(int lower, int upper, int c) {
|
||||
time_t x;
|
||||
srand(x);
|
||||
return (rand() % (upper - lower + 1)) + lower;
|
||||
}
|
||||
|
||||
/* This will go through the word_array structure and find the requested
|
||||
amount of words */
|
||||
int generate_words(int num_words, Word_array *words, char *to_return) {
|
||||
int i, k, number_words_in_txt;
|
||||
int generate_words(int number_of_words, Word_array *words, Word_array *to_return) {
|
||||
int i, k;
|
||||
|
||||
to_return = malloc(num_words * 64); /* Each word will be less than 64 chars */
|
||||
/* Remember to free me after the test ends! */
|
||||
|
||||
for (i = 0; i < words->number_of_words; i++) {
|
||||
to_return->number_of_words = number_of_words;
|
||||
to_return->words = malloc(sizeof(Word) * number_of_words);
|
||||
|
||||
for (i = 0; i < number_of_words; i++) {
|
||||
k = generate_random(0, words->number_of_words, i);
|
||||
to_return->words[i].length = words->words[k].length;
|
||||
to_return->words[i].text = malloc(words->words[k].length);
|
||||
strcpy(to_return->words[i].text, words->words[k].text);
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
@ -18,15 +31,29 @@ int generate_words(int num_words, Word_array *words, char *to_return) {
|
||||
/* This will dynamically allocate memory to a 2d char array of all the words
|
||||
in words.txt */
|
||||
int parse_words_file(FILE *words_file, Word_array *words) {
|
||||
int i, number_of_words = 0;
|
||||
char buf[256];
|
||||
int i = 0, number_of_words = 0;
|
||||
char buf[256], temp[256];
|
||||
|
||||
/* Counts number of lines in words file (Each line contains a unique word) */
|
||||
while (fgets(buf, sizeof(buf), words_file) != NULL) {
|
||||
number_of_words++;
|
||||
}
|
||||
|
||||
printf("Number of words: %d\n", number_of_words);
|
||||
words->number_of_words = number_of_words;
|
||||
/* Allocates space for 'number_of_words' Words structures */
|
||||
words->words = malloc(sizeof(Word) * number_of_words);
|
||||
|
||||
rewind(words_file);
|
||||
|
||||
while (fgets(buf, sizeof(buf), words_file) != NULL) {
|
||||
sscanf(buf, "%s", temp);
|
||||
words->words[i].length = strlen(temp);
|
||||
words->words[i].text = malloc(strlen(temp));
|
||||
strcpy(words->words[i].text, temp);
|
||||
i++;
|
||||
}
|
||||
|
||||
fclose(words_file);
|
||||
|
||||
return SUCCESS;
|
||||
}
|
@ -5,10 +5,15 @@
|
||||
#define SUCCESS 1
|
||||
#define FAILURE 0
|
||||
|
||||
typedef struct {
|
||||
int length;
|
||||
char *text;
|
||||
} Word;
|
||||
|
||||
typedef struct {
|
||||
int number_of_words;
|
||||
char words[];
|
||||
Word *words;
|
||||
} Word_array;
|
||||
|
||||
int generate_words(int num_words, Word_array *words, char *to_return);
|
||||
int generate_words(int num_words, Word_array *words, Word_array *to_return);
|
||||
int parse_words_file(FILE *words_file, Word_array *words);
|
Loading…
x
Reference in New Issue
Block a user