summaryrefslogtreecommitdiff
path: root/src/helper.cpp
diff options
context:
space:
mode:
authorStephen Enders <smenders@gmail.com>2021-01-27 22:15:08 -0500
committerStephen Enders <smenders@gmail.com>2021-01-27 22:22:40 -0500
commitd73c93b4c783edfc6f3afe19d35b9409953c7323 (patch)
treedb6841bd704d8de2b16abfa34fa72e7e0039aed6 /src/helper.cpp
parent100907bd56b18c50aab9efb10700956b8c164c05 (diff)
Legal placement logic and capturing
Use board_t for board and prevent illegal placements. And allow for piece capture
Diffstat (limited to 'src/helper.cpp')
-rw-r--r--src/helper.cpp79
1 files changed, 64 insertions, 15 deletions
diff --git a/src/helper.cpp b/src/helper.cpp
index 96eb919..11374e8 100644
--- a/src/helper.cpp
+++ b/src/helper.cpp
@@ -36,10 +36,10 @@ next(int* p, int max)
return i;
}
-std::shared_ptr<std::vector<struct piece_t>>
+std::shared_ptr<std::vector<struct board_t>>
createBoard(std::shared_ptr<std::vector<sf::Texture>> textures)
{
- auto board = std::make_shared<std::vector<struct piece_t>>();
+ auto board = std::make_shared<std::vector<struct board_t>>();
sf::Texture& star_texture = (*textures)[STAR_TILE];
int blank_idx = 0;
int sp_idx = 0;
@@ -53,12 +53,12 @@ createBoard(std::shared_ptr<std::vector<sf::Texture>> textures)
// p1 pieces
// p1 star
// id's don't matter
- int id = 0;
{
sf::Sprite s;
s.setTexture(star_texture);
s.setPosition(pos(3, 5));
- board->push_back({ id++, 3, s });
+ struct board_t bp = { P1_ID, 3, s };
+ board->push_back(bp);
}
// p1 start
for (int i = 0; i < 3; i++) {
@@ -66,25 +66,29 @@ createBoard(std::shared_ptr<std::vector<sf::Texture>> textures)
sf::Sprite s;
s.setTexture(t);
s.setPosition(pos(4 + i, 5));
- board->push_back({ id++, 2 - i, s });
+ struct board_t bp = { P1_ID, 2 - i, s };
+ board->push_back(bp);
}
// p1 end
{
sf::Sprite goal;
goal.setTexture((*textures)[P1_END]);
goal.setPosition(pos(8, 5));
- board->push_back({ id++, EXIT_SPACE, goal });
+ struct board_t exit_p = { P1_ID, EXIT_SPACE, goal };
+ board->push_back(exit_p);
sf::Sprite end_star;
end_star.setTexture(star_texture);
end_star.setPosition(pos(9, 5));
- board->push_back({ id++, EXIT_SPACE - 1, end_star });
+ struct board_t end_p = { P1_ID, EXIT_SPACE - 1, end_star };
+ board->push_back(end_p);
sf::Texture& t = (*textures)[P1_BOARD_TILES[next(&sp_idx, 2)]];
sf::Sprite s;
s.setTexture(t);
s.setPosition(pos(10, 5));
- board->push_back({ id++, EXIT_SPACE - 2, s });
+ struct board_t bp = { P1_ID, EXIT_SPACE - 2, s };
+ board->push_back(bp);
}
// center pieces
for (int i = 0; i < 8; i++) {
@@ -96,7 +100,8 @@ createBoard(std::shared_ptr<std::vector<sf::Texture>> textures)
s.setTexture(t);
}
s.setPosition(pos(3 + i, 4));
- board->push_back({ id++, 4 + i, s });
+ struct board_t bp = { SHARED_ID, 4 + i, s };
+ board->push_back(bp);
}
// p2 pieces
// p2 star
@@ -104,7 +109,8 @@ createBoard(std::shared_ptr<std::vector<sf::Texture>> textures)
sf::Sprite s;
s.setTexture(star_texture);
s.setPosition(pos(3, 3));
- board->push_back({ id++, 3, s });
+ struct board_t bp = { P2_ID, 3, s };
+ board->push_back(bp);
}
// p2 start
for (int i = 0; i < 3; i++) {
@@ -112,25 +118,29 @@ createBoard(std::shared_ptr<std::vector<sf::Texture>> textures)
sf::Sprite s;
s.setTexture(t);
s.setPosition(pos(4 + i, 3));
- board->push_back({ id++, 2 - i, s });
+ struct board_t bp = { P2_ID, 2 - i, s };
+ board->push_back(bp);
}
// p2 end
{
sf::Sprite goal;
goal.setTexture((*textures)[P2_END]);
goal.setPosition(pos(8, 3));
- board->push_back({ id++, EXIT_SPACE, goal });
+ struct board_t goal_p = { P2_ID, EXIT_SPACE, goal };
+ board->push_back(goal_p);
sf::Sprite end_star;
end_star.setTexture(star_texture);
end_star.setPosition(pos(9, 3));
- board->push_back({ id++, EXIT_SPACE - 1, end_star });
+ struct board_t end_p = { P2_ID, EXIT_SPACE - 1, end_star };
+ board->push_back(end_p);
sf::Texture& t = (*textures)[P2_BOARD_TILES[next(&sp_idx, 2)]];
sf::Sprite end_tile;
end_tile.setTexture(t);
end_tile.setPosition(pos(10, 3));
- board->push_back({ id++, EXIT_SPACE - 2, end_tile });
+ struct board_t bp = { P2_ID, EXIT_SPACE - 2, end_tile };
+ board->push_back(bp);
}
return board;
@@ -161,9 +171,10 @@ createPiece(int id, sf::Texture& texture)
}
std::shared_ptr<struct player_t>
-createPlayer(sf::Texture& texture)
+createPlayer(const int pid, sf::Texture& texture)
{
std::shared_ptr<struct player_t> player = std::make_shared<struct player_t>();
+ player->pid = pid;
player->score = 0;
player->pieces = std::make_shared<std::vector<struct piece_t>>();
for (int i = 0; i < NUM_PIECES; i++) {
@@ -273,3 +284,41 @@ pos(float c, float r)
{
return { c * SPRITE_SIZE, r * SPRITE_SIZE };
}
+
+bool
+canPlace(struct piece_t* piece,
+ int turn_pid,
+ struct board_t board_tile,
+ std::shared_ptr<std::vector<struct piece_t>> myPieces,
+ std::shared_ptr<std::vector<struct piece_t>> opponentPieces,
+ int& takenPieceId)
+{
+ if (board_tile.pid != turn_pid && board_tile.pid != SHARED_ID)
+ return false;
+ int position = board_tile.position;
+ if (position == EXIT_SPACE)
+ return true; // can always place in the exit
+
+ // can never collide with your own pieces
+ for (auto& p : (*myPieces)) {
+ if ((p.id != piece->id) // not the same
+ && (p.position == position)) {
+ return false; // collides with own pieces
+ }
+ }
+ // can collide with an enemy
+ if (position >= 4 && position <= 11) {
+ for (auto& p : (*opponentPieces)) {
+ // collided
+ if (p.position == position) {
+ // can not capture on safe space
+ if (position == SAFE_SPACE)
+ return false;
+ takenPieceId = p.id;
+ return true;
+ }
+ }
+ }
+
+ return true;
+}