123 lines
3.0 KiB
C++
123 lines
3.0 KiB
C++
|
#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);
|
||
|
}
|