fixed game logic

This commit is contained in:
2025-03-23 21:41:37 +01:00
parent 507bc9cc86
commit 38008e00bc
7 changed files with 90 additions and 59 deletions

View File

@@ -36,9 +36,12 @@ void GameBoard::initialize() {
this->activePiece = nullptr;
this->heldPiece = nullptr;
this->isLastMoveKick = false;
this->movedLeftLast = false;
}
bool GameBoard::moveLeft() {
this->movedLeftLast = true;
if (this->activePieceInWall(Position{-1, 0})) {
return false;
}
@@ -50,6 +53,8 @@ bool GameBoard::moveLeft() {
}
bool GameBoard::moveRight() {
this->movedLeftLast = false;
if (this->activePieceInWall(Position{1, 0})) {
return false;
}
@@ -137,30 +142,21 @@ bool GameBoard::tryKicking(bool testingBottom, const std::set<Position>& safePos
bool overlapsRight = true;
int i = (j == 0) ? 1 : 0;
do {
// check right before left arbitrarly, we don't decide this with rotations since it would still be arbitrary with 180° rotations
if (overlapsRight) {
Position shift{+i, j};
if (!this->activePieceOverlaps(safePositions, shift)) {
overlapsLeft = false;
// we first check the side to which the player moved last
if (movedLeftLast) {
if (overlapsLeft) {
if (this->tryFittingKickedPiece(safePositions, Position({-i, j}), overlapsLeft)) return true;
}
else {
if (!this->activePieceInWall(shift)) {
this->activePiecePosition += shift;
return true;
}
if (overlapsRight) {
if (this->tryFittingKickedPiece(safePositions, Position({+i, j}), overlapsRight)) return true;
}
}
if (overlapsLeft) {
Position shift{-i, j};
if (!this->activePieceOverlaps(safePositions, shift)) {
overlapsLeft = false;
else {
if (overlapsRight) {
if (this->tryFittingKickedPiece(safePositions, Position({+i, j}), overlapsRight)) return true;
}
else {
if (!this->activePieceInWall(shift)) {
this->activePiecePosition += shift;
return true;
}
if (overlapsLeft) {
if (this->tryFittingKickedPiece(safePositions, Position({-i, j}), overlapsLeft)) return true;
}
}
@@ -177,6 +173,26 @@ bool GameBoard::tryKicking(bool testingBottom, const std::set<Position>& safePos
return false;
}
bool GameBoard::tryFittingKickedPiece(const std::set<Position>& safePositions, const Position& shift, bool& overlaps) {
if (!this->activePieceOverlaps(safePositions, shift)) {
overlaps = false;
}
else {
if (!this->activePieceInWall(shift)) {
this->activePiecePosition += shift;
return true;
}
}
return false;
}
bool GameBoard::activePieceOverlaps(const std::set<Position>& safePositions, const Position& shift) const {
for (Position position : this->activePiece->getPositions()) {
if (safePositions.contains(position + this->activePiecePosition + shift)) return true;
}
return false;
}
bool GameBoard::hold(Rotation initialRotation) {
std::swap(this->activePiece, this->heldPiece);
@@ -236,6 +252,13 @@ bool GameBoard::spawnNextPiece() {
return this->activePieceInWall();
}
bool GameBoard::activePieceInWall(const Position& shift) const {
for (Position position : this->activePiece->getPositions()) {
if (this->board.getBlock(position + this->activePiecePosition + shift) != NOTHING) return true;
}
return false;
}
bool GameBoard::touchesGround() const {
return this->activePieceInWall(Position{0, -1});
}
@@ -257,6 +280,8 @@ LineClear GameBoard::lockPiece() {
this->board.changeBlock(position + this->activePiecePosition, this->activePiece->getBlockType());
}
this->activePiece = nullptr;
return LineClear{this->board.clearRows(), isLockedInPlace, (!isLockedInPlace) && this->isLastMoveKick};
}
@@ -291,20 +316,6 @@ const std::vector<Piece>& GameBoard::getNextPieces() const {
return this->nextQueue;
}
bool GameBoard::activePieceInWall(const Position& shift) const {
for (Position position : this->activePiece->getPositions()) {
if (this->board.getBlock(position + this->activePiecePosition + shift) != NOTHING) return true;
}
return false;
}
bool GameBoard::activePieceOverlaps(const std::set<Position>& safePositions, const Position& shift) const {
for (Position position : this->activePiece->getPositions()) {
if (safePositions.contains(position + this->activePiecePosition + shift)) return true;
}
return false;
}
void GameBoard::goToSpawnPosition() {
int lowestPosition = this->activePiece->getLength() - 1;
for (Position position : this->activePiece->getPositions()) {