ajouté classe Menu

This commit is contained in:
2025-03-01 20:27:36 +01:00
parent d443b25de1
commit 8088894073
11 changed files with 136 additions and 48 deletions

View File

@@ -91,6 +91,10 @@ std::vector<std::vector<Block>> Board::getBlocks() const {
return this->grid; return this->grid;
} }
int Board::getWidth() const {
return this->width;
}
int Board::getGridHeight() const { int Board::getGridHeight() const {
return this->grid.size(); return this->grid.size();
} }
@@ -99,10 +103,6 @@ int Board::getBaseHeight() const {
return this->height; return this->height;
} }
int Board::getWidth() const {
return this->width;
}
std::ostream& operator<<(std::ostream& os, const Board& board) { std::ostream& operator<<(std::ostream& os, const Board& board) {
for (int y = board.grid.size() - 1; y >= 0; y--) { for (int y = board.grid.size() - 1; y >= 0; y--) {
for (int x = 0; x < board.width; x++) { for (int x = 0; x < board.width; x++) {

View File

@@ -14,7 +14,7 @@ class Board {
std::vector<std::vector<Block>> grid; // the grid, (0,0) is downleft std::vector<std::vector<Block>> grid; // the grid, (0,0) is downleft
std::vector<Block> emptyRow; // an empty row of blocks std::vector<Block> emptyRow; // an empty row of blocks
int width; // the width of the grid 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: public:
/** /**
@@ -53,6 +53,11 @@ class Board {
*/ */
std::vector<std::vector<Block>> getBlocks() const; std::vector<std::vector<Block>> getBlocks() const;
/**
* @return The width of the grid
*/
int getWidth() const;
/** /**
* @return The actual height of the grid * @return The actual height of the grid
*/ */
@@ -63,11 +68,6 @@ class Board {
*/ */
int getBaseHeight() const; int getBaseHeight() const;
/**
* @return The width of the grid
*/
int getWidth() const;
/** /**
* Stream output operator, adds a 2D grid representing the board * Stream output operator, adds a 2D grid representing the board
* @return A reference to the output stream * @return A reference to the output stream

View File

@@ -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) 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<PiecesList>& bag) : Game::Game(Gamemode gamemode, const Player& controls, int boardWidth, int boardHeight, const std::shared_ptr<PiecesList>& piecesList) :
parameters(gamemode, controls), parameters(gamemode, controls),
board(boardWidth, boardHeight, bag, parameters.getNextQueueLength()) { board(boardWidth, boardHeight, piecesList, parameters.getNextQueueLength()) {
this->initialize(); this->initialize();
} }

View File

@@ -33,7 +33,7 @@ class Game {
/** /**
* Initialize the parameters and creates a new board * Initialize the parameters and creates a new board
*/ */
Game(Gamemode gamemode, const Player& controls, int boardWidth, int boardHeight, const std::shared_ptr<PiecesList>& bag); Game(Gamemode gamemode, const Player& controls, int boardWidth, int boardHeight, const std::shared_ptr<PiecesList>& piecesList);
/** /**
* Starts the game * Starts the game

View File

@@ -11,9 +11,9 @@
#include <cstdlib> #include <cstdlib>
GameBoard::GameBoard(int boardWidth, int boardHeight, const std::shared_ptr<PiecesList>& pieceList, int nextQueueLength) : GameBoard::GameBoard(int boardWidth, int boardHeight, const std::shared_ptr<PiecesList>& piecesList, int nextQueueLength) :
board(boardWidth, boardHeight), board(boardWidth, boardHeight),
generator(pieceList), generator(piecesList),
nextQueueLength(nextQueueLength) { nextQueueLength(nextQueueLength) {
this->initialize(); this->initialize();

View File

@@ -27,7 +27,7 @@ class GameBoard {
/** /**
* Creates a new board, generator, and next queue * Creates a new board, generator, and next queue
*/ */
GameBoard(int boardWidth, int boardHeight, const std::shared_ptr<PiecesList>& bag, int nextQueueLength); GameBoard(int boardWidth, int boardHeight, const std::shared_ptr<PiecesList>& piecesList, int nextQueueLength);
/** /**
* Resets the board as if it was newly created * Resets the board as if it was newly created

46
src/Core/Menu.cpp Normal file
View File

@@ -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<PiecesList>(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;
}

61
src/Core/Menu.h Normal file
View File

@@ -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();
};

View File

@@ -1,13 +1,8 @@
#include "../Pieces/PiecesFiles.h" #include "Menu.h"
#include "../Pieces/Generator.h" #include "../Pieces/Generator.h"
#include "GameBoard.h" #include "../Pieces/PiecesFiles.h"
#include "PiecesList.h"
#include <chrono> #include <chrono>
#include <string>
#include <algorithm>
#include <filesystem>
#include <fstream>
void testGeneratorForAllSizes(int amount); void testGeneratorForAllSizes(int amount);
@@ -21,25 +16,11 @@ void readStatsFromFilesForAllSizes(int amount);
int main(int argc, char** argv) { int main(int argc, char** argv) {
std::srand(std::time(NULL)); std::srand(std::time(NULL));
/* Menu menu;
int sizeSelected = 3; menu.getPiecesList().loadPieces(4);
menu.getPiecesList().selectAllPieces(4);
PiecesList pli; Game game = menu.startGame(SPRINT);
std::shared_ptr<PiecesList> pl = std::make_shared<PiecesList>(pli); game.start();
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);
return 0; return 0;
} }

View File

@@ -11,12 +11,12 @@
Generator::Generator() { Generator::Generator() {
} }
std::vector<Polyomino> Generator::generatePolyominos(unsigned int polyominoSize) { std::vector<Polyomino> Generator::generatePolyominos(int polyominoSize) {
this->validPolyominos.clear(); this->validPolyominos.clear();
this->currentTestedShape.clear(); this->currentTestedShape.clear();
// no polyomino of size 0 // a polyomino has at least 1 square
if (polyominoSize == 0) return this->validPolyominos; if (polyominoSize < 1) return this->validPolyominos;
// start generating from the monomino // start generating from the monomino
this->currentTestedShape.insert(Position{0, 0}); this->currentTestedShape.insert(Position{0, 0});
@@ -26,7 +26,7 @@ std::vector<Polyomino> Generator::generatePolyominos(unsigned int polyominoSize)
return this->validPolyominos; return this->validPolyominos;
} }
void Generator::generate(unsigned int polyominoSize, int lastAddedPositionNumber, int nextAvaibleNumber, std::map<Position, int> candidatePositions) { void Generator::generate(int polyominoSize, int lastAddedPositionNumber, int nextAvaibleNumber, std::map<Position, int> candidatePositions) {
// recursion stop // recursion stop
if (polyominoSize == this->currentTestedShape.size()) { if (polyominoSize == this->currentTestedShape.size()) {
Polyomino candidate(this->currentTestedShape); Polyomino candidate(this->currentTestedShape);

View File

@@ -25,13 +25,13 @@ class Generator {
* Generates the list of all one-sided polyominos of the specified size * Generates the list of all one-sided polyominos of the specified size
* @return The list of polyominos * @return The list of polyominos
*/ */
std::vector<Polyomino> generatePolyominos(unsigned int polyominoSize); std::vector<Polyomino> generatePolyominos(int polyominoSize);
private: private:
/** /**
* Generates all one-sided polyominos of the specified size using the current tested shape * 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<Position, int> candidatePositions); void generate(int polyominoSize, int lastAddedPositionNumber, int nextAvaibleNumber, std::map<Position, int> candidatePositions);
/** /**
* Checks wheter a candidate position can be added to the current tested shape * Checks wheter a candidate position can be added to the current tested shape