From 39fa40eaad4f16474540e523e4c2024a1f4e9cce Mon Sep 17 00:00:00 2001 From: David Vereb Date: Mon, 1 Apr 2019 09:08:41 -0400 Subject: [PATCH] Fixed number generation bugs. Added simple draw code that needs to be styled, yet. --- Problem.cpp | 46 +++++++++++++++++++++++++++++++++++++--------- main.cpp | 16 +++++++++++----- 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/Problem.cpp b/Problem.cpp index 62e6055..b652bd6 100644 --- a/Problem.cpp +++ b/Problem.cpp @@ -29,13 +29,37 @@ Problem::~Problem() void Problem::Draw(bool selected) { - + if(!win) + return; + + // Draw the window: + if(selected) + box(win, 0, 0); + else + box(win, 0, 0); // 0, 0 gives default characters for the vertical and horizontal lines + + mvwprintw(win, 1, 2, "%5d", number_top); + mvwprintw(win, 2, 2, "%5d", number_bottom); + switch(problem_type) + { + case EASY_ADDITION: + case HARD_ADDITION: + mvwprintw(win, 2, 1, "+"); + break; + case EASY_SUBTRACTION: + case HARD_SUBTRACTION: + mvwprintw(win, 2, 1, "-"); + break; + }; + wrefresh(win); } 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 + unsigned short digits = rand() % (max_digits) + 1; // at least one digit + number_top = 0; + number_bottom = 0; switch(problem_type) { @@ -44,7 +68,11 @@ void Problem::GenerateNumbers(PROBLEM_TYPE type, unsigned short max_digits) for(auto digit = 0; digit < digits; ++digit) { unsigned short temp_number_top = rand() % 10; - unsigned short temp_number_bottom = rand() % (10 - number_top); + unsigned short temp_number_bottom; + if(temp_number_top == 9) + temp_number_bottom = 0; + else + temp_number_bottom = rand() % (10 - temp_number_top); // Place into correct digit posititon: number_top += temp_number_top * (pow(10, digit)); @@ -90,7 +118,12 @@ void Problem::GenerateNumbers(PROBLEM_TYPE type, unsigned short max_digits) } // NOTE(dev): don't allow negative numbers: if(number_bottom > number_top) - number_bottom %= number_top; + { + if(number_top) + number_bottom %= number_top; + else + number_bottom = 0; // lol, 0 - 0 is hard? + } break; }; } @@ -114,9 +147,4 @@ void Problem::GenerateWindow(unsigned win_x, unsigned win_y) 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/main.cpp b/main.cpp index a6a4bcf..302a6f1 100644 --- a/main.cpp +++ b/main.cpp @@ -54,11 +54,14 @@ int main(int argc, char *argv[]) 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(addition != subtraction) + { + 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); @@ -66,6 +69,9 @@ int main(int argc, char *argv[]) test = new Problem(HARD_SUBTRACTION, 5, 3, 12); } + if(test) + test->Draw(true); + getch(); if(test)