Milestone 2! Polished code, add settings menu

This commit is contained in:
Aidan Haas 2023-01-05 12:04:51 -05:00 committed by GitHub
parent b737ac8375
commit 6ce73ab765
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 89 additions and 33 deletions

View File

@ -4,6 +4,7 @@
#include <string.h> #include <string.h>
#include <ncurses.h> #include <ncurses.h>
#include <sys/time.h> #include <sys/time.h>
#include <time.h>
#include "utilities.h" #include "utilities.h"
#include "typing_test.h" #include "typing_test.h"
@ -31,15 +32,14 @@ void debug_words() {
} }
void debug_stats() { void debug_stats() {
int i;
FILE *stats_file; FILE *stats_file;
Stat_struct x; Stat_struct x;
char buf[256], temp[256];
int temp_stat;
stats_file = fopen("stats", "r"); stats_file = fopen("stats", "r");
load_stats(stats_file, &x); load_stats(stats_file, &x);
for (int i = 0; i < 9; i++) { for (i = 0; i < 9; i++) {
printf("%d\n", x.data[i]); printf("%d\n", x.data[i]);
} }
@ -47,5 +47,6 @@ void debug_stats() {
int main() { int main() {
debug_stats(); debug_stats();
return 0;
} }

View File

@ -8,11 +8,6 @@ void print_centered_text(WINDOW *win, int row, char *str) {
mvwprintw(win, row, center_col, str); mvwprintw(win, row, center_col, str);
} }
/* To do */
/* Track misinputs? */
/* Fix stats screen on timed mode */
/* Polish timed mode code */
/* Prints centered text for menu items */ /* Prints centered text for menu items */
void print_centered_text_menu(WINDOW *win, int row, int target, char str[][MAX_STRING], void print_centered_text_menu(WINDOW *win, int row, int target, char str[][MAX_STRING],
int highlight, int elements) { int highlight, int elements) {
@ -43,6 +38,20 @@ void print_centered_text_menu(WINDOW *win, int row, int target, char str[][MAX_S
} }
} }
/* Highlights text if cursor points to the col */
void print_centered_text_menu_single(WINDOW *win, int row, int target, char str[]) {
int text_length = strlen(str), cursor;
cursor = (win->_maxx - text_length) / 2;
move(row, cursor);
if (target == row) {
attron(A_STANDOUT);
}
printw(str);
attroff(A_STANDOUT);
}
/* Prints the typing prompt onto the terminal. /* Prints the typing prompt onto the terminal.
Automatically centers, wraps, and scrolls through text */ Automatically centers, wraps, and scrolls through text */
void print_typing_prompt(WINDOW *win, Word_array *prompt, char *prompt_string, void print_typing_prompt(WINDOW *win, Word_array *prompt, char *prompt_string,
@ -120,7 +129,6 @@ void typing_ui(WINDOW *win, int level, int mode, Word_array *word_array, Stat_st
generate_words(WORD_MODES[level], word_array, prompt); generate_words(WORD_MODES[level], word_array, prompt);
} }
if (user_input != NULL) { if (user_input != NULL) {
free(user_input); free(user_input);
} }
@ -385,25 +393,70 @@ void typing_ui(WINDOW *win, int level, int mode, Word_array *word_array, Stat_st
/* Draws settings UI to console */ /* Draws settings UI to console */
/* Placeholder, will work on next */ /* Placeholder, will work on next */
void settings_ui(WINDOW *win) { void settings_ui(WINDOW *win, Stat_struct *stats) {
char ch; FILE *stats_file;
int selection = 13, row = 0, count = 2, run = 1;
int selections[] = { 8, 11, 13 };
int ch;
while (run) {
clear(); clear();
print_centered_text(win, 0, "Settings"); row = 0;
print_centered_text(win, row, "Settings");
print_centered_text(win, 3, "Controls"); row += 3;
print_centered_text(win, 4, "Tab - Reset Test"); print_centered_text(win, row++, "Controls");
print_centered_text(win, 5, "Esc - End Test"); print_centered_text(win, row++, "Tab - Reset Test");
print_centered_text(win, row, "Esc - End Test");
print_centered_text(win, 7, "Themes"); row += 2;
print_centered_text(win, 8, "Default");
print_centered_text(win, 10, "Statistics"); print_centered_text(win, row++, "Themes");
print_centered_text(win, 11, "Reset Stats"); print_centered_text_menu_single(win, row, selection, "Default");
print_centered_text(win, 13, "Return to Menu"); row += 2;
print_centered_text(win, row++, "Statistics");
print_centered_text_menu_single(win, row, selection, "Reset Stats");
row += 2;
print_centered_text_menu_single(win, row, selection, "Return to Menu");
refresh(); refresh();
ch = getchar(); ch = getch();
switch (ch) {
case KEY_UP:
if (count == 0) {
count = 2;
} else {
count--;
}
break;
case KEY_DOWN:
if (count < 2) {
count++;
} else {
count = 0;
}
break;
case '\n':
if (count == 2) { /* Exit menu */
run = 0;
} else if (count == 1) { /* Clear stats */
stats_file = fopen(STATS_FILEPATH, "w");
create_stats_file(stats_file);
stats_file = fopen(STATS_FILEPATH, "r");
load_stats(stats_file, stats);
}
}
selection = selections[count];
move(0, 0);
printw("The count is: %d", count);
refresh();
}
clear(); clear();
} }
@ -428,7 +481,7 @@ void stat_ui(WINDOW *win, Stat_struct *stats) {
temp_str[0] = '\0'; temp_str[0] = '\0';
append_line("Average WPM: ", temp_str); append_line("Average WPM: ", temp_str);
temp = (((double) stats->data[CHARS_CORRECT]) / 5) / (((double) stats->data[TIME_TYPED]) / 600); temp = (((double)stats->data[CHARS_CORRECT]) / 5) / (((double)stats->data[TIME_TYPED]) / 600);
gcvt(temp, 5, temp_num); gcvt(temp, 5, temp_num);
append_line(temp_num, temp_str); append_line(temp_num, temp_str);
print_centered_text(win, row, temp_str); print_centered_text(win, row, temp_str);
@ -459,7 +512,7 @@ void stat_ui(WINDOW *win, Stat_struct *stats) {
temp_str[0] = '\0'; temp_str[0] = '\0';
append_line("Time Spent Typing: ", temp_str); append_line("Time Spent Typing: ", temp_str);
gcvt((double) stats->data[TIME_TYPED]/ 600, 5, temp_num); gcvt((double)stats->data[TIME_TYPED] / 600, 5, temp_num);
append_line(temp_num, temp_str); append_line(temp_num, temp_str);
append_line(" min", temp_str); append_line(" min", temp_str);
print_centered_text(win, row, temp_str); print_centered_text(win, row, temp_str);
@ -505,6 +558,7 @@ void stat_ui(WINDOW *win, Stat_struct *stats) {
refresh(); refresh();
ch = getchar(); ch = getchar();
clear(); clear();
} }
@ -523,8 +577,8 @@ int main() {
noecho(); noecho();
print_centered_text(stdscr, 4, "Loading..."); print_centered_text(stdscr, 4, "Loading...");
words_file = fopen("words.txt", "r"); words_file = fopen(WORDS_FILEPATH, "r");
stats_file = fopen("stats", "r"); stats_file = fopen(STATS_FILEPATH, "r");
if (has_colors() == FALSE) { if (has_colors() == FALSE) {
print_centered_text(stdscr, 4, "Your terminal does not support color"); print_centered_text(stdscr, 4, "Your terminal does not support color");
@ -554,9 +608,9 @@ int main() {
/* Creates a new stats file if one is not detected */ /* Creates a new stats file if one is not detected */
if (stats_file == NULL) { if (stats_file == NULL) {
stats_file = fopen("stats", "w"); stats_file = fopen(STATS_FILEPATH, "w");
create_stats_file(stats_file); create_stats_file(stats_file);
stats_file = fopen("stats", "r"); stats_file = fopen(STATS_FILEPATH, "r");
} }
/* Load stats from stats file */ /* Load stats from stats file */
@ -621,7 +675,7 @@ int main() {
stat_ui(stdscr, &stats); stat_ui(stdscr, &stats);
} else if (cursor_x == 2 && cursor_y == 2) { } else if (cursor_x == 2 && cursor_y == 2) {
/* Settings */ /* Settings */
settings_ui(stdscr); settings_ui(stdscr, &stats);
} else if (cursor_y == 1 || cursor_y == 0) { } else if (cursor_y == 1 || cursor_y == 0) {
typing_ui(stdscr, cursor_x, cursor_y, word_array, &stats); typing_ui(stdscr, cursor_x, cursor_y, word_array, &stats);
clear(); clear();
@ -636,7 +690,7 @@ int main() {
/* Saves stats and closes file */ /* Saves stats and closes file */
/* Open stats first */ /* Open stats first */
stats_file = fopen("stats", "w"); stats_file = fopen(STATS_FILEPATH, "w");
save_stats(stats_file, &stats); save_stats(stats_file, &stats);
return 0; return 0;

View File

@ -14,6 +14,8 @@
#define NUM_MISC 3 #define NUM_MISC 3
#define SUCCESS 1 #define SUCCESS 1
#define FAILURE 0 #define FAILURE 0
#define WORDS_FILEPATH "words.txt"
#define STATS_FILEPATH "stats"
char TIMED_MODES_STRING[5][MAX_STRING] = { "5s", "10s", "25s", "30s", "60s" }; char TIMED_MODES_STRING[5][MAX_STRING] = { "5s", "10s", "25s", "30s", "60s" };
int TIMED_MODES[5] = {5, 10, 25, 30, 60}; int TIMED_MODES[5] = {5, 10, 25, 30, 60};

View File

@ -127,7 +127,6 @@ int update_max_wpm(Stat_struct *stats) {
return SUCCESS; return SUCCESS;
} }
/* Appends first parameter to the end of end of the second parameter /* Appends first parameter to the end of end of the second parameter
(Does not change size of the target, assumes there is space) */ (Does not change size of the target, assumes there is space) */
void append_line(char *source, char *target) { void append_line(char *source, char *target) {