#pragma once #include "../Pieces/Piece.h" #include "Board.h" #include "Bag.h" #include "LineClear.h" #include #include /** * 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 activePiece; // the piece currently in the board Cell activePiecePosition; // the position of the piece currently in the board std::shared_ptr heldPiece; // a piece being holded int nextQueueLength; // the number of next pieces seeable at a time std::vector 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& 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& 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 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& 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); };