summaryrefslogtreecommitdiff
path: root/src
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
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')
-rw-r--r--src/helper.cpp40
-rw-r--r--src/helper.hpp7
-rw-r--r--src/ur.cpp20
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<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)
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<struct piece_t> piece,
std::shared_ptr<std::vector<struct piece_t>> myPieces,
std::shared_ptr<std::vector<struct piece_t>> enemyPieces);
-std::vector<int>
-getLegalMoves(std::shared_ptr<struct player_t> activePlayer,
- std::shared_ptr<struct player_t> opponent);
+bool
+hasMoves(std::shared_ptr<struct player_t> activePlayer,
+ std::shared_ptr<struct player_t> 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<struct player_t> active_player,
+ std::shared_ptr<struct player_t> opponent,
std::shared_ptr<std::vector<struct dice_t>> dice,
std::shared_ptr<std::vector<sf::Sprite>> roll_sprites,
std::shared_ptr<std::vector<sf::Sprite>> 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,