From d73c93b4c783edfc6f3afe19d35b9409953c7323 Mon Sep 17 00:00:00 2001 From: Stephen Enders Date: Wed, 27 Jan 2021 22:15:08 -0500 Subject: Legal placement logic and capturing Use board_t for board and prevent illegal placements. And allow for piece capture --- src/helper.cpp | 79 +++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 64 insertions(+), 15 deletions(-) (limited to 'src/helper.cpp') 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::shared_ptr> createBoard(std::shared_ptr> textures) { - auto board = std::make_shared>(); + auto board = std::make_shared>(); sf::Texture& star_texture = (*textures)[STAR_TILE]; int blank_idx = 0; int sp_idx = 0; @@ -53,12 +53,12 @@ createBoard(std::shared_ptr> 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> 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> 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> 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> 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 -createPlayer(sf::Texture& texture) +createPlayer(const int pid, sf::Texture& texture) { std::shared_ptr player = std::make_shared(); + player->pid = pid; player->score = 0; player->pieces = std::make_shared>(); 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> myPieces, + std::shared_ptr> 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; +} -- cgit v1.2.3-54-g00ecf