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;
}
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++) {

View File

@@ -14,7 +14,7 @@ class Board {
std::vector<std::vector<Block>> grid; // the grid, (0,0) is downleft
std::vector<Block> 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<std::vector<Block>> 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

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)
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),
board(boardWidth, boardHeight, bag, parameters.getNextQueueLength()) {
board(boardWidth, boardHeight, piecesList, parameters.getNextQueueLength()) {
this->initialize();
}

View File

@@ -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<PiecesList>& bag);
Game(Gamemode gamemode, const Player& controls, int boardWidth, int boardHeight, const std::shared_ptr<PiecesList>& piecesList);
/**
* Starts the game

View File

@@ -11,9 +11,9 @@
#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),
generator(pieceList),
generator(piecesList),
nextQueueLength(nextQueueLength) {
this->initialize();

View File

@@ -27,7 +27,7 @@ class GameBoard {
/**
* 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

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 "GameBoard.h"
#include "PiecesList.h"
#include "../Pieces/PiecesFiles.h"
#include <chrono>
#include <string>
#include <algorithm>
#include <filesystem>
#include <fstream>
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<PiecesList> pl = std::make_shared<PiecesList>(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;
}