Files
jminos/src/Core/GameBoard.h
2025-02-25 12:07:16 +01:00

127 lines
3.8 KiB
C++

#pragma once
#include "../Pieces/Piece.h"
#include "Board.h"
#include "Bag.h"
#include "LineClear.h"
#include <Vector>
#include <memory>
/**
* Links a board with the pieces moving in it
*/
class GameBoard {
private:
Board board; // the board in which pieces moves, (0, 0) is downleft
Bag generator; // the piece generator
std::shared_ptr<Piece> activePiece; // the piece currently in the board
Cell activePiecePosition; // the position of the piece currently in the board
std::shared_ptr<Piece> heldPiece; // a piece being holded
int nextQueueLength; // the number of next pieces seeable at a time
std::vector<Piece> nextQueue; // the list of the next pieces to spawn in the board
bool isLastMoveKick; // wheter the last action the piece did was kicking
public:
/**
* Creates a new board, generator, and next queue
*/
GameBoard(int boardWidth, int boardHeight, const std::vector<Piece>& bag, int nextQueueLength);
/**
* Try moving the piece one cell to the left, and returns wheter it was sucessfull
*/
bool moveLeft();
/**
* Try moving the piece one cell to the right, and returns wheter it was sucessfull
*/
bool moveRight();
/**
* Try moving the piece one cell down, and returns wheter it was sucessfull
*/
bool moveDown();
/**
* Try rotating the piece and kicking it if necessary, and returns wheter it was sucessfull
*/
bool rotate(Rotation rotation);
private:
/**
* Try kicking the piece, testing position either above or below the piece's initial position
*/
bool tryKicking(bool testingBottom, const std::set<Cell>& safeCells);
public:
/**
* Try holding the active piece or swapping it if one was already stocked, while trying to apply an initial rotation to the newly spawned piece,
* and returns wheter it was sucessfull
*/
bool hold(Rotation initialRotation = NONE);
/**
* Spawns the next piece from the queue, and returns wheter it spawns in a wall
*/
bool spawnNextPiece();
/**
* Returns wheter the active piece is touching walls directly below it
*/
bool touchesGround();
/**
* Lock the active piece into the board and returns the resulting line clear
*/
LineClear lockPiece();
/**
* Returns a copy of the board
*/
Board getBoard() const;
/**
* Returns a copy of the active piece
*/
Piece getActivePiece() const;
/**
* Returns a copy of the position of the active piece
*/
Cell getActivePiecePosition() const;
/**
* Returns a copy of the held piece
*/
Piece getHeldPiece() const;
/**
* Returns a copy of the next piece queue
*/
std::vector<Piece> getNextPieces() const;
private:
/**
* Returns wheter the translated active piece is in a wall
*/
bool isActivePieceInWall(const Cell& shift = Cell{0, 0}) const;
/**
* Returns wheter the translated active piece overlaps with at least one of the cells
*/
bool activePieceOverlapsOneCell(const std::set<Cell>& safeCells, const Cell& shift = Cell{0, 0}) const;
/**
* Sets the active piece to its spawn position
*/
void goToSpawnPosition();
public:
/**
* Stream output operator, adds the board, the hold box and the next queue
*/
friend std::ostream& operator<<(std::ostream& os, const GameBoard& gameboard);
};