diff --git a/src/main.cc b/src/main.cc index 49af91e..43156a5 100644 --- a/src/main.cc +++ b/src/main.cc @@ -55,6 +55,7 @@ class Bot { int x, y; bool up, right, down, left; std::map, unsigned> known_map; + int field_width, field_height; asio::awaitable Protocol(); asio::awaitable Join(); @@ -114,8 +115,9 @@ asio::awaitable Bot::Protocol() { } else if (field == "error") { std::getline(iss, field); std::cout << "-> Error: " << field << std::endl; - } else if (field == "goal") { - iss >> goal_x >> dummy >> goal_y; + } else if (field == "game") { + iss >> field_width >> dummy >> field_height >> dummy >> goal_x >> + dummy >> goal_y; std::cout << "-> Goal is at (" << goal_x << ", " << goal_y << ")" << std::endl; known_map.clear(); @@ -205,7 +207,8 @@ unsigned Bot::ShortestPath(int x, int y) { auto pos = std::make_pair(position.first - 1, position.second); queue.emplace(distance + 1 + AStarHeuristic(pos), pos); } - if (!(known_map[position] & static_cast(Direction::RIGHT))) { + if (!(known_map[position] & static_cast(Direction::RIGHT)) && + position.first < field_width) { auto pos = std::make_pair(position.first + 1, position.second); queue.emplace(distance + 1 + AStarHeuristic(pos), pos); } @@ -214,7 +217,8 @@ unsigned Bot::ShortestPath(int x, int y) { auto pos = std::make_pair(position.first, position.second - 1); queue.emplace(distance + 1 + AStarHeuristic(pos), pos); } - if (!(known_map[position] & static_cast(Direction::DOWN))) { + if (!(known_map[position] & static_cast(Direction::DOWN)) && + position.second < field_height) { auto pos = std::make_pair(position.first, position.second + 1); queue.emplace(distance + 1 + AStarHeuristic(pos), pos); }