From be3e90c0eda0ff2012a72a27f69a8bde37dd471c Mon Sep 17 00:00:00 2001 From: "David Vereb (Home)" Date: Sun, 31 Mar 2019 17:40:37 -0500 Subject: [PATCH] Initial Commit. WIP! --- .gitignore | 2 + Makefile | 2 + Problem.cpp | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Problem.h | 43 ++++++++++++++++++ main.cpp | 77 +++++++++++++++++++++++++++++++++ 5 files changed, 246 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 Problem.cpp create mode 100644 Problem.h create mode 100644 main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cd633b2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*~ +mathprac \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c74f605 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +mathprac: main.cpp Problem.cpp + clang++ -o mathprac -std=c++11 -g main.cpp Problem.cpp -lncurses diff --git a/Problem.cpp b/Problem.cpp new file mode 100644 index 0000000..62e6055 --- /dev/null +++ b/Problem.cpp @@ -0,0 +1,122 @@ +#include "Problem.h" + +#include + +bool Problem::seeded = false; + +Problem::Problem(PROBLEM_TYPE type, unsigned short max_digits, unsigned win_x, unsigned win_y) + : problem_type(type), window_x(win_x), window_y(win_y) +{ + // NOTE(dev): Only ever seed rand ONCE! + // TODO(dev): Should this be in main.cpp? + if(!seeded) + { + // srand(time()); + srand(0); + seeded = true; + } + + GenerateNumbers(type, (max_digits > 5 ? 5 : max_digits)); + + GenerateWindow(win_x, win_y); +} + +Problem::~Problem() +{ + if(win) + delwin(win); +} + +void Problem::Draw(bool selected) +{ + +} + +void Problem::GenerateNumbers(PROBLEM_TYPE type, unsigned short max_digits) +{ + // Determine how many digits long these numbers should be: + unsigned short digits = rand() % (max_digits - 1) + 1; // at least one digit + + switch(problem_type) + { + default: + case EASY_ADDITION: + for(auto digit = 0; digit < digits; ++digit) + { + unsigned short temp_number_top = rand() % 10; + unsigned short temp_number_bottom = rand() % (10 - number_top); + + // Place into correct digit posititon: + number_top += temp_number_top * (pow(10, digit)); + number_bottom += temp_number_bottom * (pow(10, digit)); + } + break; + case HARD_ADDITION: + for(auto digit = 0; digit < digits; ++digit) + { + unsigned short temp_number_top = rand() % 10; + unsigned short temp_number_bottom = rand() % 10; + + // Place into correct digit posititon: + number_top += temp_number_top * (pow(10, digit)); + number_bottom += temp_number_bottom * (pow(10, digit)); + } + break; + case EASY_SUBTRACTION: + for(auto digit = 0; digit < digits; ++digit) + { + unsigned short temp_number_bottom = rand() % 10; + // NOTE(dev): top number must be higher than bottom number + unsigned short temp_number_top; + if(temp_number_bottom == 9) + temp_number_top = 9; // can't mod by zero + else + temp_number_top = 9 - (rand() % (9 - temp_number_bottom)); + + // Place into correct digit posititon: + number_top += temp_number_top * (pow(10, digit)); + number_bottom += temp_number_bottom * (pow(10, digit)); + } + break; + case HARD_SUBTRACTION: + for(auto digit = 0; digit < digits; ++digit) + { + unsigned short temp_number_top = rand() % 10; + unsigned short temp_number_bottom = rand() % 10; + + // Place into correct digit posititon: + number_top += temp_number_top * (pow(10, digit)); + number_bottom += temp_number_bottom * (pow(10, digit)); + } + // NOTE(dev): don't allow negative numbers: + if(number_bottom > number_top) + number_bottom %= number_top; + break; + }; +} + +void Problem::GenerateWindow(unsigned win_x, unsigned win_y) +{ + // NOTE(dev): Max problem dimensions: + // Width: 13 characters + // Height: 8 characters + // +-----------+ + // | | + // | 12,345 | + // | + 99,999 | + // | ======== | + // | 112,344 | + // | | + // +-----------+ + + auto height = 8; + auto width = 13; + win = newwin(height, width, win_y, win_x); + if(!win) + return; + + // Draw the window: + box(win, 0, 0); // 0, 0 gives default characters for the vertical and horizontal lines + mvwprintw(win, 1, 1, "TEST"); + wrefresh(win); +} diff --git a/Problem.h b/Problem.h new file mode 100644 index 0000000..80fb105 --- /dev/null +++ b/Problem.h @@ -0,0 +1,43 @@ +#ifndef DVEREB_PROBLEM_H +#define DVEREB_PROBLEM_H + +#include +#include + +enum PROBLEM_TYPE +{ + // NOTE(dev): EASY means each digit can be handled without carryover. + // e.g. 33 + 46 is easy because 6+3 < 10 and 3+4 is < 10. + // HARD means you might have to carry over. + // e.g. 33 + 97 is hard because 7 + 3 = 10, carry the 1, 9 + 3 + 1 = 13, 130. + EASY_ADDITION = 1, + EASY_SUBTRACTION, + HARD_ADDITION, + HARD_SUBTRACTION, +}; + +class Problem +{ +public: + Problem(PROBLEM_TYPE type, unsigned short max_digits, unsigned win_x, unsigned win_y); + ~Problem(); + + void Draw(bool selected); + +private: + Problem() = delete; + static bool seeded; + + PROBLEM_TYPE problem_type; + WINDOW *win = nullptr; + unsigned window_x, window_y; + + int number_top; + int number_bottom; + + // Helper Functions: + void GenerateNumbers(PROBLEM_TYPE type, unsigned short max_digits); + void GenerateWindow(unsigned win_x, unsigned win_y); +}; + +#endif diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..a6a4bcf --- /dev/null +++ b/main.cpp @@ -0,0 +1,77 @@ +// TODO(dev): use 'halfdelay()' to parse input for pi menu choice 'secret.' + +// NOTE(dev): Gonna need this: +// mvwprintw(win, y, x, string); /* Move to (y, x) relative to window */ +// /* co-ordinates and then print */ + +#include + +#include "Problem.h" + +#define PROMPT " > " + +char MainMenu() +{ + clear(); + printw("\n"); + printw(" Welcome to Math Practice.\n"); + printw(" Please choose from the following menu:\n"); + printw(" 1. Addition\n"); + printw(" 2. Subtraction\n"); + printw(" 3. Both / Mixed\n"); + printw(" q. Quit\n"); + printw("\n"); + printw(PROMPT); + refresh(); + + return getch(); +} + +int main(int argc, char *argv[]) +{ + initscr(); + noecho(); // don't show user input + cbreak(); // handle user input immediately (i.e. don't wait for the user to press enter) + keypad(stdscr, true); // enable the numpad, F1-F12 keys, arrow keys, etc. + + while(true) + { + // game boolean flags: + bool addition = false; + bool subtraction = false; + + // Main Menu: + char menu_action = MainMenu(); + if(menu_action == 'q') + break; + else if(menu_action == '1') + addition = true; + else if(menu_action == '2') + subtraction = true; + else if(menu_action == '3') + addition = subtraction = true; + else + continue; // restart loop + + Problem *test = nullptr; + if(addition) + test = new Problem(EASY_ADDITION, 3, 3, 12); + if(subtraction) + test = new Problem(EASY_SUBTRACTION, 3, 3, 12); + if(addition && subtraction) + { + if(rand() % 2) + test = new Problem(HARD_ADDITION, 5, 3, 12); + else + test = new Problem(HARD_SUBTRACTION, 5, 3, 12); + } + + getch(); + + if(test) + delete test; + } + + endwin(); + return 0; +}