From 808889407363fa4643e5cb1bb6d824ee2c696545 Mon Sep 17 00:00:00 2001 From: zulianc Date: Sat, 1 Mar 2025 20:27:36 +0100 Subject: [PATCH] =?UTF-8?q?ajout=C3=A9=20classe=20Menu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Core/Board.cpp | 8 +++--- src/Core/Board.h | 12 ++++---- src/Core/Game.cpp | 4 +-- src/Core/Game.h | 2 +- src/Core/GameBoard.cpp | 4 +-- src/Core/GameBoard.h | 2 +- src/Core/Menu.cpp | 46 ++++++++++++++++++++++++++++++ src/Core/Menu.h | 61 ++++++++++++++++++++++++++++++++++++++++ src/Core/main.cpp | 33 +++++----------------- src/Pieces/Generator.cpp | 8 +++--- src/Pieces/Generator.h | 4 +-- 11 files changed, 136 insertions(+), 48 deletions(-) create mode 100644 src/Core/Menu.cpp create mode 100644 src/Core/Menu.h diff --git a/src/Core/Board.cpp b/src/Core/Board.cpp index 109a9f7..6d835b5 100644 --- a/src/Core/Board.cpp +++ b/src/Core/Board.cpp @@ -91,6 +91,10 @@ std::vector> Board::getBlocks() const { return this->grid; } +int Board::getWidth() const { + return this->width; +} + int Board::getGridHeight() const { return this->grid.size(); } @@ -99,10 +103,6 @@ int Board::getBaseHeight() const { return this->height; } -int Board::getWidth() const { - return this->width; -} - std::ostream& operator<<(std::ostream& os, const Board& board) { for (int y = board.grid.size() - 1; y >= 0; y--) { for (int x = 0; x < board.width; x++) { diff --git a/src/Core/Board.h b/src/Core/Board.h index c92a9b2..ad765b0 100644 --- a/src/Core/Board.h +++ b/src/Core/Board.h @@ -14,7 +14,7 @@ class Board { std::vector> grid; // the grid, (0,0) is downleft std::vector emptyRow; // an empty row of blocks int width; // the width of the grid - int height; // the base height of the grid, which can extends indefinitely + int height; // the base height of the grid, which can extend indefinitely public: /** @@ -53,6 +53,11 @@ class Board { */ std::vector> getBlocks() const; + /** + * @return The width of the grid + */ + int getWidth() const; + /** * @return The actual height of the grid */ @@ -63,11 +68,6 @@ class Board { */ int getBaseHeight() const; - /** - * @return The width of the grid - */ - int getWidth() const; - /** * Stream output operator, adds a 2D grid representing the board * @return A reference to the output stream diff --git a/src/Core/Game.cpp b/src/Core/Game.cpp index 8eddfcf..fa095f6 100644 --- a/src/Core/Game.cpp +++ b/src/Core/Game.cpp @@ -16,9 +16,9 @@ static const int B2B_SCORE_MULTIPLIER = 2; // by how much havaing B2B on mult static const int B2B_MIN_LINE_NUMBER = 4; // the minimum number of lines needed to be cleared at once to gain B2B (without a spin) -Game::Game(Gamemode gamemode, const Player& controls, int boardWidth, int boardHeight, const std::shared_ptr& bag) : +Game::Game(Gamemode gamemode, const Player& controls, int boardWidth, int boardHeight, const std::shared_ptr& piecesList) : parameters(gamemode, controls), - board(boardWidth, boardHeight, bag, parameters.getNextQueueLength()) { + board(boardWidth, boardHeight, piecesList, parameters.getNextQueueLength()) { this->initialize(); } diff --git a/src/Core/Game.h b/src/Core/Game.h index e827f7a..0509c9d 100644 --- a/src/Core/Game.h +++ b/src/Core/Game.h @@ -33,7 +33,7 @@ class Game { /** * Initialize the parameters and creates a new board */ - Game(Gamemode gamemode, const Player& controls, int boardWidth, int boardHeight, const std::shared_ptr& bag); + Game(Gamemode gamemode, const Player& controls, int boardWidth, int boardHeight, const std::shared_ptr& piecesList); /** * Starts the game diff --git a/src/Core/GameBoard.cpp b/src/Core/GameBoard.cpp index 9a3a04f..37af858 100644 --- a/src/Core/GameBoard.cpp +++ b/src/Core/GameBoard.cpp @@ -11,9 +11,9 @@ #include -GameBoard::GameBoard(int boardWidth, int boardHeight, const std::shared_ptr& pieceList, int nextQueueLength) : +GameBoard::GameBoard(int boardWidth, int boardHeight, const std::shared_ptr& piecesList, int nextQueueLength) : board(boardWidth, boardHeight), - generator(pieceList), + generator(piecesList), nextQueueLength(nextQueueLength) { this->initialize(); diff --git a/src/Core/GameBoard.h b/src/Core/GameBoard.h index 6d34cab..7ce5fd8 100644 --- a/src/Core/GameBoard.h +++ b/src/Core/GameBoard.h @@ -27,7 +27,7 @@ class GameBoard { /** * Creates a new board, generator, and next queue */ - GameBoard(int boardWidth, int boardHeight, const std::shared_ptr& bag, int nextQueueLength); + GameBoard(int boardWidth, int boardHeight, const std::shared_ptr& piecesList, int nextQueueLength); /** * Resets the board as if it was newly created diff --git a/src/Core/Menu.cpp b/src/Core/Menu.cpp new file mode 100644 index 0000000..24c6037 --- /dev/null +++ b/src/Core/Menu.cpp @@ -0,0 +1,46 @@ +#include "Menu.h" + +#include "PiecesList.h" +#include "Player.h" +#include "Game.h" + + +Menu::Menu() { + // default board size + this->boardHeight = 20; + this->boardWidth = 10; +} + +Game Menu::startGame(Gamemode gamemode) { + return Game(gamemode, this->playerControls, this->boardWidth, this->boardHeight, std::make_shared(this->piecesList)); +} + +bool Menu::setBoardWidth(int width) { + if (width < 1) return false; + + this->boardWidth = width; + return true; +} + +bool Menu::setBoardHeight(int height) { + if (height < 1) return false; + + this->boardHeight = height; + return true; +} + +int Menu::getBoardWidth() { + return this->boardWidth; +} + +int Menu::getBoardHeight() { + return this->boardHeight; +} + +Player& Menu::getPlayerControls() { + return this->playerControls; +} + +PiecesList& Menu::getPiecesList() { + return this->piecesList; +} diff --git a/src/Core/Menu.h b/src/Core/Menu.h new file mode 100644 index 0000000..2b3e203 --- /dev/null +++ b/src/Core/Menu.h @@ -0,0 +1,61 @@ +#pragma once + +#include "PiecesList.h" +#include "Player.h" +#include "Game.h" + + +/** + * The interface between the UI and the core of the game + */ +class Menu { + private: + PiecesList piecesList; // the list of pieces in the game + Player playerControls; // the controls of the player + int boardHeight; // the height of the board for the next game + int boardWidth; // the width of the board for the next game + + public: + /** + * Initializes the board size and player controls to their default values + */ + Menu(); + + /** + * Starts a new game with the current settings + * @return The game that has been created + */ + Game startGame(Gamemode gamemode); + + /** + * Sets the width of the board, which must be greater than 0 + * @return If the width has been changed + */ + bool setBoardWidth(int width); + + /** + * Sets the height of the board, which must be greater than 0 + * @return If the height has been changed + */ + bool setBoardHeight(int height); + + /** + * @return The width of the board + */ + int getBoardWidth(); + + /** + * @return The height of the board + */ + int getBoardHeight(); + + /** + * @return A reference to the player's controls + */ + Player& getPlayerControls(); + + /** + * @return A reference to the pieces list + */ + PiecesList& getPiecesList(); +}; diff --git a/src/Core/main.cpp b/src/Core/main.cpp index 6f93e6c..33cdafa 100644 --- a/src/Core/main.cpp +++ b/src/Core/main.cpp @@ -1,13 +1,8 @@ -#include "../Pieces/PiecesFiles.h" +#include "Menu.h" #include "../Pieces/Generator.h" -#include "GameBoard.h" -#include "PiecesList.h" +#include "../Pieces/PiecesFiles.h" #include -#include -#include -#include -#include void testGeneratorForAllSizes(int amount); @@ -21,25 +16,11 @@ void readStatsFromFilesForAllSizes(int amount); int main(int argc, char** argv) { std::srand(std::time(NULL)); - /* - int sizeSelected = 3; - - PiecesList pli; - std::shared_ptr pl = std::make_shared(pli); - pl->loadPieces(sizeSelected); - for (int i = 0; i < pl->getNumberOfPiecesOfOneSize(sizeSelected); i++) { - pl->selectPieces(sizeSelected, i); - } - - GameBoard gb(10, 4, pl, 1); - - for (int i = 0; i < pl->getNumberOfPiecesOfOneSize(sizeSelected) * 3; i++) { - gb.spawnNextPiece(); - std::cout << gb << std::endl; - } - */ - - testGeneratorForOneSize(11); + Menu menu; + menu.getPiecesList().loadPieces(4); + menu.getPiecesList().selectAllPieces(4); + Game game = menu.startGame(SPRINT); + game.start(); return 0; } diff --git a/src/Pieces/Generator.cpp b/src/Pieces/Generator.cpp index d6229b4..b681d8c 100644 --- a/src/Pieces/Generator.cpp +++ b/src/Pieces/Generator.cpp @@ -11,12 +11,12 @@ Generator::Generator() { } -std::vector Generator::generatePolyominos(unsigned int polyominoSize) { +std::vector Generator::generatePolyominos(int polyominoSize) { this->validPolyominos.clear(); this->currentTestedShape.clear(); - // no polyomino of size 0 - if (polyominoSize == 0) return this->validPolyominos; + // a polyomino has at least 1 square + if (polyominoSize < 1) return this->validPolyominos; // start generating from the monomino this->currentTestedShape.insert(Position{0, 0}); @@ -26,7 +26,7 @@ std::vector Generator::generatePolyominos(unsigned int polyominoSize) return this->validPolyominos; } -void Generator::generate(unsigned int polyominoSize, int lastAddedPositionNumber, int nextAvaibleNumber, std::map candidatePositions) { +void Generator::generate(int polyominoSize, int lastAddedPositionNumber, int nextAvaibleNumber, std::map candidatePositions) { // recursion stop if (polyominoSize == this->currentTestedShape.size()) { Polyomino candidate(this->currentTestedShape); diff --git a/src/Pieces/Generator.h b/src/Pieces/Generator.h index 5c6cee7..00aaffc 100644 --- a/src/Pieces/Generator.h +++ b/src/Pieces/Generator.h @@ -25,13 +25,13 @@ class Generator { * Generates the list of all one-sided polyominos of the specified size * @return The list of polyominos */ - std::vector generatePolyominos(unsigned int polyominoSize); + std::vector generatePolyominos(int polyominoSize); private: /** * Generates all one-sided polyominos of the specified size using the current tested shape */ - void generate(unsigned int polyominoSize, int lastAddedPositionNumber, int nextAvaibleNumber, std::map candidatePositions); + void generate(int polyominoSize, int lastAddedPositionNumber, int nextAvaibleNumber, std::map candidatePositions); /** * Checks wheter a candidate position can be added to the current tested shape