148 lines
3.5 KiB
C++
148 lines
3.5 KiB
C++
// 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 <ncurses.h>
|
|
|
|
#include "Problem.h"
|
|
|
|
#include <vector>
|
|
|
|
#define PROMPT " > "
|
|
|
|
std::vector<Problem*> GenerateProblems(bool addition, bool subtraction);
|
|
char MainMenu();
|
|
|
|
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
|
|
|
|
clear();
|
|
wrefresh(stdscr);
|
|
|
|
std::vector<Problem*> problems = GenerateProblems(addition, subtraction);
|
|
std::vector<Problem*>::iterator selected = problems.begin();
|
|
|
|
INPUT_STYLE input_style = INPUT_STYLE::INPUT_APPEND;
|
|
for(auto *problem : problems)
|
|
if(problem)
|
|
problem->Draw(problem == *selected, input_style);
|
|
|
|
while(selected != problems.end() && *selected)
|
|
{
|
|
Problem *current_problem = *selected;
|
|
current_problem->Draw(true, input_style); // refresh with new input
|
|
auto input = getch();
|
|
if(input == 'q') // early exit
|
|
break;
|
|
switch(input)
|
|
{
|
|
case KEY_LEFT:
|
|
input_style = INPUT_STYLE::INPUT_PREPEND;
|
|
break;
|
|
case KEY_RIGHT:
|
|
input_style = INPUT_STYLE::INPUT_APPEND;
|
|
break;
|
|
case KEY_BACKSPACE:
|
|
current_problem->ClearInput();
|
|
break;
|
|
case '0':
|
|
case '1':
|
|
case '2':
|
|
case '3':
|
|
case '4':
|
|
case '5':
|
|
case '6':
|
|
case '7':
|
|
case '8':
|
|
case '9':
|
|
current_problem->Input(input_style, input);
|
|
break;
|
|
};
|
|
|
|
if(current_problem->Correct())
|
|
++selected;
|
|
current_problem->Draw(current_problem == *selected, input_style); // refresh with new input
|
|
}
|
|
|
|
for(auto *problem : problems)
|
|
if(problem)
|
|
delete problem;
|
|
}
|
|
|
|
endwin();
|
|
return 0;
|
|
}
|
|
|
|
char MainMenu()
|
|
{
|
|
int middle_h = COLS / 2;
|
|
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);
|
|
refresh();
|
|
|
|
return getch();
|
|
}
|
|
|
|
std::vector<Problem*> GenerateProblems(bool addition, bool subtraction)
|
|
{
|
|
std::vector<Problem*> rtn;
|
|
for(auto y = 1; y < LINES - 10; y += 9)
|
|
{
|
|
for(auto x = 2; x < COLS - 16; x += 15)
|
|
{
|
|
Problem *test = nullptr;
|
|
if(addition != subtraction)
|
|
{
|
|
if(addition)
|
|
test = new Problem(EASY_ADDITION, 3, x, y);
|
|
if(subtraction)
|
|
test = new Problem(EASY_SUBTRACTION, 3, x, y);
|
|
}
|
|
if(addition && subtraction)
|
|
{
|
|
if(rand() % 2)
|
|
test = new Problem(HARD_ADDITION, 5, x, y);
|
|
else
|
|
test = new Problem(HARD_SUBTRACTION, 5, x, y);
|
|
}
|
|
rtn.push_back(test);
|
|
}
|
|
}
|
|
return rtn;
|
|
}
|