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 ++++++++++++++++++++++++++++++++++++++++ src/helper.hpp | 7 ++++--- src/ur.cpp | 20 ++++++++++++-------- 3 files changed, 56 insertions(+), 11 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 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) diff --git a/src/helper.hpp b/src/helper.hpp index b56dc30..f7d6b8d 100644 --- a/src/helper.hpp +++ b/src/helper.hpp @@ -117,9 +117,10 @@ canMovePiece(std::shared_ptr piece, std::shared_ptr> myPieces, std::shared_ptr> enemyPieces); -std::vector -getLegalMoves(std::shared_ptr activePlayer, - std::shared_ptr opponent); +bool +hasMoves(std::shared_ptr activePlayer, + std::shared_ptr opponent, + int roll); sf::Vector2f pos(float c, float r); diff --git a/src/ur.cpp b/src/ur.cpp index a1d88e8..881cff1 100644 --- a/src/ur.cpp +++ b/src/ur.cpp @@ -77,6 +77,8 @@ p2_turn() inline void render_dice(sf::RenderWindow* window, + std::shared_ptr active_player, + std::shared_ptr opponent, std::shared_ptr> dice, std::shared_ptr> roll_sprites, std::shared_ptr> pass_sprites, @@ -101,7 +103,7 @@ render_dice(sf::RenderWindow* window, turn_roll += r; } makeNum(roll_result, turn_roll, textures); - if (turn_roll == 0) { + if (turn_roll == 0 || !hasMoves(active_player, opponent, turn_roll)) { change_state(GameState::PASSING); } else { change_state(GameState::PLACING); @@ -122,15 +124,15 @@ render_dice(sf::RenderWindow* window, roll_c = dice_c; } - if (state == GameState::PLACING || state == GameState::PASSING) { + if (state == GameState::PLACING) { + window->draw(*roll_result); + } else if (state == GameState::PASSING) { // draw roll text window->draw(*roll_result); - if (turn_roll == 0) { - int psi = SPRITE_COLS / 2 - 1; - for (auto& ps : (*pass_sprites)) { - ps.setPosition(pos(psi++, SPRITE_ROWS / 2)); - window->draw(ps); - } + int psi = SPRITE_COLS / 2 - 1; + for (auto& ps : (*pass_sprites)) { + ps.setPosition(pos(psi++, SPRITE_ROWS / 2)); + window->draw(ps); } } else if (state == GameState::ROLLING) { // if completed update dice sprites @@ -415,6 +417,8 @@ main() } render_dice(&window, + p1_turn() ? p1 : p2, + p2_turn() ? p1 : p2, dice, roll_sprites, pass_sprites, -- cgit v1.2.3-54-g00ecf