From a13f7d2968ea5e3d1cd20bba3c6b23fafc21aa5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Teuli=C3=A8re?= Date: Thu, 17 Jan 2013 17:33:19 +0100 Subject: [PATCH] Bag: add a contains() convenience method --- game/bag.cpp | 2 +- game/bag.h | 6 ++++++ game/cmd/game_move_cmd.cpp | 2 +- game/game.cpp | 6 +++--- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/game/bag.cpp b/game/bag.cpp index 035a453..194cb0e 100644 --- a/game/bag.cpp +++ b/game/bag.cpp @@ -82,7 +82,7 @@ unsigned Bag::getNbConsonants() const void Bag::takeTile(const Tile &iTile) { - ASSERT(count(iTile), + ASSERT(contains(iTile), "The bag does not contain the letter " + lfw(iTile.getDisplayStr())); m_tilesMap[iTile]--; diff --git a/game/bag.h b/game/bag.h index b8bd7aa..454ae69 100644 --- a/game/bag.h +++ b/game/bag.h @@ -48,6 +48,12 @@ public: /// Count how many tiles identical to iTile are available in the bag unsigned count(const Tile &iTile) const; + /** + * Return true if the bag contains the given letter. + * This is a shortcut for: count(iTile) != 0 + */ + bool contains(const Tile &iTile) const { return count(iTile); } + /** * Return how many tiles/vowels/consonants are available * Warning: b.getNbVowels() + b.getNbConsonants() != b.getNbTiles(), diff --git a/game/cmd/game_move_cmd.cpp b/game/cmd/game_move_cmd.cpp index 3272fe5..d389ae2 100644 --- a/game/cmd/game_move_cmd.cpp +++ b/game/cmd/game_move_cmd.cpp @@ -111,7 +111,7 @@ void GameMoveCmd::playRound() { // Is the represented letter still available in the bag? const Tile &t = m_round.getTile(i).toUpper(); - if (bag.count(t)) + if (bag.contains(t)) { bag.replaceTile(Tile::Joker()); bag.takeTile(t); diff --git a/game/game.cpp b/game/game.cpp index 4112601..423f45c 100644 --- a/game/game.cpp +++ b/game/game.cpp @@ -348,7 +348,7 @@ PlayedRack Game::helperSetRackRandom(const PlayedRack &iPld, } // 2) If there was no joker, we add one if possible - if (!jokerFound && bag.count(Tile::Joker())) + if (!jokerFound && bag.contains(Tile::Joker())) { jokerAdded = true; pld.addNew(Tile::Joker()); @@ -356,7 +356,7 @@ PlayedRack Game::helperSetRackRandom(const PlayedRack &iPld, } // 3) Remove all the jokers from the bag, to avoid taking another one - while (bag.count(Tile::Joker())) + while (bag.contains(Tile::Joker())) { bag.takeTile(Tile::Joker()); } @@ -491,7 +491,7 @@ PlayedRack Game::helperSetRackRandom(const PlayedRack &iPld, // If the bag does not contain the letter anymore, // simply keep the joker in the rack. - if (bag.count(replacingTile)) + if (bag.contains(replacingTile)) { // The bag contains the replacing letter // We need to swap the joker (it is necessarily in the