Show all pieces and their rotations in demo mode. Need to add FLIP.

This commit is contained in:
David Vereb 2023-04-02 12:31:01 -04:00
parent 1c24ed4670
commit 10c06d8103
4 changed files with 303 additions and 4 deletions

View File

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

239
Pieces.cpp Normal file
View File

@ -0,0 +1,239 @@
#include "Pieces.h"
#include <curses.h>
#include <stdlib.h> // exit()
unsigned PieceHeight(const Piece &piece)
{
switch(piece)
{
case PIECE_ONE:
return 2;
break;
case PIECE_TWO:
return 4;
break;
case PIECE_THREE:
return 4;
break;
case PIECE_FOUR:
return 3;
break;
case PIECE_FIVE:
return 3;
break;
case PIECE_SIX:
return 2;
break;
case PIECE_SEVEN:
return 3;
break;
case PIECE_EIGHT:
return 3;
break;
}
std::exit(-1);
}
void DrawPiece(const Piece &piece, int y, int x, Rotation rotation)
{
attrset(COLOR_PAIR(static_cast<int>(piece) % 8));
switch(piece)
{
case PIECE_ONE:
switch(rotation)
{
default:
case ROTATION_NONE:
case ROTATION_180:
mvaddstr(y, x, "[][][]");
mvaddstr(y + 1, x, "[][][]");
break;
case ROTATION_90:
case ROTATION_270:
mvaddstr(y, x, "[][]");
mvaddstr(y + 1, x, "[][]");
mvaddstr(y + 2, x, "[][]");
break;
};
break;
case PIECE_TWO:
switch(rotation)
{
default:
case ROTATION_NONE:
mvaddstr(y, x, "[]");
mvaddstr(y + 1, x, "[][]");
mvaddstr(y + 2, x, "[]");
mvaddstr(y + 3, x, "[]");
break;
case ROTATION_90:
mvaddstr(y, x, "[][][][]");
mvaddstr(y + 1, x + 4, "[]");
break;
case ROTATION_180:
mvaddstr(y, x + 2, "[]");
mvaddstr(y + 1, x + 2, "[]");
mvaddstr(y + 2, x, "[][]");
mvaddstr(y + 3, x + 2, "[]");
break;
case ROTATION_270:
mvaddstr(y, x + 2, "[]");
mvaddstr(y + 1, x, "[][][][]");
break;
};
break;
case PIECE_THREE:
switch(rotation)
{
default:
case ROTATION_NONE:
mvaddstr(y, x + 2, "[]");
mvaddstr(y + 1, x + 2, "[]");
mvaddstr(y + 2, x + 2, "[]");
mvaddstr(y + 3, x, "[][]");
break;
case ROTATION_90:
mvaddstr(y, x, "[]");
mvaddstr(y + 1, x, "[][][][]");
break;
case ROTATION_180:
mvaddstr(y, x, "[][]");
mvaddstr(y + 1, x, "[]");
mvaddstr(y + 2, x, "[]");
mvaddstr(y + 3, x, "[]");
break;
case ROTATION_270:
mvaddstr(y, x, "[][][][]");
mvaddstr(y + 1, x + 6, "[]");
break;
};
break;
case PIECE_FOUR:
switch(rotation)
{
default:
case ROTATION_NONE:
mvaddstr(y, x, "[][]");
mvaddstr(y + 1, x, "[]");
mvaddstr(y + 2, x, "[][]");
break;
case ROTATION_90:
mvaddstr(y, x, "[][][]");
mvaddstr(y + 1, x, "[]");
mvaddstr(y + 1, x + 4, "[]"); // same line as above
break;
case ROTATION_180:
mvaddstr(y, x, "[][]");
mvaddstr(y + 1, x + 2, "[]");
mvaddstr(y + 2, x, "[][]");
break;
case ROTATION_270:
mvaddstr(y, x, "[]");
mvaddstr(y, x + 4, "[]"); // same line as above
mvaddstr(y + 1, x, "[][][]");
break;
};
break;
case PIECE_FIVE:
switch(rotation)
{
default:
case ROTATION_NONE:
case ROTATION_180:
mvaddstr(y, x, "[][]");
mvaddstr(y + 1, x + 2, "[]");
mvaddstr(y + 2, x + 2, "[][]");
break;
case ROTATION_90:
case ROTATION_270:
mvaddstr(y, x + 4, "[]");
mvaddstr(y + 1, x, "[][][]");
mvaddstr(y + 2, x, "[]");
break;
};
break;
case PIECE_SIX:
switch(rotation)
{
default:
case ROTATION_NONE:
mvaddstr(y, x, "[][]");
mvaddstr(y + 1, x + 2, "[][][]");
break;
case ROTATION_90:
mvaddstr(y, x + 2, "[]");
mvaddstr(y + 1, x, "[][]");
mvaddstr(y + 2, x, "[]");
mvaddstr(y + 3, x, "[]");
break;
case ROTATION_180:
mvaddstr(y, x, "[][][]");
mvaddstr(y + 1, x + 4, "[][]");
break;
case ROTATION_270:
mvaddstr(y, x + 2, "[]");
mvaddstr(y + 1, x + 2, "[]");
mvaddstr(y + 2, x, "[][]");
mvaddstr(y + 3, x, "[]");
break;
};
break;
case PIECE_SEVEN:
switch(rotation)
{
default:
case ROTATION_NONE:
mvaddstr(y, x, "[][]");
mvaddstr(y + 1, x, "[][]");
mvaddstr(y + 2, x, "[]");
break;
case ROTATION_90:
mvaddstr(y, x, "[][][]");
mvaddstr(y + 1, x + 2, "[][]");
break;
case ROTATION_180:
mvaddstr(y, x + 2, "[]");
mvaddstr(y + 1, x, "[][]");
mvaddstr(y + 2, x, "[][]");
break;
case ROTATION_270:
mvaddstr(y, x, "[][]");
mvaddstr(y + 1, x, "[][][]");
break;
};
break;
case PIECE_EIGHT:
switch(rotation)
{
default:
case ROTATION_NONE:
mvaddstr(y, x + 4, "[]");
mvaddstr(y + 1, x + 4, "[]");
mvaddstr(y + 2, x, "[][][]");
break;
case ROTATION_90:
mvaddstr(y, x, "[]");
mvaddstr(y + 1, x, "[]");
mvaddstr(y + 2, x, "[][][]");
break;
case ROTATION_180:
mvaddstr(y, x, "[][][]");
mvaddstr(y + 1, x, "[]");
mvaddstr(y + 2, x, "[]");
break;
case ROTATION_270:
mvaddstr(y, x, "[][][]");
mvaddstr(y + 1, x + 4, "[]");
mvaddstr(y + 2, x + 4, "[]");
break;
};
break;
default:
std::exit(-1);
break;
}
}

View File

@ -3,6 +3,13 @@
#include <stdint.h>
enum Rotation {
ROTATION_NONE,
ROTATION_90,
ROTATION_180,
ROTATION_270,
};
#define NUM_PIECES 8
typedef uint8_t Piece; // only need literally 8, not 8 bits, but hey.
@ -20,7 +27,7 @@ typedef uint8_t Piece; // only need literally 8, not 8 bits, but hey.
* []
*/
#define PIECE_TRHEE 0x2
#define PIECE_THREE 0x2
/*
* []
* []
@ -62,6 +69,8 @@ typedef uint8_t Piece; // only need literally 8, not 8 bits, but hey.
* [][][]
*/
void DrawPiece(const Piece &piece, int y, int x);
unsigned PieceHeight(const Piece &piece);
void DrawPiece(const Piece &piece, int y, int x,
Rotation rotation = Rotation::ROTATION_NONE);
#endif

View File

@ -3,12 +3,16 @@
#include <curses.h>
#include <signal.h>
#include <set>
#include <string>
#include <unordered_map>
static void finish(int sig);
void init_colors();
// DEBUG:
void DemoPieces();
int main(int argc, char *argv[])
{
initscr(); /* initialize the curses library */
@ -20,6 +24,29 @@ int main(int argc, char *argv[])
if(has_colors())
init_colors();
if(argc == 2 && std::string(argv[1]) == "-d")
{
DemoPieces();
finish(0);
}
// for each piece passed in, render it:
{
int y = 1;
for(auto input = 1; input < argc; ++input)
{
try {
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 += 2;
}
}
}
int num = 0;
while(true)
{
@ -36,6 +63,7 @@ int main(int argc, char *argv[])
static void finish(int sig)
{
endwin();
std::exit(0);
}
void init_colors()
@ -56,3 +84,26 @@ void init_colors()
init_pair(6, COLOR_MAGENTA, COLOR_BLACK);
init_pair(7, COLOR_WHITE, COLOR_BLACK);
}
void DemoPieces()
{
std::set rotations = {
Rotation::ROTATION_NONE,
Rotation::ROTATION_90,
Rotation::ROTATION_180,
Rotation::ROTATION_270,
};
for(auto rotation : rotations)
{
clear();
int x = 1;
for(auto i = 0; i < 8; ++i)
{
Piece piece = static_cast<Piece>(i);
DrawPiece(piece, 2, x, rotation);
x += 12;
}
getch();
}
move(8, 2);
}