From 975055e40938730cb09d151ba17bf8defd3b4adf Mon Sep 17 00:00:00 2001 From: Steph Enders Date: Sun, 26 Feb 2023 10:43:21 -0500 Subject: Code quality and improvements I genuinely cannot remember what I was doing here, as I never committed it. But basically - it looks like I used my IDE and fixed warnings and improved some code blocks / references --- src/helper.cpp | 107 +++++++++++++++------------------------------------------ 1 file changed, 28 insertions(+), 79 deletions(-) (limited to 'src/helper.cpp') diff --git a/src/helper.cpp b/src/helper.cpp index 3d44bc6..7ced30c 100644 --- a/src/helper.cpp +++ b/src/helper.cpp @@ -37,7 +37,7 @@ next(int* p, int max) } std::shared_ptr> -createBoard(std::shared_ptr> textures) +createBoard(const std::shared_ptr>& textures) { auto board = std::make_shared>(); sf::Texture& star_texture = (*textures)[STAR_TILE]; @@ -146,17 +146,6 @@ createBoard(std::shared_ptr> textures) return board; } -sf::Font -loadFont() -{ - sf::Font font; - if (!font.loadFromFile("./res/DejaVuSansMono.ttf")) { - std::cerr << "Unable to load font" << std::endl; - throw std::runtime_error("Unable to load font"); - } - return font; -} - std::shared_ptr createPiece(int id, sf::Texture& texture) { @@ -171,10 +160,9 @@ createPiece(int id, sf::Texture& texture) } std::shared_ptr -createPlayer(const int pid, sf::Texture& texture) +createPlayer(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++) { @@ -229,19 +217,19 @@ createRollSprites(sf::Texture& t1, sf::Texture& t2) } std::shared_ptr> -createPassSprites(std::shared_ptr> textures) +createPassSprites(const std::shared_ptr>& textures) { auto sprites = std::make_shared>(); - for (int i = 0; i < 3; i++) { - sprites->push_back(sf::Sprite((*textures)[PASS_TILES[i]])); + for (int i : PASS_TILES) { + sprites->push_back(sf::Sprite((*textures)[i])); } return sprites; } std::shared_ptr> createWinSprites(int player_id, - std::shared_ptr> textures) + const std::shared_ptr>& textures) { auto sprites = std::make_shared>(); auto tile_ids = player_id == P1_ID ? P1_WIN_TILES : P2_WIN_TILES; @@ -256,12 +244,12 @@ createWinSprites(int player_id, } std::shared_ptr> -createStartSprites(std::shared_ptr> textures) +createStartSprites(const std::shared_ptr>& textures) { auto sprites = std::make_shared>(); - for (int i = 0; i < 3; i++) { - sprites->push_back(sf::Sprite((*textures)[START_TILES[i]])); + for (int i : START_TILES) { + sprites->push_back(sf::Sprite((*textures)[i])); } return sprites; } @@ -269,51 +257,12 @@ createStartSprites(std::shared_ptr> textures) void makeNum(sf::Sprite* sprite_ptr, int num, - std::shared_ptr> textures) + const std::shared_ptr>& textures) { sf::Texture& t = (*textures)[NUMS_TILES[num]]; sprite_ptr->setTexture(t); }; -bool -clickedPiece(sf::Vector2i mousePosition, struct piece_t* piece) -{ - return piece->sprite.getGlobalBounds().contains(mousePosition.x, - mousePosition.y); -} - -bool -canMovePiece(struct piece_t* piece, - int roll, - std::shared_ptr> myPieces, - std::shared_ptr> enemyPieces) -{ - int next = piece->position + roll; - - // rolled passed the exit - if (next > EXIT_SPACE) { - return false; - } - - // colliding with another piece - for (struct piece_t& p : (*myPieces)) { - // cannot move onto your own piece - if (p.id != piece->id && p.position == next) { - return false; - } - } - - // can't attack in safe square - for (struct piece_t& p : (*enemyPieces)) { - // cannot move onto a protected enemy piece - if (next == SAFE_SPACE && p.position == SAFE_SPACE) { - return false; - } - } - - return true; -} - // This function takes in an row and col we want to put the sprite in // and translate it to a real position in the view // This is because we've ZOOMed in an that adjusts the entire view. @@ -326,9 +275,9 @@ pos(float c, float r) bool canPlace(struct piece_t* piece, int turn_pid, - struct board_t board_tile, - std::shared_ptr> myPieces, - std::shared_ptr> opponentPieces, + const struct board_t& board_tile, + const std::shared_ptr>& myPieces, + const std::shared_ptr>& opponentPieces, int& takenPieceId) { if (board_tile.pid != turn_pid && board_tile.pid != SHARED_ID) @@ -366,39 +315,39 @@ canPlace(struct piece_t* piece, * this is for the purpose of passing if no legal move available */ bool -hasMoves(std::shared_ptr activePlayer, - std::shared_ptr opponent, +hasMoves(const std::shared_ptr& activePlayer, + const std::shared_ptr& opponent, int roll) { int occupied_spaces[14] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + // Populate player pieces for (auto& p : (*activePlayer->pieces)) { if (p.position < 0) continue; // not on board int target = p.position + roll; - if (target == EXIT_SPACE) + if (target == EXIT_SPACE) { return true; // can always land in the exit skip rest of the function + } occupied_spaces[p.position]++; } + // Only populate safe space for (auto& op : (*opponent->pieces)) { if (op.position == SAFE_SPACE) { occupied_spaces[SAFE_SPACE]++; - break; + break; // only 1 piece can be in the safe space } } - 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; + // find if any moves available + return std::ranges::any_of(activePlayer->pieces->begin(), + activePlayer->pieces->end(), + [&occupied_spaces, &roll](const piece_t& p) { + int target = p.position + roll; + return target == EXIT_SPACE || + occupied_spaces[target] == 0; + }); } void -- cgit v1.2.3-54-g00ecf