Compare commits

..

No commits in common. "main" and "59cab98b201367ad31a8356bb9e8d701cb59179d" have entirely different histories.

9 changed files with 48 additions and 490 deletions

View File

@ -1,91 +0,0 @@
#include "Demo.h"
#include "Pieces.h"
#include <curses.h>
#include <set>
#include <string>
#include <vector>
void DemoPieces()
{
std::set rotations = {
Rotation::ROTATION_NONE,
Rotation::ROTATION_90,
Rotation::ROTATION_180,
Rotation::ROTATION_270,
};
for(auto rotation : rotations)
{
clear();
int x, y;
// Always same distance apart:
x = 10;
y = 2;
for(auto i = 0; i < 8; ++i)
{
PieceData data = pieces.at(i);
data = RotatePieceData(data, rotation);
PD_DrawPiece(data, y, x, i);
mvaddstr(y-1, x, "Piece");
mvaddstr(y-1, x + 6, std::to_string(i + 1).c_str());
x += 12;
}
// Always same width apart:
x = 2;
y = 10;
for(auto i = 0; i < 8; ++i)
{
PieceData data = pieces.at(i);
data = RotatePieceData(data, rotation);
PD_DrawPiece(data, y, x, i);
x += PD_PieceWidth(data) * 2 + 2;
}
// Always same height apart, sharing first piece with same-width row:
x = 2;
y = 10;
{
auto data = pieces.at(0);
data = RotatePieceData(data, rotation);
y += PD_PieceHeight(data) + 1;
}
for(auto i = 1; i < 8; ++i) // skip first
{
PieceData data = pieces.at(i);
data = RotatePieceData(data, rotation);
PD_DrawPiece(data, y, x, i);
y += PD_PieceHeight(data) + 1;
}
getch();
}
std::vector<Flip> flips = {
Flip::FLIP_NONE,
Flip::FLIP_HORIZONTAL,
Flip::FLIP_HORIZONTAL,
Flip::FLIP_VERTICAL,
Flip::FLIP_VERTICAL,
};
std::vector<PieceData> piece_data_to_flip;
for(size_t p = 0; p < pieces.size(); ++p)
piece_data_to_flip.push_back(pieces.at(p));
for(const auto &flip : flips)
{
clear();
int y = 2;
int i = 0;
for(auto &data : piece_data_to_flip)
{
FlipPieceData(data, flip);
PD_DrawPiece(data, y, 2, i++);
y += PD_PieceHeight(data) + 2;
}
getch();
}
move(8, 2);
}

6
Demo.h
View File

@ -1,6 +0,0 @@
#ifndef APAD_DEMO_H
#define APAD_DEMO_H
void DemoPieces();
#endif

View File

@ -1,6 +1,6 @@
build/apad: main.cpp Pieces.cpp Pieces.h Demo.cpp Demo.h
build/apad: main.cpp Pieces.cpp
mkdir -p build
clang++ -g -O0 -std=c++17 -o $@ main.cpp Pieces.cpp Demo.cpp -lncurses
clang++ -g -O0 -std=c++17 -o $@ $^ -lncurses
clean:
rm build/apad

View File

@ -330,152 +330,3 @@ void DrawPiece(const Piece &piece, int y, int x, Rotation rotation)
}
}
PieceData RotatePieceData(const PieceData &data, Rotation rotation)
{
PieceData rtn;
if(!data.size())
return rtn;
if(!data[0].size())
return rtn;
// Step 1: Size it:
switch(rotation)
{
default:
case Rotation::ROTATION_NONE:
case Rotation::ROTATION_180:
rtn.resize(data.size());
for(auto &row : rtn)
row.resize(data[0].size());
break;
case Rotation::ROTATION_90:
case Rotation::ROTATION_270:
rtn.resize(data[0].size());
for(auto &row : rtn)
row.resize(data.size());
break;
};
// Step 2: Move it:
switch(rotation)
{
default:
case Rotation::ROTATION_NONE:
rtn = data;
break;
case Rotation::ROTATION_90:
for(size_t row = 0; row < data.size(); ++row)
{
size_t opposite_row = data.size() - row - 1;
for(size_t col = 0; col < data[row].size(); ++col)
{
size_t opposite_col = data[opposite_row].size() - col - 1;
// NOTE(dev): x's become y's
// NOTE(dev): y's become max x - x's
rtn[col][opposite_row] = data[row][col];
}
}
break;
case Rotation::ROTATION_180:
for(size_t row = 0; row < data.size(); ++row)
{
size_t opposite_row = data.size() - row - 1;
for(size_t col = 0; col < data[row].size(); ++col)
{
size_t opposite_col = data[opposite_row].size() - col - 1;
rtn[opposite_row][opposite_col] = data[row][col];
}
}
break;
case Rotation::ROTATION_270:
for(size_t row = 0; row < data.size(); ++row)
{
size_t opposite_row = data.size() - row - 1;
for(size_t col = 0; col < data[row].size(); ++col)
{
size_t opposite_col = data[opposite_row].size() - col - 1;
// NOTE(dev): x's become max y - y's
// NOTE(dev): y's become x's
rtn[opposite_col][row] = data[row][col];
}
}
break;
};
return rtn;
}
void FlipPieceData(PieceData &data, Flip flip)
{
if(!data.size())
return;
if(!data[0].size())
return;
switch(flip)
{
default:
case Flip::FLIP_NONE:
return;
case Flip::FLIP_HORIZONTAL:
// Go through EVERY row:
for(size_t row = 0; row < data.size(); ++row)
{
// And swap the front half with the back half
// [1][2][3][2][1]
// ^ ^ ^ ^
// | +-----+ | <-- second
// | |
// +-----------+ <-- first
for(size_t col = 0; col < data[0].size() / 2; ++col)
{
// for each row, swap columns
bool temp = data[row][col];
data[row][col] = data[row][data[0].size() - col - 1];
data[row][data[0].size() - col - 1] = temp;
}
}
break;
case Flip::FLIP_VERTICAL:
// Go through EVERY col:
for(size_t col = 0; col < data[0].size(); ++col)
{
// And swap the top half with the bottom half
for(size_t row = 0; row < data.size() / 2; ++row)
{
// for each row, swap columns
bool temp = data[row][col];
data[row][col] = data[data.size() - row - 1][col];
data[data.size() - row - 1][col] = temp;
}
}
break;
};
}
unsigned PD_PieceHeight(const PieceData &data)
{
return data.size();
}
unsigned PD_PieceWidth(const PieceData &data)
{
size_t rtn = 0;
for(auto row : data)
rtn = std::max(rtn, row.size());
return rtn;
}
void PD_DrawPiece(const PieceData &data, int y, int x, int color)
{
attrset(COLOR_PAIR(static_cast<int>(color) % 8));
for(size_t row = 0; row < data.size(); ++row)
{
for(size_t col = 0; col < data[row].size(); ++col)
{
if(data[row][col])
mvaddstr(y + row, x + (col * 2), "[]");
}
}
}

View File

@ -2,8 +2,6 @@
#define APAD_PIECES_H
#include <stdint.h>
#include <unordered_map>
#include <vector>
enum Rotation {
ROTATION_NONE,
@ -11,11 +9,6 @@ enum Rotation {
ROTATION_180,
ROTATION_270,
};
enum Flip {
FLIP_NONE,
FLIP_HORIZONTAL,
FLIP_VERTICAL,
};
#define NUM_PIECES 8
typedef uint8_t Piece; // only need literally 8, not 8 bits, but hey.
@ -76,75 +69,6 @@ typedef uint8_t Piece; // only need literally 8, not 8 bits, but hey.
* [][][]
*/
typedef std::vector<std::vector<bool>> PieceData;
const std::unordered_map<Piece, PieceData> pieces =
{
{
PIECE_ONE,
{
{ true, true, true, },
{ true, true, true, },
}
},
{
PIECE_TWO,
{
{ true, false, },
{ true, true, },
{ true, false, },
{ true, false, },
}
},
{
PIECE_THREE,
{
{ false, true, },
{ false, true, },
{ false, true, },
{ true, true, },
}
},
{
PIECE_FOUR,
{
{ true, true, },
{ true, false, },
{ true, true, },
}
},
{
PIECE_FIVE,
{
{ true, true, false, },
{ false, true, false, },
{ false, true, true, },
}
},
{
PIECE_SIX,
{
{ true, true, false, false, },
{ false, true, true, true, },
}
},
{
PIECE_SEVEN,
{
{ true, true, },
{ true, true, },
{ true, false, },
}
},
{
PIECE_EIGHT,
{
{ false, false, true, },
{ false, false, true, },
{ true, true, true, },
}
},
};
unsigned PieceHeight(const Piece &piece,
Rotation rotation = Rotation::ROTATION_NONE);
unsigned PieceWidth(const Piece &piece,
@ -152,11 +76,4 @@ unsigned PieceWidth(const Piece &piece,
void DrawPiece(const Piece &piece, int y, int x,
Rotation rotation = Rotation::ROTATION_NONE);
PieceData RotatePieceData(const PieceData &data, Rotation rotation);
void FlipPieceData(PieceData &data, Flip flip);
unsigned PD_PieceHeight(const PieceData &data);
unsigned PD_PieceWidth(const PieceData &data);
void PD_DrawPiece(const PieceData &data, int y, int x, int color = 0);
#endif

31
README
View File

@ -1,31 +0,0 @@
# Grid:
[0 1 2 3 4 5 ]
[6 7 8 9 10 11]
[12 13 14 15 16 17 18]
[19 20 21 22 23 24 25]
[26 27 28 29 30 31 32]
[33 34 35 36 37 38 39]
[40 41 42]
# Order of Operations:
When rendering, always perform the actions in this order:
1. Flips, if set
2. Rotation
# All Piece Placement Format:
Assumes you're placing, in order, all pieces
2 bits for flip, 2 bits for rotation, 6 bits for position, 8 times.
10 * 8 = 80 bits.
# TODO(dev):
# Individual Piece Placement Format:
3 bits for piece number (#0 - #7)
2 bits for flip
(first bit = horizontal flip on)
(second bit = vertical flip on)
2 bits for rotation
(none, 90, 180, 270)
6 bits for position

7
TODO
View File

@ -1,7 +0,0 @@
# https://www.amazon.com/Puzzle-Day-Acrylic-Original-Challenges/dp/B0BNRSLTG5?th=1
The acrylic version of the puzzle has colors for the pieces. Perhaps change our colors to match?
Techincally you can reach all possible orientations & flips of a piece in just three bytes (instead of four).
A flip horizontally & vertically is identical to a 180 degree rotation, for example.
You can probably get away with just a horizontal flip and then 0,90,180,270 for a total of three bytes.

160
main.cpp
View File

@ -1,4 +1,3 @@
#include "Demo.h"
#include "Pieces.h"
#include <curses.h>
@ -11,11 +10,8 @@
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);
void GetPosition(unsigned long position, int &y, int &x);
void DrawGrid(int y = 0, int x = 0);
// DEBUG:
void DemoPieces();
int main(int argc, char *argv[])
{
@ -37,36 +33,16 @@ int main(int argc, char *argv[])
// 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);
unsigned long position = (0b1111110000000 & input_val) >> 7;
GetPosition(position, y, x);
PlacePiece(piece,
{ horizontal, vertical },
rotation,
y,
x * 2);
// y += PD_PieceHeight(pieces.at(piece)) + 1;
y += 5;
Piece piece = 0b111 & std::atoi(argv[input]);
DrawPiece(piece, y, 1);
y += PieceHeight(piece) + 1;
} catch(const std::exception &e) {
mvaddstr(y, 1, "std::atoi error");
y += 3;
}
if(++count > 7)
{
count = 0;
y = 1;
x += 10;
y += 2;
}
}
}
@ -109,96 +85,52 @@ void init_colors()
init_pair(7, COLOR_WHITE, COLOR_BLACK);
}
void PlacePiece(Piece piece, const std::vector<Flip> &flips, Rotation rotation, int y, int x)
void DemoPieces()
{
PieceData data = pieces.at(piece);
for(Flip flip : flips)
FlipPieceData(data, flip);
data = RotatePieceData(data, rotation);
PD_DrawPiece(data, y, x, piece);
}
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;
}
void GetPosition(unsigned long position, int &y, int &x)
{
if(position < 6)
std::set rotations = {
Rotation::ROTATION_NONE,
Rotation::ROTATION_90,
Rotation::ROTATION_180,
Rotation::ROTATION_270,
};
for(auto rotation : rotations)
{
y = 0;
x = position;
return;
}
clear();
if(position < 12)
{
y = 1;
x = position - 6;
return;
}
int x, y;
if(position < 19)
{
// Always same distance apart:
x = 10;
y = 2;
x = position - 12;
return;
}
for(auto i = 0; i < 8; ++i)
{
Piece piece = static_cast<Piece>(i);
DrawPiece(piece, y, x, rotation);
mvaddstr(y-1, x, "Piece");
mvaddstr(y-1, x + 6, std::to_string(i + 1).c_str());
x += 12;
}
if(position < 26)
{
y = 3;
x = position - 19;
return;
}
// Always same width apart:
x = 2;
y = 10;
for(auto i = 0; i < 8; ++i)
{
Piece piece = static_cast<Piece>(i);
DrawPiece(piece, y, x, rotation);
x += PieceWidth(piece, rotation) * 2 + 2;
}
// Always same height apart, sharing first piece with same-width row:
x = 2;
y = 10 + PieceHeight(0, rotation) + 1;
for(auto i = 1; i < 8; ++i) // skip first
{
Piece piece = static_cast<Piece>(i);
DrawPiece(piece, y, x, rotation);
y += PieceHeight(piece, rotation) + 1;
}
if(position < 33)
{
y = 4;
x = position - 26;
return;
getch();
}
if(position < 40)
{
y = 5;
x = position - 33;
return;
}
if(position < 43)
{
y = 6;
x = position - 40;
return;
}
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());
move(8, 2);
}

View File

@ -1,7 +0,0 @@
# Format:
# Month-Day ./apad 0 1 2 3 4 5 6 7
0404 ./apad 29 518 800 1900 2194 2823 3377 4267
0405 ./apad 71 526 788 1061 2178 2816 3331 4401
0406 ./apad 0 518 818 2241 2463 2596 2947 4237
0408 ./apad 32 130 430 1969 2596 3083 3367 3597