手动和自动地通过迷宫引导机器人c ++ [英] Guiding a robot through a maze manually and automatically c++
问题描述
整个程序有一个函数可以读取迷宫,这是一个测试文件,它使用X代表墙,减去路径符号,$代表结束位置,+代表起始位置,并将其转换为更好的图形迷宫使用draw_boxes函数绘制不同颜色的方块,具体取决于是否有墙或路径,结束位置或起始位置示例如果它读取X然后它将放入灰色方块(墙)白色 - 用于 - (一条路径),$(最终位置)的红色方块等...。
我画了迷宫,下面的这个功能可以移动一个机器人(只是一个基于命令(箭头键)的蓝色正方形,但现在我需要修改move_robot函数,以便能够让机器人自己移动到它自己,直到它到达终点位置以及手动(用户会输入)确定机器人是自动还是手动移动的命令。
我怎么能这样做?
void move_robot(int& x,int& amp; y)
{
while(true)
{
char c = wait_for_key_typed();
int x_new = x;
int y_new = y;
if(c == - 91)//左箭头键的ASCII
x_new = x-square_side; //(移动蓝色机器人方块左侧1个空格)
否则if(c == - 89)//右箭头键ASCII码
x_new = x + square_side; //(移动蓝色机器人正方形右边1个空格)
否则if(c == - 90)//向上箭头键的ASCII
y_new = y-square_side; //(将蓝色机器人方向向上移动1个空格)
否则if(c == - 88)//向下箭头键的ASCII
y_new = y + square_side; //(移动蓝色机器人正方形向下1个空间)
否则if(c =='x')//退出游戏
hide_window();
if(m_array [(y_new / square_side)+1] [(x_new / square_side)+1]!='X')
//如果位置不是墙,请执行以下操作...
{
draw_robot(x_new,y_new); //调用绘制蓝色方块的函数。
draw_boxes(x,y,' - '); //绘制白框以覆盖之前的机器人位置
x = x_new;
y = y_new; //这些更新位置
}
if(m_array [(y_new / square_side)+1] [(x_new / square_side)+ 1] =='$')
//如果位置是结束位置,请执行以下操作
{
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(); //再次运行迷宫
if(repeat =='r')
{
draw_maze();
int x = square_side * 20;
int y = square_side * 9;
move_robot(x,y);
}
}
}
}
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);
}
}
}
}
推荐答案
用于结束位置,+用于开始位置,并将其转换为更好的图形迷宫我们一个draw_boxes函数,根据是否有墙或路径,结束位置或起始位置绘制不同颜色的正方形示例如果它读取X然后它将放入灰色方块(墙)白色 - 用于 - (路径),红色方块
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
(最终位置)等......
我画了迷宫下面的这个功能可以根据命令(箭头键)移动一个机器人(只是一个蓝色方块),但是现在我需要修改move_robot功能,让机器人能够自己移动到它自己直到它结束位置和手动(用户可以输入命令来确定机器人是自动移动还是手动移动)。
我怎么能这样做?
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
x_new = x-square_side; //(移动蓝色机器人方块左侧1个空格)
否则if(c == - 89)//右箭头键ASCII码
x_new = x + square_side; //(移动蓝色机器人正方形右边1个空格)
否则if(c == - 90)//向上箭头键的ASCII
y_new = y-square_side; //(将蓝色机器人方向向上移动1个空格)
否则if(c == - 88)//向下箭头键的ASCII
y_new = y + square_side; //(移动蓝色机器人正方形向下1个空间)
否则if(c =='x')//退出游戏
hide_window();
if(m_array [(y_new / square_side)+1] [(x_new / square_side)+1]!='X')
//如果位置不是墙,请执行以下操作...
{
draw_robot(x_new,y_new); //调用绘制蓝色方块的函数。
draw_boxes(x,y,' - '); //绘制白框以覆盖之前的机器人位置
x = x_new;
y = y_new; //这些更新位置
}
if(m_array [(y_new / square_side)+1] [(x_new / square_side)+ 1] =='
(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]=='
')
//如果位置是结束位置,请执行以下操作
{
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(); //再次运行迷宫
if(repeat ==' r')
{
draw_maze();
int x = square_side * 20;
int y = square_side * 9;
move_robot(x,y);
}
}
}
}
')
// 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);
}
}
}
}
这篇关于手动和自动地通过迷宫引导机器人c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!