The whole program has a function that reads in the maze that is a test file that uses X's for walls, minus symbols for paths, a $ for end position, and + for start position, and converts it to a better graphics maze using a draw_boxes function that draws in squares of different color depending on if there is a wall or a path,end position or start position Example if it reads an X then it would put in a gray square(a wall) a white one for - (a path),red square for $(final position) etc... .
I have the maze drawn and this function below is able to move a robot (just a blue square) based on commands(arrow keys), but now i need to modify the move_robot function to be able to have the robot move to the on its own until it reaches the end position as well as manually (the user would type a command to determine whether the robot will move automatically or manually).
How could i do this?
void move_robot(int &x, int &y)
{
while(true)
{
char c=wait_for_key_typed();
int x_new=x;
int y_new=y;
if(c==-91) //ASCII for left arrow key
x_new=x-square_side; //(move blue robot square left 1 space)
else if(c==-89) //ASCII for right arrow key
x_new=x+square_side; //(move blue robot square right 1 space)
else if(c==-90) //ASCII for up arrow key
y_new=y-square_side; //(move blue robot square up 1 space)
else if(c==-88) //ASCII for down arrow key
y_new=y+square_side; //(move move blue robot square down 1 space)
else if (c == 'x') //exits the game
hide_window();
if(m_array[(y_new/square_side)+1][(x_new/square_side)+1]!='X')
// if position is not a wall do the following...
{
draw_robot(x_new,y_new); // calls a function that draws a blue square.
draw_boxes(x,y,'-'); // draws white box to cover previous robot position
x=x_new;
y=y_new; //these update the position
}
if(m_array[(y_new/square_side)+1][(x_new/square_side)+1]=='$')
// if position is the end position do the following
{
move_to(window_length/2,window_width/2);
set_pen_width(2);
set_pen_color(color::blue);
write_string("WINNING!!!");
char repeat=wait_for_key_typed();//to run the maze again
if(repeat=='r')
{
draw_maze();
int x=square_side*20;
int y=square_side*9;
move_robot(x,y);
}
}
}
}