summaryrefslogtreecommitdiff
path: root/src/helper.cpp
diff options
context:
space:
mode:
authorStephen Enders <smenders@gmail.com>2021-01-29 18:12:04 -0500
committerStephen Enders <smenders@gmail.com>2021-01-29 18:12:04 -0500
commitbf74001c193ae18cf208f57c145e8c9b8b88c7e8 (patch)
tree04d60fd18b6590e909bcbc489e953a0a544d774c /src/helper.cpp
parent6f3e64619980c87cf5d4cceff0b4546fb0aa6ae7 (diff)
Allow player to pass when no available moves
If the player has NO legal moves display the pass option
Diffstat (limited to 'src/helper.cpp')
-rw-r--r--src/helper.cpp40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/helper.cpp b/src/helper.cpp
index 57e067e..d0a349a 100644
--- a/src/helper.cpp
+++ b/src/helper.cpp
@@ -345,6 +345,46 @@ canPlace(struct piece_t* piece,
return true;
}
+/*
+ * this move simply checks for if any legal move exists
+ * this is for the purpose of passing if no legal move available
+ */
+bool
+hasMoves(std::shared_ptr<struct player_t> activePlayer,
+ std::shared_ptr<struct player_t> opponent,
+ int roll)
+{
+ int occupied_spaces[14] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+
+ for (auto& p : (*activePlayer->pieces)) {
+ if (p.position < 0)
+ continue; // not on board
+ int target = p.position + roll;
+ if (target == EXIT_SPACE)
+ return true; // can always land in the exit skip rest of the function
+ occupied_spaces[p.position]++;
+ }
+
+ for (auto& op : (*opponent->pieces)) {
+ if (op.position == SAFE_SPACE) {
+ occupied_spaces[SAFE_SPACE]++;
+ break;
+ }
+ }
+
+ for (auto& p : (*activePlayer->pieces)) {
+ int target = p.position + roll;
+ if (target > EXIT_SPACE) {
+ continue; // off the board
+ } else if (target == EXIT_SPACE) {
+ return true; // can always land in the exit
+ } else if (occupied_spaces[target] == 0) {
+ return true; // has a free space
+ }
+ }
+ return false;
+}
+
void
clearPiece(std::shared_ptr<std::vector<struct piece_t>> pieces,
struct piece_t* piece)