actually move pawns, add some helper functions, fix text rendering

first "playable" prototype so far
This commit is contained in:
2025-01-27 22:19:53 +01:00
parent c378e34e62
commit 79d53efea2
4 changed files with 58 additions and 9 deletions

View File

@@ -288,9 +288,11 @@ void Board::drawPawn(short color, short field, short howMany) {
if (howMany > 1) {
sf::Text number(std::to_string(howMany), Ubuntu);
number.setCharacterSize(30);
number.setFillColor(sf::Color::Black);
number.setPosition(XY.x, XY.y);
number.setCharacterSize(48);
number.setFillColor(sf::Color::White);
number.setPosition(XY.x - 12, XY.y - 30); // wartości dobrane empirycznie :)
number.setOutlineColor (sf::Color::Black);
number.setOutlineThickness(2);
this->piecesText.push_back(number);
}
@@ -299,8 +301,8 @@ void Board::drawPawn(short color, short field, short howMany) {
}
void Board::drawPawns() {
/*this->pieces.clear();
this->piecesText.clear();*/
this->pieces.clear();
this->piecesText.clear();
for (int i = 0; i < 121; i++) {
// Wyznacz kolor i zlicz pionki
short color = -1;

View File

@@ -39,7 +39,7 @@ void Engine::initializeGame() {
// 3. najmniej znaczący bit informuje o zajęciu pola
// Ponadto przyjmujemy, że jeśli tylko 0 miejsce jest
// zajęte, to możemy wyświetlić
this->board.fields[getPawnInitialPosition(playerColor, i)][0] = playerColor + 4;
this->board.fields[getPawnInitialPosition(playerColor, i)][i] = playerColor + 4;
}
}
}
@@ -107,7 +107,8 @@ void Engine::nextTurn() {
pickAPlace = false;
std::cout << currentPlayer.getName() << " wychodzi "
<< currentPlayer.pawnsActive + 1 << ". pionkiem z bazy.\n";
this->spawnPiece(currentPlayer.getColor(), currentPlayer.pawnsActive + 1);
// Rusz pierwszy możliwy pionek w bazie:
this->spawnPiece(currentPlayer.getColor(), this->getFirstPawnAtBase(currentPlayer.getColor()));
this->pawnmoveBuffer.play();
this->board.smartSleep(2000);
break;
@@ -160,6 +161,11 @@ void Engine::nextTurn() {
// TODO: znajdź ten pionek i ustaw selectedField na niego!!!
std::cout << currentPlayer.getName() << " rusza jedyny pionek "
<< diceRoll << " pol do przodu.\n";
for (int i = 0; i < 4; i++) {
short* relativePawns = currentPlayer.getRelativePawns();
if (relativePawns[i] >= 0)
this->movePiece(currentPlayer.getColor(), relPosToField(currentPlayer.getColor(), relativePawns[i]), diceRoll);
}
this->pawnmoveBuffer.play();
this->board.smartSleep(2000);
break;
@@ -245,6 +251,42 @@ void Engine::nextTurn() {
return;
}
/**
* @brief Returns the first pawn at a player's base.
*
* @param[in] color Player's color
*
* @return The first pawn at base, or -1 if no pawns at base.
*/
short Engine::getFirstPawnAtBase(short color) {
for (int i = 0; i < 4; i++) {
if (players[color].getRelativePawns()[i] == -1) {
// -1 => pionek jest w bazie
return i;
}
}
return -1;
}
/**
* @brief Returns the first pawn at a player's base.
*
* @param[in] color Player's color
*
* @return The first pawn at base, or -1 if no pawns at base.
*/
short Engine::getFirstPawnNotAtBase(short color) {
for (int i = 0; i < 4; i++) {
if (players[color].getRelativePawns()[i] != -1) {
// -1 => pionek jest w bazie
return i;
}
}
return -1;
}
/**
* @brief Put the piece onto the board
*
@@ -259,6 +301,7 @@ short Engine::spawnPiece(short color, short pawnId) {
if (relativePieces[pawnId] == -1) {
players[color].sendToBoard(pawnId);
this->board.fields[relPosToField(color, relativePieces[pawnId])][pawnId] = color + 4;
this->board.fields[getPawnInitialPosition(color, pawnId)][pawnId] = 0;
return true;
}
return false;
@@ -335,8 +378,10 @@ bool Engine::movePiece(short color, short field, short steps) { // podajemy colo
// - jeśli ma, musimy go (/je) zbić
if (isNewFieldOccupied) takePawns(occupyingColor, newField);
// - w końcu, dopisujemy pionek bieżącego gracza
this->board.fields[field][i] = color + 4;
this->board.fields[newField][i] = color + 4;
this->players[this->currentPlayerIndex].unsafeMovePiece(i, steps);
// oraz usuwamy z bieżącego pola
this->board.fields[field][i] = 0;
// - po przesunięciu jednego z pionków kończymy pętlę, ponieważ
// nie chcemy przesuwać kolejnych pionków
return true;

View File

@@ -27,6 +27,8 @@ class Engine {
short spawnPiece(short color, short pawnId);
short takePawns(short color, short field);
bool movePiece(short color, short field, short steps);
short getFirstPawnAtBase(short color);
short getFirstPawnNotAtBase(short color);
// Audio
bool dicerollLoaded = true;

View File

@@ -36,7 +36,7 @@ class Player {
unsigned int seed;
std::vector<Pawn> pawns;
short color;
short relativePawns[4] = {-1};
short relativePawns[4] = {-1, -1, -1, -1};
int rollCount = 0;