From bf74001c193ae18cf208f57c145e8c9b8b88c7e8 Mon Sep 17 00:00:00 2001 From: Stephen Enders Date: Fri, 29 Jan 2021 18:12:04 -0500 Subject: Allow player to pass when no available moves If the player has NO legal moves display the pass option --- src/helper.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'src/helper.cpp') 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 activePlayer, + std::shared_ptr 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> pieces, struct piece_t* piece) -- cgit v1.2.3-54-g00ecf