Skip to content

Commit

Permalink
Add checking if is bomb at position
Browse files Browse the repository at this point in the history
  • Loading branch information
kn65op committed Feb 1, 2018
1 parent 6839dfb commit 716f0db
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
11 changes: 5 additions & 6 deletions src/game/LimitedBombLauncher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@

LimitedBombLauncher::LimitedBombLauncher(std::shared_ptr<GameWorld> gw,
const int maximum_bombs)
: game_world{std::move(gw)},
max_bombs{maximum_bombs}
: game_world{std::move(gw)}, max_bombs{maximum_bombs}
{
}

bool LimitedBombLauncher::try_spawn_bomb(const math::Position2)
bool LimitedBombLauncher::try_spawn_bomb(const math::Position2 pos)
{
const auto bombCanBeSpawned = can_spawn_bomb();
const auto bombCanBeSpawned = can_spawn_bomb(pos);
if (bombCanBeSpawned)
{
launch_bomb();
}
return bombCanBeSpawned;
}

bool LimitedBombLauncher::can_spawn_bomb() const
bool LimitedBombLauncher::can_spawn_bomb(const math::Position2 pos) const
{
return max_bombs > bombs;
return max_bombs > bombs && !game_world->is_bomb_at_pos(BombPosition{pos});
}

void LimitedBombLauncher::launch_bomb()
Expand Down
2 changes: 1 addition & 1 deletion src/game/LimitedBombLauncher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class LimitedBombLauncher : public BombLauncher
const int max_bombs;
int bombs{0};

bool can_spawn_bomb() const;
bool can_spawn_bomb(math::Position2) const;
void launch_bomb();
bool can_retrieve_bomb() const;
void retrieve_bomb();
Expand Down
18 changes: 18 additions & 0 deletions src/game/LimitedBombLauncher.ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,17 @@ using namespace ::fakeit;

struct LimitedBombLauncherTest : public ::testing::Test
{
LimitedBombLauncherTest()
{
When(Method(game_world, is_bomb_at_pos)).AlwaysReturn(no_bom_at_pos);
}

const math::Position2 default_position{0.2f, 0.2f};
const BombPosition default_bomb_position{default_position};
const bool bomb_has_been_spawned = true;
const bool bomb_cannot_be_spawned = false;
const bool bomb_at_pos = true;
const bool no_bom_at_pos = false;
const int max_bombs = 2;

Mock<GameWorld> game_world;
Expand All @@ -30,6 +38,16 @@ TEST_F(LimitedBombLauncherWithoutBombsLaunched, ShouldLaunchBomb)
::testing::Eq(bomb_has_been_spawned));
}

TEST_F(LimitedBombLauncherWithoutBombsLaunched,
WhenThereIsBombAtPostition_ShouldNotSpawnBomb)
{
When(Method(game_world, is_bomb_at_pos).Using(default_bomb_position))
.Return(bomb_at_pos);

ASSERT_THAT(launcher.try_spawn_bomb(default_position),
::testing::Eq(bomb_cannot_be_spawned));
}

struct LimitedBombLauncherWithAllBombsLunched : public LimitedBombLauncherTest
{
LimitedBombLauncherWithAllBombsLunched()
Expand Down

0 comments on commit 716f0db

Please sign in to comment.