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:
mvwprintw(win, 3, 2, "-");
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, "========");
if(Input() > 999999)
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());
else if(selected)
mvwprintw(win, 5, 3, " ");
// Place the cursor in the correct position!
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));
}
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:
number_top = 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?
}
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:
return number_top - number_bottom == Input();
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!

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.
EASY_ADDITION = 1,
EASY_SUBTRACTION,
EASY_MULTIPLICATION,
EASY_DIVISION,
HARD_ADDITION,
HARD_SUBTRACTION,
HARD_MULTIPLICATION,
HARD_DIVISION,
};
enum INPUT_STYLE

View File

@ -12,7 +12,8 @@
#define PROMPT " > "
std::vector<Problem*> GenerateProblems(bool addition, bool subtraction);
std::vector<Problem*> GenerateProblems(bool addition, bool subtraction,
bool multiplication, bool division);
char MainMenu();
int main(int argc, char *argv[])
@ -27,6 +28,8 @@ int main(int argc, char *argv[])
// game boolean flags:
bool addition = false;
bool subtraction = false;
bool multiplication = false;
bool division = false;
// Main Menu:
char menu_action = MainMenu();
@ -38,13 +41,20 @@ int main(int argc, char *argv[])
subtraction = true;
else if(menu_action == '3')
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
continue; // restart loop
clear();
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();
INPUT_STYLE input_style = INPUT_STYLE::INPUT_APPEND;
@ -84,7 +94,9 @@ int main(int argc, char *argv[])
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;
current_problem->Draw(current_problem == *selected, input_style); // refresh with new input
}
@ -104,21 +116,25 @@ char MainMenu()
int middle_v = LINES / 2;
clear();
mvprintw(middle_v - 5, middle_h - 20, "\n");
mvprintw(middle_v - 4, 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 - 2, middle_h - 20, " 1. Addition\n");
mvprintw(middle_v - 1, middle_h - 20, " 2. Subtraction\n");
mvprintw(middle_v + 0, middle_h - 20, " 3. Both / Mixed\n");
mvprintw(middle_v + 1, middle_h - 20, " q. Quit\n");
mvprintw(middle_v + 2, middle_h - 20, "\n");
mvprintw(middle_v + 3, middle_h - 20, PROMPT);
mvprintw(middle_v - 6, middle_h - 20, "\n");
mvprintw(middle_v - 5, middle_h - 20, " Welcome to Math Practice.\n");
mvprintw(middle_v - 4, middle_h - 20, " Please choose from the following menu:\n");
mvprintw(middle_v - 3, middle_h - 20, " 1. Addition\n");
mvprintw(middle_v - 2, middle_h - 20, " 2. Subtraction\n");
mvprintw(middle_v - 1, middle_h - 20, " 3. Both / Mixed\n");
mvprintw(middle_v - 0, middle_h - 20, " 4. Multiplication\n");
mvprintw(middle_v + 1, middle_h - 20, " 5. Division\n");
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();
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;
for(auto y = 1; y < LINES - 10; y += 9)
@ -126,6 +142,8 @@ std::vector<Problem*> GenerateProblems(bool addition, bool subtraction)
for(auto x = 2; x < COLS - 16; x += 15)
{
Problem *test = nullptr;
if(addition || subtraction)
{
if(addition != subtraction)
{
if(addition)
@ -133,7 +151,7 @@ std::vector<Problem*> GenerateProblems(bool addition, bool subtraction)
if(subtraction)
test = new Problem(EASY_SUBTRACTION, 3, x, y);
}
if(addition && subtraction)
else
{
if(rand() % 2)
test = new Problem(HARD_ADDITION, 5, x, y);
@ -142,6 +160,24 @@ std::vector<Problem*> GenerateProblems(bool addition, bool subtraction)
}
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);
}
}
}
return rtn;
}