Trying piece data with vectors of vectors of booleans.

This commit is contained in:
David Vereb 2023-04-02 21:52:12 -04:00
parent 59cab98b20
commit 9da2be773a
4 changed files with 204 additions and 10 deletions

View File

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

View File

@ -330,3 +330,116 @@ 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;
};
switch(rotation)
{
default:
case Rotation::ROTATION_NONE:
rtn = data;
break;
case Rotation::ROTATION_90:
rtn = data;
// TODO(dev):
// TODO(dev): x's become y's
// TODO(dev): y's become max x - x's
// TODO(dev):
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:
rtn = data;
// TODO(dev):
// TODO(dev): I mean we could just rotate 90 & then 180,
// TODO(dev): but that seems like the cheap way out.
// TODO(dev):
break;
};
return rtn;
}
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;
}
unsigned PD_PieceHeight(const Piece &piece, Rotation rotation)
{
switch(rotation)
{
default:
case Rotation::ROTATION_NONE:
case Rotation::ROTATION_180:
return pieces.at(piece).size();
break;
case Rotation::ROTATION_90:
case Rotation::ROTATION_270:
return PD_PieceHeight(RotatePieceData(pieces.at(piece), Rotation::ROTATION_90));
break;
};
}
unsigned PD_PieceWidth(const Piece &piece, Rotation rotation)
{
size_t rtn = 0;
for(auto row : pieces.at(piece))
rtn = std::max(rtn, row.size());
return rtn;
}
void PD_DrawPiece(const Piece &piece, int y, int x, Rotation rotation)
{
attrset(COLOR_PAIR(static_cast<int>(piece) % 8));
auto data = RotatePieceData(pieces.at(piece), rotation);
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,6 +2,8 @@
#define APAD_PIECES_H
#include <stdint.h>
#include <unordered_map>
#include <vector>
enum Rotation {
ROTATION_NONE,
@ -69,6 +71,75 @@ 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,
@ -76,4 +147,14 @@ 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);
unsigned PD_PieceHeight(const PieceData &data);
unsigned PD_PieceHeight(const Piece &piece,
Rotation rotation = Rotation::ROTATION_NONE);
unsigned PD_PieceWidth(const PieceData &data);
unsigned PD_PieceWidth(const Piece &piece,
Rotation rotation = Rotation::ROTATION_NONE);
void PD_DrawPiece(const Piece &piece, int y, int x,
Rotation rotation = Rotation::ROTATION_NONE);
#endif

View File

@ -38,8 +38,8 @@ int main(int argc, char *argv[])
{
try {
Piece piece = 0b111 & std::atoi(argv[input]);
DrawPiece(piece, y, 1);
y += PieceHeight(piece) + 1;
PD_DrawPiece(piece, y, 1);
y += PD_PieceHeight(piece) + 1;
} catch(const std::exception &e) {
mvaddstr(y, 1, "std::atoi error");
y += 2;
@ -105,7 +105,7 @@ void DemoPieces()
for(auto i = 0; i < 8; ++i)
{
Piece piece = static_cast<Piece>(i);
DrawPiece(piece, y, x, rotation);
PD_DrawPiece(piece, y, x, rotation);
mvaddstr(y-1, x, "Piece");
mvaddstr(y-1, x + 6, std::to_string(i + 1).c_str());
x += 12;
@ -117,17 +117,17 @@ void DemoPieces()
for(auto i = 0; i < 8; ++i)
{
Piece piece = static_cast<Piece>(i);
DrawPiece(piece, y, x, rotation);
x += PieceWidth(piece, rotation) * 2 + 2;
PD_DrawPiece(piece, y, x, rotation);
x += PD_PieceWidth(piece, rotation) * 2 + 2;
}
// Always same height apart, sharing first piece with same-width row:
x = 2;
y = 10 + PieceHeight(0, rotation) + 1;
y = 10 + PD_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;
PD_DrawPiece(piece, y, x, rotation);
y += PD_PieceHeight(piece, rotation) + 1;
}
getch();