I have this function that returns the location of a robot (which is just a [row][col] pair of indices from a matrix):
std::pair<int, int> World::getRobotLocation(char robot_name){
auto const & location = robots.find(robot_name);
if (location == robots.end()) {
std::cout << "Robot " << robot_name << " does not exist." << std::endl;
}
return location->second;
}
Below, I am trying to implement the move()
function, which takes in the robot name, location and which direction to move and updates the position accordingly:
std::string move(char robot, char direction) {
// Get robot and its location
std::pair<int, int> robot_location = std::pair<int, int> World::getRobotLocation(robot);
// Get direction to move from user
// if L, map_[row+1][col]
// if D, map_[row][col+1]
// if R, map_[row-1][col]
// if U, map_[row][col+1]
// According to user input, update the robot's location
if (direction == 'L') {
robot_location = robot_location[+1][]
}
else if (direction == 'D') {
robot_location = robot_location[][-1]
}
else if (direction == 'R') {
robot_location = robot_location[-1][]
}
else {
robot_location = robot_location[][+1]
}
}
In my variable robot_location
, I am saving the location of that particular robot. How can I access the values of this std::pair<int, int>
to be able to update them?