From 9ed25e3f4dde2e06425948773096c8e49f0c080c Mon Sep 17 00:00:00 2001 From: Wouter Wijsman Date: Fri, 29 Nov 2024 15:06:51 +0100 Subject: [PATCH] Add point for shells in multiple matches --- src/Board.cpp | 47 ++++++++++++++++++++++++++++++++++---------- src/BoardManager.cpp | 16 +++++++++------ src/Shell.hpp | 7 ++++--- src/constants.hpp | 2 +- 4 files changed, 52 insertions(+), 20 deletions(-) diff --git a/src/Board.cpp b/src/Board.cpp index 6126bad..4dc7539 100644 --- a/src/Board.cpp +++ b/src/Board.cpp @@ -37,11 +37,11 @@ std::vector Board::match() { std::vector matchesFound = getMatches(this->shells); for (Match m : matchesFound) { - if (m.direction == Direction::HORIZONTAL) { + if (m.match_type == MatchType::HORIZONTAL) { for(int x = 0; x < 3; x++) { this->shells[m.x+x][m.y] = ShellType::NONE; } - } else { + } else if (m.match_type == MatchType::VERTICAL) { for(int y = 0; y < 3; y++) { this->shells[m.x][m.y+y] = ShellType::NONE; } @@ -53,22 +53,49 @@ std::vector Board::match() { std::vector Board::getMatches(std::vector> shells){ std::vector matchesFound; + std::vector verticalMatches; + std::vector horizontalMatches; + std::vector bonusMatches; for (int x = 0; x < getWidth(); x++) { for (int y = 0; y < getHeight() - 2; y++) { if (shellsMatch(shells, {x,y},{x,y+1},{x,y+2})) { - matchesFound.push_back({shells[x][y], x, y, Direction::VERTICAL}); + verticalMatches.push_back({shells[x][y], x, y, MatchType::VERTICAL}); } } } for (int y = 0; y < getHeight(); y++) { for (int x = 0; x < getWidth() - 2; x++) { if (shellsMatch(shells, {x,y},{x+1,y},{x+2,y})) { - matchesFound.push_back({shells[x][y], x, y, Direction::HORIZONTAL}); + horizontalMatches.push_back({shells[x][y], x, y, MatchType::HORIZONTAL}); } } } + // Find bonus matches, aka shells that are in both a horizontal and vertical match + for (Match horizontal : horizontalMatches) { + for (Match vertical : verticalMatches) { + for (int h = 0; h < 3; h++) { + for (int v = 0; v < 3; v++) { + if (horizontal.x + h == vertical.x && horizontal.y == vertical.y + v) { + if (horizontal.type == ShellType::BUBBLE) { + continue; + } + if (horizontal.type == ShellType::URCHIN) { + continue; + } + bonusMatches.push_back({horizontal.type, horizontal.x + h, vertical.y + v, MatchType::BONUS}); + } + } + } + } + } + + matchesFound.reserve(verticalMatches.size() + horizontalMatches.size() + bonusMatches.size()); + matchesFound.insert(matchesFound.end(), verticalMatches.begin(), verticalMatches.end()); + matchesFound.insert(matchesFound.end(), horizontalMatches.begin(), horizontalMatches.end()); + matchesFound.insert(matchesFound.end(), bonusMatches.begin(), bonusMatches.end()); + return matchesFound; } @@ -178,15 +205,15 @@ std::vector> Board::getShellsAfterSwap(std::vector 0) ? 1 : -1; - for (int i = p1.x + direction; i != p2.x + direction; i+=direction) { - shells[i-direction][p1.y] = shells[i][p1.y]; + int match_type = ((p2.x - p1.x) > 0) ? 1 : -1; + for (int i = p1.x + match_type; i != p2.x + match_type; i+=match_type) { + shells[i-match_type][p1.y] = shells[i][p1.y]; shells[i][p1.y] = type1; } } else if (p1.x == p2.x) { - int direction = ((p2.y - p1.y) > 0) ? 1 : -1; - for (int i = p1.y + direction; i != p2.y + direction; i+=direction) { - shells[p1.x][i-direction] = shells[p1.x][i]; + int match_type = ((p2.y - p1.y) > 0) ? 1 : -1; + for (int i = p1.y + match_type; i != p2.y + match_type; i+=match_type) { + shells[p1.x][i-match_type] = shells[p1.x][i]; shells[p2.x][i] = type1; } } diff --git a/src/BoardManager.cpp b/src/BoardManager.cpp index 8b04655..8d8a57a 100644 --- a/src/BoardManager.cpp +++ b/src/BoardManager.cpp @@ -550,8 +550,8 @@ void BoardManager::drawIntroduction(SDL_Renderer * renderer) { bool BoardManager::isDoubleMatch(Match match) { for(Match m : this->matches_made) { - if (m.direction != match.direction) { - if (m.direction == Direction::HORIZONTAL) { + if (m.match_type != match.match_type) { + if (m.match_type == MatchType::HORIZONTAL) { if (m.x+1 == match.x && m.y == match.y+1) { return true; } @@ -592,6 +592,10 @@ void BoardManager::drawMatches(SDL_Renderer * renderer) { current_shell_size = (this->options->getShellSize()*(MATCH_STEPS-animation))/MATCH_STEPS; } for(int i = 0; i < 3; i++) { + // Do not draw extra shells for bonus matches + if (match.match_type == MatchType::BONUS) { + break; + } SDL_Rect srcrect; srcrect.x = this->options->getShellSize() * (int) match.type; srcrect.y = 0; @@ -604,9 +608,9 @@ void BoardManager::drawMatches(SDL_Renderer * renderer) { dstrect.w = current_shell_size; dstrect.h = current_shell_size; - if (match.direction == Direction::HORIZONTAL) { + if (match.match_type == MatchType::HORIZONTAL) { dstrect.x += this->options->getShellSize()*i; - } else { + } else if (match.match_type == MatchType::VERTICAL) { dstrect.y += this->options->getShellSize()*i; } @@ -644,9 +648,9 @@ void BoardManager::drawMatches(SDL_Renderer * renderer) { SDL_QueryTexture(text, NULL, NULL, &rect_plus_one.w, &rect_plus_one.h); rect_plus_one.x += this->options->getShellSize()/2 - rect_plus_one.w/2; rect_plus_one.y += this->options->getShellSize()/2 - rect_plus_one.h/2; - if (match.direction == Direction::HORIZONTAL) { + if (match.match_type == MatchType::HORIZONTAL) { rect_plus_one.x += this->options->getShellSize(); - } else { + } else if (match.match_type == MatchType::VERTICAL) { rect_plus_one.y += this->options->getShellSize(); } diff --git a/src/Shell.hpp b/src/Shell.hpp index e4ba9cb..827fb27 100644 --- a/src/Shell.hpp +++ b/src/Shell.hpp @@ -19,16 +19,17 @@ struct Shell { int y; }; -enum class Direction { +enum class MatchType { HORIZONTAL, - VERTICAL + VERTICAL, + BONUS // Having a shell in both a vertical and horizontal match results in a bonus point }; struct Match { ShellType type; int x; int y; - Direction direction; + MatchType match_type; }; #endif // SHELL_HPP diff --git a/src/constants.hpp b/src/constants.hpp index 287ba66..0e715f9 100644 --- a/src/constants.hpp +++ b/src/constants.hpp @@ -5,7 +5,7 @@ inline constexpr float ANALOG_DEADZONE_MULTIPLIER = 0.5; inline constexpr int DROP_TIME = 300; // in ms -inline constexpr int MATCH_TIME = 500; // in ms +inline constexpr int MATCH_TIME = 2000; // in ms inline constexpr int MATCH_STEPS = 100; inline constexpr float MATCH_DELAY = MATCH_TIME/MATCH_STEPS;