Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Maze::positionConsideringTunnel() #100

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions demo/include/utils/maze.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <algorithm>
#include <optional>
#include <utility>

Expand Down Expand Up @@ -56,7 +57,7 @@ class Maze {
if (isPassableCell(wrappedPosition)) {
return wrappedPosition;
}
return position;
return {std::clamp(position.x, 0, width() - 1), std::clamp(position.y, 0, height() - 1)};
ll-nick marked this conversation as resolved.
Show resolved Hide resolved
}

bool isDot(const Position& position) const {
Expand Down Expand Up @@ -87,7 +88,7 @@ struct BaseCell {
using TileType = demo::TileType;
using Position = demo::Position;

BaseCell(const Position& position, const TileType& type) : position(position), type(type) {};
BaseCell(const Position& position, const TileType& type) : position(position), type(type){};

double manhattanDistance(const Position& other) const {
return std::abs(position.x - other.x) + std::abs(position.y - other.y);
Expand All @@ -112,7 +113,7 @@ class MazeAdapter {
using MazeStateConstPtr = std::shared_ptr<const MazeState>;
using Position = demo::Position;

explicit MazeAdapter(Maze::ConstPtr maze) : maze_(std::move(maze)), cells_({maze_->width(), maze_->height()}) {};
explicit MazeAdapter(Maze::ConstPtr maze) : maze_(std::move(maze)), cells_({maze_->width(), maze_->height()}){};

CellT& cell(const Position& position) const {
if (!cells_[{position.x, position.y}]) {
Expand Down
37 changes: 37 additions & 0 deletions demo/test/maze.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "utils/maze.hpp"

#include <gtest/gtest.h>

namespace utils::a_star {

using namespace demo;

TEST(MazeState, positionConsideringTunnel) {
const char str[] = {"### #"
"# "
"### #"};
Maze maze{makeCustomMazeState({5, 3}, str)};

// Regular position -> no wrapping
EXPECT_EQ(maze.positionConsideringTunnel({1, 1}).x, 1);
EXPECT_EQ(maze.positionConsideringTunnel({1, 1}).y, 1);

// Regular position -> no wrapping
EXPECT_EQ(maze.positionConsideringTunnel({4, 1}).x, 4);
EXPECT_EQ(maze.positionConsideringTunnel({4, 1}).y, 1);

// Position out of maze, without tunnel -> clamping
EXPECT_EQ(maze.positionConsideringTunnel({5, 1}).x, 4);
EXPECT_EQ(maze.positionConsideringTunnel({5, 1}).y, 1);

// Regular position -> no wrapping
EXPECT_EQ(maze.positionConsideringTunnel({3, 0}).x, 3);
EXPECT_EQ(maze.positionConsideringTunnel({3, 0}).y, 0);

// Position out of maze, with tunnel -> wrapping
EXPECT_EQ(maze.positionConsideringTunnel({3, -1}).x, 3);
EXPECT_EQ(maze.positionConsideringTunnel({3, -1}).y, 2);
}


} // namespace utils::a_star
Loading