Les couleurs #1
10
.vscode/c_cpp_properties.json
vendored
Normal file
10
.vscode/c_cpp_properties.json
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Tetris",
|
||||
"cppStandard": "c++20",
|
||||
"compileCommands": ".vscode/compile_commands.json"
|
||||
}
|
||||
],
|
||||
"version": 4
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
#include "../Pieces/Piece.h"
|
||||
|
||||
#include <Vector>
|
||||
#include <vector>
|
||||
#include <cstdlib>
|
||||
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
#include "../Pieces/Piece.h"
|
||||
|
||||
#include <Vector>
|
||||
#include <vector>
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,13 +2,13 @@
|
||||
|
||||
#include "../Pieces/Piece.h"
|
||||
|
||||
#include <Vector>
|
||||
#include <Set>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
Board::Board(int width, int height) : width(width), height(height) {
|
||||
std::vector<Color> emptyRow;
|
||||
std::vector<ColorEnum> emptyRow;
|
||||
for (int i = 0; i < width; i ++) {
|
||||
emptyRow.push_back(NOTHING);
|
||||
}
|
||||
@@ -20,13 +20,13 @@ Board::Board(int width, int height) : width(width), height(height) {
|
||||
}
|
||||
}
|
||||
|
||||
void Board::addBlock(const Cell& position, Color block) {
|
||||
void Board::addBlock(const Cell& position, ColorEnum block) {
|
||||
// if the block is out of bounds we discard it
|
||||
if (position.x < 0 || position.x >= this->width || position.y < 0) return;
|
||||
|
||||
// resize the grid if needed
|
||||
if (position.y >= this->grid.size()) {
|
||||
std::vector<Color> emptyRow;
|
||||
std::vector<ColorEnum> emptyRow;
|
||||
for (int i = 0; i < width; i ++) {
|
||||
emptyRow.push_back(NOTHING);
|
||||
}
|
||||
@@ -40,7 +40,7 @@ void Board::addBlock(const Cell& position, Color block) {
|
||||
}
|
||||
|
||||
int Board::clearRows() {
|
||||
std::vector<Color> emptyRow;
|
||||
std::vector<ColorEnum> emptyRow;
|
||||
for (int i = 0; i < width; i ++) {
|
||||
emptyRow.push_back(NOTHING);
|
||||
}
|
||||
@@ -67,18 +67,20 @@ int Board::clearRows() {
|
||||
return clearedLines;
|
||||
}
|
||||
|
||||
Color Board::getBlock(const Cell& position) const {
|
||||
ColorEnum Board::getBlock(const Cell& position) const {
|
||||
// if the block is out of bounds
|
||||
if (position.x < 0 || position.x >= this->width || position.y < 0) return OUT_OF_BOUND;
|
||||
if (position.x < 0 || position.x >= this->width || position.y < 0)
|
||||
return OUT_OF_BOUNDS;
|
||||
|
||||
// if the block is higher than the current grid, since it can grow indefinitely we do as if it was there but empty
|
||||
if (position.y >= this->grid.size()) return NOTHING;
|
||||
if (position.y >= this->grid.size())
|
||||
return NOTHING;
|
||||
|
||||
// else get the color in the grid
|
||||
return this->grid.at(position.y).at(position.x);
|
||||
}
|
||||
|
||||
std::vector<std::vector<Color>> Board::getBlocks() const {
|
||||
std::vector<std::vector<ColorEnum>> Board::getBlocks() const {
|
||||
return this->grid;
|
||||
}
|
||||
|
||||
@@ -98,8 +100,8 @@ std::ostream& operator<<(std::ostream& os, const Board& board) {
|
||||
// print the board
|
||||
for (int y = board.grid.size() - 1; y >= 0; y--) {
|
||||
for (int x = 0; x < board.width; x++) {
|
||||
Color block = board.grid.at(y).at(x);
|
||||
os << COLOR_CODES[block];
|
||||
ColorEnum block = board.grid.at(y).at(x);
|
||||
os << getColorCode(block);
|
||||
if (block != NOTHING) {
|
||||
os << "*";
|
||||
}
|
||||
@@ -111,7 +113,7 @@ std::ostream& operator<<(std::ostream& os, const Board& board) {
|
||||
}
|
||||
|
||||
// reset console color
|
||||
os << COLOR_RESET;
|
||||
os << getColorCode(NOTHING);
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
#include "../Pieces/Piece.h"
|
||||
|
||||
#include <Vector>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
*/
|
||||
class Board {
|
||||
private:
|
||||
std::vector<std::vector<Color>> grid; // the grid, (0,0) is downleft
|
||||
std::vector<std::vector<ColorEnum>> grid; // the grid, (0,0) is downleft
|
||||
int width; // the width of the grid
|
||||
int height; // the base height of the grid, which can extends indefinitely
|
||||
|
||||
@@ -24,7 +24,7 @@ class Board {
|
||||
/**
|
||||
* Change the color of the specified block, if the block is out of bounds it is simply ignored
|
||||
*/
|
||||
void addBlock(const Cell& position, Color block);
|
||||
void addBlock(const Cell& position, ColorEnum block);
|
||||
|
||||
/**
|
||||
* Clears any complete row and moves down the rows on top, returns the number of cleared rows
|
||||
@@ -34,12 +34,12 @@ class Board {
|
||||
/**
|
||||
* Returns the color of the block at the specified position
|
||||
*/
|
||||
Color getBlock(const Cell& position) const;
|
||||
ColorEnum getBlock(const Cell& position) const;
|
||||
|
||||
/**
|
||||
* Returns a copy of the grid
|
||||
*/
|
||||
std::vector<std::vector<Color>> getBlocks() const;
|
||||
std::vector<std::vector<ColorEnum>> getBlocks() const;
|
||||
|
||||
/**
|
||||
* Returns the actual height of the grid
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include "GameParameters.h"
|
||||
#include "Action.h"
|
||||
|
||||
#include <Vector>
|
||||
#include <vector>
|
||||
|
||||
static const int SUBPX_PER_ROW = 60; // the number of position the active piece can take "between" two rows
|
||||
static const int SOFT_DROP_SCORE = 1; // the score gained by line soft dropped
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include "GameParameters.h"
|
||||
#include "Action.h"
|
||||
|
||||
#include <Vector>
|
||||
#include <vector>
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
#include "Bag.h"
|
||||
#include "LineClear.h"
|
||||
|
||||
#include <Vector>
|
||||
#include <Set>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <memory>
|
||||
|
||||
|
||||
@@ -264,7 +264,8 @@ std::vector<Piece> GameBoard::getNextPieces() const {
|
||||
bool GameBoard::isActivePieceInWall(const Cell& shift) const {
|
||||
// check if every cell of the active piece is in an empty spot
|
||||
for (Cell cell : this->activePiece->getPositions()) {
|
||||
if (this->board.getBlock(cell + this->activePiecePosition + shift) != NOTHING) return true;
|
||||
if (this->board.getBlock(cell + this->activePiecePosition + shift) != NOTHING)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -296,8 +297,8 @@ std::ostream& operator<<(std::ostream& os, const GameBoard& gameboard) {
|
||||
if (gameboard.activePiece != nullptr) {
|
||||
|
||||
// change to the color of the active piece
|
||||
Color pieceColor = gameboard.activePiece->getColor();
|
||||
os << COLOR_CODES[pieceColor];
|
||||
ColorEnum pieceColor = gameboard.activePiece->getColor();
|
||||
os << getColorCode(pieceColor);
|
||||
|
||||
// print only the cell were the active piece is
|
||||
for (int y = gameboard.activePiecePosition.y + gameboard.activePiece->getLength() - 1; y >= gameboard.board.getBaseHeight(); y--) {
|
||||
@@ -315,21 +316,21 @@ std::ostream& operator<<(std::ostream& os, const GameBoard& gameboard) {
|
||||
}
|
||||
|
||||
// print the board
|
||||
Color pieceColor = (gameboard.activePiece == nullptr) ? NOTHING : gameboard.activePiece->getColor();
|
||||
ColorEnum pieceColor = (gameboard.activePiece == nullptr) ? NOTHING : gameboard.activePiece->getColor();
|
||||
for (int y = gameboard.board.getBaseHeight() - 1; y >= 0; y--) {
|
||||
for (int x = 0; x < gameboard.board.getWidth(); x++) {
|
||||
bool hasActivePiece = (gameboard.activePiece == nullptr) ? false : gameboard.activePiece->getPositions().contains(Cell{x, y} - gameboard.activePiecePosition);
|
||||
|
||||
// if the active piece is on this cell, print it
|
||||
if (hasActivePiece) {
|
||||
os << COLOR_CODES[pieceColor];
|
||||
os << getColorCode(pieceColor);
|
||||
os << "*";
|
||||
}
|
||||
|
||||
// else print the cell of the board
|
||||
else {
|
||||
Color block = gameboard.board.getBlock(Cell{x, y});
|
||||
os << COLOR_CODES[block];
|
||||
ColorEnum block = gameboard.board.getBlock(Cell{x, y});
|
||||
os << getColorCode(block);
|
||||
if (block != NOTHING) {
|
||||
os << "*";
|
||||
}
|
||||
@@ -354,7 +355,7 @@ std::ostream& operator<<(std::ostream& os, const GameBoard& gameboard) {
|
||||
}
|
||||
|
||||
// reset console color
|
||||
os << COLOR_RESET;
|
||||
os << getColorCode(NOTHING);
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include "Bag.h"
|
||||
#include "LineClear.h"
|
||||
|
||||
#include <Vector>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
|
||||
|
||||
38
src/Pieces/Color.cpp
Normal file
38
src/Pieces/Color.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#include "Color.h"
|
||||
|
||||
static const Color C_NOTHING = {255, 255, 255};
|
||||
static const Color C_OUT_OF_BOUNDS = C_NOTHING;
|
||||
static const Color C_GARBAGE = {150, 150, 150};
|
||||
static const Color C_PURPLE = {150, 0, 255};
|
||||
static const Color C_ORANGE = {255, 150, 0};
|
||||
static const Color C_CYAN = {0, 255, 0};
|
||||
static const Color C_PINK = {255, 0, 200};
|
||||
static const Color C_YELLOW = {255, 255, 0};
|
||||
static const Color C_RED = {255, 0, 0};
|
||||
static const Color C_BLUE = {0, 100, 255};
|
||||
static const Color C_GREEN = {0, 255, 0};
|
||||
|
||||
static const Color colors[] = {
|
||||
C_NOTHING,
|
||||
C_OUT_OF_BOUNDS,
|
||||
C_GARBAGE,
|
||||
C_PURPLE,
|
||||
C_ORANGE,
|
||||
C_CYAN,
|
||||
C_PINK,
|
||||
C_YELLOW,
|
||||
C_RED,
|
||||
C_BLUE,
|
||||
C_GREEN
|
||||
};
|
||||
|
||||
const Color& getColor(ColorEnum color) {
|
||||
return colors[color];
|
||||
}
|
||||
|
||||
void setNextPieceColor(ColorEnum& color) {
|
||||
if (color == GREEN)
|
||||
color = PURPLE;
|
||||
else
|
||||
color = ColorEnum(color + 1);
|
||||
}
|
||||
@@ -1,14 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <String>
|
||||
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* Every possible colors a block can take
|
||||
*/
|
||||
enum Color {
|
||||
enum ColorEnum {
|
||||
NOTHING,
|
||||
OUT_OF_BOUND,
|
||||
OUT_OF_BOUNDS,
|
||||
GARBAGE,
|
||||
PURPLE,
|
||||
ORANGE,
|
||||
@@ -20,32 +19,30 @@ enum Color {
|
||||
GREEN
|
||||
};
|
||||
|
||||
struct Color {
|
||||
std::uint8_t r;
|
||||
std::uint8_t g;
|
||||
std::uint8_t b;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns the first color a piece can take
|
||||
*/
|
||||
inline Color firstPieceColor() {
|
||||
return Color(PURPLE);
|
||||
inline ColorEnum firstPieceColor() {
|
||||
return PURPLE;
|
||||
}
|
||||
|
||||
const Color& getColor(ColorEnum color);
|
||||
|
||||
/**
|
||||
* Sets the color to the next available piece color
|
||||
*/
|
||||
inline void nextPieceColor(Color& color) {
|
||||
color = (color == GREEN) ? PURPLE : Color(color + 1);
|
||||
void setNextPieceColor(ColorEnum& color);
|
||||
|
||||
inline std::string getColorCode(const Color& color) {
|
||||
return "\033[38;2;" + std::to_string(color.r) + ";" + std::to_string(color.g) + ";" + std::to_string(color.b) + "m";
|
||||
}
|
||||
|
||||
static const std::string COLOR_RESET = "\033[38;2;255;255;255m"; // color code to reset the console color
|
||||
static const std::string COLOR_CODES[] = { // color codes to change the console color
|
||||
COLOR_RESET, // NOTHING
|
||||
COLOR_RESET, // OUT_OF_BOUND
|
||||
"\033[38;2;150;150;150m", // GARBAGE
|
||||
"\033[38;2;150;0;255m", // PURPLE
|
||||
"\033[38;2;255;150;0m", // ORANGE
|
||||
"\033[38;2;0;255;255m", // CYAN
|
||||
"\033[38;2;255;0;200m", // PINK
|
||||
"\033[38;2;255;255;0m", // YELLOW
|
||||
"\033[38;2;255;0;0m", // RED
|
||||
"\033[38;2;0;100;255m", // BLUE
|
||||
"\033[38;2;0;255;0m" // GREEN
|
||||
};
|
||||
inline std::string getColorCode(ColorEnum color) {
|
||||
return getColorCode(color);
|
||||
}
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
#include "Polyomino.h"
|
||||
|
||||
#include <Vector>
|
||||
#include <Set>
|
||||
#include <Map>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <map>
|
||||
#include <algorithm>
|
||||
|
||||
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
#include "Polyomino.h"
|
||||
|
||||
#include <Vector>
|
||||
#include <Set>
|
||||
#include <Map>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <map>
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,11 +4,11 @@
|
||||
#include "Rotation.h"
|
||||
#include "Color.h"
|
||||
|
||||
#include <Set>
|
||||
#include <String>
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
|
||||
Piece::Piece(const Polyomino& polyomino, Color color) : polyomino(polyomino), color(color) {
|
||||
Piece::Piece(const Polyomino& polyomino, ColorEnum color) : polyomino(polyomino), color(color) {
|
||||
}
|
||||
|
||||
void Piece::rotate(Rotation rotation) {
|
||||
@@ -28,11 +28,11 @@ int Piece::getLength() const {
|
||||
return this->polyomino.getLength();
|
||||
}
|
||||
|
||||
Color Piece::getColor() const {
|
||||
ColorEnum Piece::getColor() const {
|
||||
return this->color;
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const Piece& piece) {
|
||||
os << COLOR_CODES[piece.color] << piece.polyomino << COLOR_RESET;
|
||||
os << getColorCode(piece.color) << piece.polyomino << getColorCode(NOTHING);
|
||||
return os;
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include "Rotation.h"
|
||||
#include "Color.h"
|
||||
|
||||
#include <Set>
|
||||
#include <set>
|
||||
|
||||
|
||||
/**
|
||||
@@ -13,13 +13,13 @@
|
||||
class Piece {
|
||||
private:
|
||||
Polyomino polyomino; // a polyomino representing the piece, (0, 0) is downleft
|
||||
Color color; // the color of the piece
|
||||
ColorEnum color; // the color of the piece
|
||||
|
||||
public:
|
||||
/**
|
||||
* Creates a piece with a specified shape and color
|
||||
*/
|
||||
Piece(const Polyomino& piece, Color color);
|
||||
Piece(const Polyomino& piece, ColorEnum color);
|
||||
|
||||
/**
|
||||
* Rotates the piece in the specified direction
|
||||
@@ -39,7 +39,7 @@ class Piece {
|
||||
/**
|
||||
* Returns the color of the piece
|
||||
*/
|
||||
Color getColor() const;
|
||||
ColorEnum getColor() const;
|
||||
|
||||
/**
|
||||
* Stream output operator, adds a 2D grid representing the piece
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
#include "Generator.h"
|
||||
#include "Piece.h"
|
||||
|
||||
#include <Vector>
|
||||
#include <String>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <filesystem>
|
||||
@@ -38,8 +38,9 @@ bool PiecesFiles::savePieces(int order) const {
|
||||
std::sort(nMinos.begin(), nMinos.end());
|
||||
|
||||
// write pieces
|
||||
Color pieceColor = firstPieceColor();
|
||||
for (int i = 0; i < order; i++) nextPieceColor(pieceColor);
|
||||
ColorEnum pieceColor = firstPieceColor();
|
||||
for (int i = 0; i < order; i++)
|
||||
setNextPieceColor(pieceColor);
|
||||
for (const Polyomino& nMino : nMinos) {
|
||||
// write polyomino length
|
||||
char lengthByte = nMino.getLength();
|
||||
@@ -47,7 +48,7 @@ bool PiecesFiles::savePieces(int order) const {
|
||||
|
||||
// write the type and color of the piece
|
||||
char infoByte = (nMino.isConvex() << 7) + (nMino.hasHole() << 6) + pieceColor;
|
||||
nextPieceColor(pieceColor);
|
||||
setNextPieceColor(pieceColor);
|
||||
piecesFile.write(&infoByte, 1);
|
||||
|
||||
// write the cells of the piece
|
||||
@@ -96,7 +97,7 @@ bool PiecesFiles::loadPieces(int order, std::vector<Piece>& pieces, std::vector<
|
||||
piecesFile.get(infoByte);
|
||||
bool isConvex = (infoByte & convexMask) >> 7;
|
||||
bool hasHole = (infoByte & holeMask) >> 6;
|
||||
Color color = Color(infoByte & colorMask);
|
||||
ColorEnum color = ColorEnum(infoByte & colorMask);
|
||||
|
||||
// read cells
|
||||
std::set<Cell> pieceCells;
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
#include "Piece.h"
|
||||
|
||||
#include <Vector>
|
||||
#include <String>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
#include "Cell.h"
|
||||
|
||||
#include <Vector>
|
||||
#include <Set>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <iostream>
|
||||
#include <climits>
|
||||
#include <algorithm>
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
#include "Cell.h"
|
||||
|
||||
#include <Vector>
|
||||
#include <Set>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user