Initial Commit. WIP!

This commit is contained in:
David Vereb 2019-03-31 17:40:37 -05:00
commit be3e90c0ed
5 changed files with 246 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*~
mathprac

2
Makefile Normal file
View File

@ -0,0 +1,2 @@
mathprac: main.cpp Problem.cpp
clang++ -o mathprac -std=c++11 -g main.cpp Problem.cpp -lncurses

122
Problem.cpp Normal file
View File

@ -0,0 +1,122 @@
#include "Problem.h"
#include <cmath>
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);
}

43
Problem.h Normal file
View File

@ -0,0 +1,43 @@
#ifndef DVEREB_PROBLEM_H
#define DVEREB_PROBLEM_H
#include <cstdlib>
#include <ncurses.h>
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

77
main.cpp Normal file
View File

@ -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 <ncurses.h>
#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;
}