Skip to content

Commit fb44466

Browse files
authored
Merge pull request utilForever#42 from utilForever/fix-41
Fix icon spread problem when they are stacked at the end of map
2 parents 602b7c4 + 8986184 commit fb44466

File tree

5 files changed

+49
-4
lines changed

5 files changed

+49
-4
lines changed

Includes/baba-is-auto/Games/Map.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ class Map
7878
std::vector<Position> GetPositions(ObjectType type) const;
7979

8080
private:
81+
//! Checks position (x, y) is boundary.
82+
//! \param x The x position.
83+
//! \param y The y position.
84+
//! \return true if it is boundary, false otherwise.
85+
bool IsBoundary(std::size_t x, std::size_t y) const;
86+
8187
std::size_t m_width = 0;
8288
std::size_t m_height = 0;
8389

Includes/baba-is-auto/Games/Object.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ class Object
4545

4646
//! Adds an object type.
4747
//! \param type An object type to add.
48-
void Add(ObjectType type);
48+
//! \param isBoundary A flag to indicate it is boundary.
49+
void Add(ObjectType type, bool isBoundary);
4950

5051
//! Removes an object type.
5152
//! \param type An object type to remove.

Sources/baba-is-auto/Games/Map.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void Map::Load(std::string_view filename)
6060

6161
void Map::AddObject(std::size_t x, std::size_t y, ObjectType type)
6262
{
63-
m_objects.at(y * m_width + x).Add(type);
63+
m_objects.at(y * m_width + x).Add(type, IsBoundary(x, y));
6464
}
6565

6666
void Map::RemoveObject(std::size_t x, std::size_t y, ObjectType type)
@@ -95,4 +95,9 @@ std::vector<Position> Map::GetPositions(ObjectType type) const
9595

9696
return res;
9797
}
98+
99+
bool Map::IsBoundary(std::size_t x, std::size_t y) const
100+
{
101+
return x == 0 || x == m_width - 1 || y == 0 || y == m_height - 1;
102+
}
98103
} // namespace baba_is_auto

Sources/baba-is-auto/Games/Object.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,18 @@ bool Object::operator==(const Object& rhs) const
2323
return m_types == rhs.m_types;
2424
}
2525

26-
void Object::Add(ObjectType type)
26+
void Object::Add(ObjectType type, bool isBoundary)
2727
{
2828
if (m_types.find(type) != m_types.end())
2929
{
30-
m_types[type] += 1;
30+
if (isBoundary)
31+
{
32+
m_types[type] = 1;
33+
}
34+
else
35+
{
36+
m_types[type] += 1;
37+
}
3138
}
3239
else
3340
{

Tests/UnitTests/UnitTests.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,32 @@ TEST_CASE("Map - Icon Vanishing")
177177
CHECK(game.GetMap().At(14, 1).HasType(ObjectType::ICON_WALL));
178178
}
179179

180+
TEST_CASE("Map - Icon Spread")
181+
{
182+
Game game(MAPS_DIR "off_limits_bug.txt");
183+
184+
game.MovePlayer(Direction::RIGHT);
185+
game.MovePlayer(Direction::RIGHT);
186+
game.MovePlayer(Direction::RIGHT);
187+
game.MovePlayer(Direction::RIGHT);
188+
game.MovePlayer(Direction::RIGHT);
189+
game.MovePlayer(Direction::RIGHT);
190+
game.MovePlayer(Direction::RIGHT);
191+
game.MovePlayer(Direction::RIGHT);
192+
game.MovePlayer(Direction::LEFT);
193+
game.MovePlayer(Direction::LEFT);
194+
195+
CHECK(game.GetMap().At(21, 3).HasType(ObjectType::ICON_WALL));
196+
CHECK(game.GetMap().At(21, 4).HasType(ObjectType::ICON_WALL));
197+
CHECK(game.GetMap().At(21, 5).HasType(ObjectType::ICON_WALL));
198+
CHECK(game.GetMap().At(22, 3).HasType(ObjectType::ICON_EMPTY));
199+
CHECK(game.GetMap().At(22, 4).HasType(ObjectType::ICON_EMPTY));
200+
CHECK(game.GetMap().At(22, 5).HasType(ObjectType::ICON_FLOWER));
201+
CHECK(game.GetMap().At(23, 3).HasType(ObjectType::ICON_EMPTY));
202+
CHECK(game.GetMap().At(23, 4).HasType(ObjectType::ICON_EMPTY));
203+
CHECK(game.GetMap().At(23, 5).HasType(ObjectType::ICON_EMPTY));
204+
}
205+
180206
TEST_CASE("Preprocess - Basic")
181207
{
182208
Game game(MAPS_DIR "baba_is_you.txt");

0 commit comments

Comments
 (0)