I have a project that asks the user how many robots they want to create, the user then names the robots and has the option to move the robots in one of 4 directions along the X/Y axis.
The program allows the user to move one robot in one direction per turn: up, down, left, or right. If the user chooses up, they will move up one unit. If the user continues to choose one direction consecutively, they will be able to move farther in that direction. For example, if a user moves up, they will move up one unit. If the user moves up again, they will move up 2 units. If the user moved up for the third time, they will move up 3 units. Users can do this a maximum of 4 times in a row, meaning that they can move a maximum of 4 units per turn, even if the user goes in one direction for 4, 7, 18, etc times in a row. This is pattern is broken and starts again if the user chooses a different direction.
So far I have only tried test cases but it doesn't seem like my speed
variable is increasing. I tried to make it to where if userDirection
is the same as lastCommand
, a variable called speed
will increase by one, in theory, increasing each time the user moves the robot in the same direction. However, this is not the case. speed
is only increasing to 1 for some reason.
I want to test the code by only going up. Moving one robot only going up, it should show (0,1), then (0,3), then (0,6), then (0,10), (0,14), (0,18) and so on, first increasing by one, then two, then three, and maxing out at moving 4 units per turn. However, what I am finding is that the code goes from (0,1) to (0,3), to (0,5), (0,7), (0,9), and so on. Maxing out at only moving 2 units per turn.
I need to know why speed
is not updating even though it has ++
attached to it. If there is any other advice you can give me for my code, it would be greatly appreciated. Thank you!
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct userRobot {
string name;
int Xvalue = 0;
int Yvalue = 0;
int distance = 0;
char lastCommand;
};
void MenuFunction() {
cout << "Please select one of the following:" << endl;
cout << "m - move" << endl << "d - distance" << endl << "q - quit" << endl << endl;
}
void MoveFunction(userRobot RobotArray[], int NumberOfRobots) {
cout << endl << "Which robot would you like to move?" << endl << endl;
string robotName;
cin >> robotName;
for (int k = 0; k < NumberOfRobots; k++) {
if (robotName == RobotArray[k].name) {
cout << endl << "In what direction would you like to move " << RobotArray[k].name << "?" << endl;
cout << "Select: u - up / d - down / l - left / r - right" << endl << endl;
char userDirection;
cin >> userDirection;
int speed = 0;
if (userDirection == 'u' || userDirection == 'U') {
if (userDirection == RobotArray[k].lastCommand) {
speed++;
if(speed > 4)
{
speed = 4;
}
RobotArray[k].Yvalue += speed;
}
RobotArray[k].Yvalue++;
RobotArray[k].distance++;
cout << endl << RobotArray[k].name << "'s position has been updated to ";
cout << "(" << RobotArray[k].Xvalue << "," << RobotArray[k].Yvalue << ")" << endl << endl;
RobotArray[k].lastCommand = 'u';
} else if (userDirection == 'd' || userDirection == 'D') {
RobotArray[k].Yvalue--;
RobotArray[k].distance++;
cout << endl << RobotArray[k].name << "'s position has been updated to ";
cout << "(" << RobotArray[k].Xvalue << "," << RobotArray[k].Yvalue << ")" << endl << endl;
} else if (userDirection == 'l' || userDirection == 'L') {
RobotArray[k].Xvalue--;
RobotArray[k].distance++;
cout << endl << RobotArray[k].name << "'s position has been updated to ";
cout << "(" << RobotArray[k].Xvalue << "," << RobotArray[k].Yvalue << ")" << endl << endl;
} else if (userDirection == 'r' || userDirection == 'R') {
RobotArray[k].Xvalue++;
RobotArray[k].distance++;
cout << endl << RobotArray[k].name << "'s position has been updated to ";
cout << "(" << RobotArray[k].Xvalue << "," << RobotArray[k].Yvalue << ")" << endl << endl;
} else {
cout << endl << "Sorry, that is not a valid direction, please input either 'u', 'd', 'l', or 'r'" << endl;
cout << "Returning to main menu..." << endl << endl;
}
return;
}
}
cout << endl << "Sorry, we couldn't find that robot. Kindly check your spelling and try again." << endl;
cout << "Returning to main menu..." << endl << endl;
}
int main() {
cout << "Enter the number of robots" << endl << endl;
int NumberOfRobots;
cin >> NumberOfRobots;
vector < userRobot > RobotArray(NumberOfRobots);
cout << endl << "Enter their name(s)" << endl << endl;
for (int i = 0; i < NumberOfRobots; i++) {
cin >> RobotArray[i].name;
}
cout << endl;
cout << "Welcome to MultiRobo Guider!" << endl;
MenuFunction();
char input;
cin >> input;
while (input != 'q' && input != 'Q') {
if (input == 'm' || input == 'M') {
MoveFunction(RobotArray.data(), NumberOfRobots);
MenuFunction();
cin >> input;
} else if (input == 'd' || input == 'D') {
for (int j = 0; j < NumberOfRobots; j++) {
cout << endl << RobotArray[j].name << " has moved a total of " << RobotArray[j].distance << " time(s)";
cout << endl << RobotArray[j].name << "'s position is (" << RobotArray[j].Xvalue << "," << RobotArray[j].Yvalue << ")" << endl;
}
cout << endl << endl;
MenuFunction();
cin >> input;
} else {
cout << endl << "Sorry, that is not a valid menu input, please input either 'm', 'd', or 'q'" << endl << endl;
MenuFunction();
cin >> input;
}
}
cout << endl << "Goodbye, and thank you for using MultiRobo Guider!";
}