A-Puzzle-A-Day/main.cpp

204 lines
4.2 KiB
C++
Raw Normal View History

#include "Demo.h"
2023-04-02 10:09:17 -04:00
#include "Pieces.h"
#include <curses.h>
#include <signal.h>
#include <set>
2023-04-02 10:09:17 -04:00
#include <string>
#include <unordered_map>
static void finish(int sig);
void init_colors();
void PlacePiece(Piece piece, const std::vector<Flip> &flips, Rotation rotation, int y, int x);
std::string PlaceName(unsigned int position);
2023-04-03 22:22:59 -04:00
void GetPosition(unsigned long position, int &y, int &x);
void DrawGrid(int y = 0, int x = 0);
2023-04-02 10:09:17 -04:00
int main(int argc, char *argv[])
{
initscr(); /* initialize the curses library */
keypad(stdscr, TRUE); /* enable keyboard mapping */
nonl(); /* tell curses not to do NL->CR/NL on output */
cbreak(); /* take input chars one at a time, no wait for \n */
echo(); /* echo input - in color */
if(has_colors())
init_colors();
if(argc == 2 && std::string(argv[1]) == "-d")
{
DemoPieces();
finish(0);
}
// for each piece passed in, render it:
{
DrawGrid();
int x = 1;
int y = 1;
auto count = 0;
for(auto input = 1; input < argc; ++input)
{
try {
unsigned long input_val = std::atoi(argv[input]);
Piece piece = 0b111 & input_val;
Flip horizontal = (0b1000 & input_val) ? Flip::FLIP_HORIZONTAL : Flip::FLIP_NONE;
Flip vertical = (0b10000 & input_val) ? Flip::FLIP_VERTICAL : Flip::FLIP_NONE;
Rotation rotation = static_cast<Rotation>((0b1100000 & input_val) >> 5);
2023-04-03 22:22:59 -04:00
unsigned long position = (0b1111110000000 & input_val) >> 7;
GetPosition(position, y, x);
PlacePiece(piece,
{ horizontal, vertical },
rotation,
y,
2023-04-03 22:22:59 -04:00
x * 2);
// y += PD_PieceHeight(pieces.at(piece)) + 1;
y += 5;
} catch(const std::exception &e) {
mvaddstr(y, 1, "std::atoi error");
y += 3;
}
if(++count > 7)
{
count = 0;
y = 1;
x += 10;
}
}
}
2023-04-02 10:09:17 -04:00
int num = 0;
while(true)
{
int c = getch(); /* refresh, accept single keystroke of input */
attrset(COLOR_PAIR(num++ % 8));
/* process the command keystroke */
}
finish(0);
}
// NOTE(dev): ALways execute so we don't screw up our terminal.
static void finish(int sig)
{
endwin();
std::exit(0);
2023-04-02 10:09:17 -04:00
}
void init_colors()
{
start_color();
/*
* Simple color assignment, often all we need. Color pair 0 cannot
* be redefined. This example uses the same value for the color
* pair as for the foreground color, though of course that is not
* necessary:
*/
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_YELLOW, COLOR_BLACK);
init_pair(4, COLOR_BLUE, COLOR_BLACK);
init_pair(5, COLOR_CYAN, COLOR_BLACK);
init_pair(6, COLOR_MAGENTA, COLOR_BLACK);
init_pair(7, COLOR_WHITE, COLOR_BLACK);
}
void PlacePiece(Piece piece, const std::vector<Flip> &flips, Rotation rotation, int y, int x)
{
PieceData data = pieces.at(piece);
for(Flip flip : flips)
FlipPieceData(data, flip);
2023-04-03 21:22:44 -04:00
data = RotatePieceData(data, rotation);
2023-04-03 21:22:44 -04:00
PD_DrawPiece(data, y, x, piece);
}
2023-04-03 22:22:59 -04:00
std::string PlaceName(unsigned int position)
{
static const std::vector<std::string> months =
{ "JA", "FE", "MR", "AP", "MY", "JN", "JL", "AU", "SE", "OC", "NO", "DE", };
if(position < 12)
return months[position];
auto rtn = std::to_string(position - 11);
if(rtn.size() == 1)
rtn = "0" + rtn;
return rtn;
}
2023-04-03 22:22:59 -04:00
void GetPosition(unsigned long position, int &y, int &x)
{
if(position < 6)
{
y = 0;
x = position;
return;
}
if(position < 12)
{
y = 1;
x = position - 6;
return;
}
if(position < 19)
{
y = 2;
x = position - 12;
return;
}
if(position < 26)
{
y = 3;
x = position - 19;
return;
}
if(position < 33)
{
y = 4;
x = position - 26;
return;
}
if(position < 40)
{
y = 5;
x = position - 33;
return;
}
if(position < 43)
{
y = 6;
x = position - 40;
}
x = y = 0;
return;
}
void DrawGrid(int y, int x)
{
// DEBUG: Draw Grid
for(auto row = 0; row < 2; ++row)
for(auto col = 0; col < 6; ++col)
mvaddstr(y+ row, x + col * 2, PlaceName((6 * row) + col).c_str());
for(auto row = 2; row < 6; ++row)
for(auto col = 0; col < 7; ++col)
mvaddstr(y +row, x + col * 2, PlaceName(12 + (7 * (row - 2) + col)).c_str());
for(auto col = 0; col < 3; ++col)
mvaddstr(y + 6, x + col * 2, PlaceName(40 + col).c_str());
}