Added multiplication.

This commit is contained in:
David Vereb 2020-03-10 15:52:49 -04:00
parent 2d0e74d0de
commit ed001c9139
3 changed files with 102 additions and 26 deletions

View File

@ -56,12 +56,22 @@ void Problem::Draw(bool selected, const INPUT_STYLE &input_style)
case HARD_SUBTRACTION: case HARD_SUBTRACTION:
mvwprintw(win, 3, 2, "-"); mvwprintw(win, 3, 2, "-");
break; break;
case EASY_MULTIPLICATION:
case HARD_MULTIPLICATION:
mvwprintw(win, 3, 2, "*");
break;
case EASY_DIVISION:
case HARD_DIVISION:
mvwprintw(win, 3, 2, "÷");
break;
}; };
mvwprintw(win, 4, 2, "========"); mvwprintw(win, 4, 2, "========");
if(Input() > 999999) if(Input() > 999999)
mvwprintw(win, 5, 3, " FAILED"); mvwprintw(win, 5, 3, " FAILED");
else if(Input() != 0) else if(Input() != 0 || selected) // if it's not selected then the answer could have been zero
mvwprintw(win, 5, 3, "%'7d", Input()); mvwprintw(win, 5, 3, "%'7d", Input());
else if(selected)
mvwprintw(win, 5, 3, " ");
// Place the cursor in the correct position! // Place the cursor in the correct position!
int length = input.length() + ((input.length() - 1) / 3); // add commas, too! int length = input.length() + ((input.length() - 1) / 3); // add commas, too!
@ -113,6 +123,15 @@ void Problem::GenerateNumbers(PROBLEM_TYPE type, unsigned short max_digits)
number_bottom += temp_number_bottom * (pow(10, digit)); number_bottom += temp_number_bottom * (pow(10, digit));
} }
break; break;
case EASY_MULTIPLICATION:
number_top = rand() % 13;
number_bottom = rand() % 13;
break;
case EASY_DIVISION:
// TODO(dev):
number_top = 1;
number_bottom = 1;
break;
case HARD_ADDITION: case HARD_ADDITION:
number_top = rand() % int(pow(10, digits)); number_top = rand() % int(pow(10, digits));
number_bottom = rand() % int(pow(10, digits)); number_bottom = rand() % int(pow(10, digits));
@ -129,6 +148,15 @@ void Problem::GenerateNumbers(PROBLEM_TYPE type, unsigned short max_digits)
number_bottom = 0; // lol, 0 - 0 is hard? number_bottom = 0; // lol, 0 - 0 is hard?
} }
break; break;
case HARD_MULTIPLICATION:
number_top = rand() % 130;
number_bottom = rand() % 130;
break;
case HARD_DIVISION:
// TODO(dev):
number_top = 10;
number_bottom = 10;
break;
}; };
} }
@ -194,6 +222,14 @@ bool Problem::Correct() const
case HARD_SUBTRACTION: case HARD_SUBTRACTION:
return number_top - number_bottom == Input(); return number_top - number_bottom == Input();
break; break;
case EASY_MULTIPLICATION:
case HARD_MULTIPLICATION:
return number_top * number_bottom == Input();
break;
case EASY_DIVISION:
case HARD_DIVISION:
return number_top / number_bottom == Input();
break;
}; };
// TODO(dev): Assert that all conditions are handled! // TODO(dev): Assert that all conditions are handled!

View File

@ -13,8 +13,12 @@ enum PROBLEM_TYPE
// e.g. 33 + 97 is hard because 7 + 3 = 10, carry the 1, 9 + 3 + 1 = 13, 130. // e.g. 33 + 97 is hard because 7 + 3 = 10, carry the 1, 9 + 3 + 1 = 13, 130.
EASY_ADDITION = 1, EASY_ADDITION = 1,
EASY_SUBTRACTION, EASY_SUBTRACTION,
EASY_MULTIPLICATION,
EASY_DIVISION,
HARD_ADDITION, HARD_ADDITION,
HARD_SUBTRACTION, HARD_SUBTRACTION,
HARD_MULTIPLICATION,
HARD_DIVISION,
}; };
enum INPUT_STYLE enum INPUT_STYLE

View File

@ -12,7 +12,8 @@
#define PROMPT " > " #define PROMPT " > "
std::vector<Problem*> GenerateProblems(bool addition, bool subtraction); std::vector<Problem*> GenerateProblems(bool addition, bool subtraction,
bool multiplication, bool division);
char MainMenu(); char MainMenu();
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -27,6 +28,8 @@ int main(int argc, char *argv[])
// game boolean flags: // game boolean flags:
bool addition = false; bool addition = false;
bool subtraction = false; bool subtraction = false;
bool multiplication = false;
bool division = false;
// Main Menu: // Main Menu:
char menu_action = MainMenu(); char menu_action = MainMenu();
@ -38,13 +41,20 @@ int main(int argc, char *argv[])
subtraction = true; subtraction = true;
else if(menu_action == '3') else if(menu_action == '3')
addition = subtraction = true; addition = subtraction = true;
else if(menu_action == '4')
multiplication = true;
else if(menu_action == '5')
division = true;
else if(menu_action == '6')
multiplication = division = true;
else else
continue; // restart loop continue; // restart loop
clear(); clear();
wrefresh(stdscr); wrefresh(stdscr);
std::vector<Problem*> problems = GenerateProblems(addition, subtraction); std::vector<Problem*> problems = GenerateProblems(addition, subtraction,
multiplication, division);
std::vector<Problem*>::iterator selected = problems.begin(); std::vector<Problem*>::iterator selected = problems.begin();
INPUT_STYLE input_style = INPUT_STYLE::INPUT_APPEND; INPUT_STYLE input_style = INPUT_STYLE::INPUT_APPEND;
@ -84,7 +94,9 @@ int main(int argc, char *argv[])
break; break;
}; };
if(current_problem->Correct()) // NOTE(dev): If the answer is zero and they hit backspace to clear input, it treated it
// as if they had entered zero themselves.
if(input != KEY_BACKSPACE && current_problem->Correct())
++selected; ++selected;
current_problem->Draw(current_problem == *selected, input_style); // refresh with new input current_problem->Draw(current_problem == *selected, input_style); // refresh with new input
} }
@ -104,21 +116,25 @@ char MainMenu()
int middle_v = LINES / 2; int middle_v = LINES / 2;
clear(); clear();
mvprintw(middle_v - 5, middle_h - 20, "\n"); mvprintw(middle_v - 6, middle_h - 20, "\n");
mvprintw(middle_v - 4, middle_h - 20, " Welcome to Math Practice.\n"); mvprintw(middle_v - 5, middle_h - 20, " Welcome to Math Practice.\n");
mvprintw(middle_v - 3, middle_h - 20, " Please choose from the following menu:\n"); mvprintw(middle_v - 4, middle_h - 20, " Please choose from the following menu:\n");
mvprintw(middle_v - 2, middle_h - 20, " 1. Addition\n"); mvprintw(middle_v - 3, middle_h - 20, " 1. Addition\n");
mvprintw(middle_v - 1, middle_h - 20, " 2. Subtraction\n"); mvprintw(middle_v - 2, middle_h - 20, " 2. Subtraction\n");
mvprintw(middle_v + 0, middle_h - 20, " 3. Both / Mixed\n"); mvprintw(middle_v - 1, middle_h - 20, " 3. Both / Mixed\n");
mvprintw(middle_v + 1, middle_h - 20, " q. Quit\n"); mvprintw(middle_v - 0, middle_h - 20, " 4. Multiplication\n");
mvprintw(middle_v + 2, middle_h - 20, "\n"); mvprintw(middle_v + 1, middle_h - 20, " 5. Division\n");
mvprintw(middle_v + 3, middle_h - 20, PROMPT); mvprintw(middle_v + 2, middle_h - 20, " 6. Both / Mixed\n");
mvprintw(middle_v + 3, middle_h - 20, " q. Quit\n");
mvprintw(middle_v + 4, middle_h - 20, "\n");
mvprintw(middle_v + 5, middle_h - 20, PROMPT);
refresh(); refresh();
return getch(); return getch();
} }
std::vector<Problem*> GenerateProblems(bool addition, bool subtraction) std::vector<Problem*> GenerateProblems(bool addition, bool subtraction,
bool multiplication, bool division)
{ {
std::vector<Problem*> rtn; std::vector<Problem*> rtn;
for(auto y = 1; y < LINES - 10; y += 9) for(auto y = 1; y < LINES - 10; y += 9)
@ -126,21 +142,41 @@ std::vector<Problem*> GenerateProblems(bool addition, bool subtraction)
for(auto x = 2; x < COLS - 16; x += 15) for(auto x = 2; x < COLS - 16; x += 15)
{ {
Problem *test = nullptr; Problem *test = nullptr;
if(addition != subtraction) if(addition || subtraction)
{ {
if(addition) if(addition != subtraction)
test = new Problem(EASY_ADDITION, 3, x, y); {
if(subtraction) if(addition)
test = new Problem(EASY_SUBTRACTION, 3, x, y); test = new Problem(EASY_ADDITION, 3, x, y);
} if(subtraction)
if(addition && subtraction) test = new Problem(EASY_SUBTRACTION, 3, x, y);
{ }
if(rand() % 2)
test = new Problem(HARD_ADDITION, 5, x, y);
else else
test = new Problem(HARD_SUBTRACTION, 5, x, y); {
if(rand() % 2)
test = new Problem(HARD_ADDITION, 5, x, y);
else
test = new Problem(HARD_SUBTRACTION, 5, x, y);
}
rtn.push_back(test);
}
if(multiplication || division)
{
if(multiplication && !division)
test = new Problem(EASY_MULTIPLICATION, 2, x, y);
else if(division && !multiplication)
test = new Problem(EASY_DIVISION, 2, x, y);
else
{
// TODO(dev): Add hard problems
if(rand() % 2)
test = new Problem(HARD_MULTIPLICATION, 3, x, y);
else
test = new Problem(HARD_DIVISION, 3, x, y);
}
rtn.push_back(test);
} }
rtn.push_back(test);
} }
} }
return rtn; return rtn;