通过迷宫自动和手动引导机器人c ++

时间:2014-04-29 19:24:20

标签: c++ maze

整个程序有一个函数可以读入迷宫,它是一个使用的测试文件 X表示墙,减去路径符号,$表示结束位置,+表示起始位置, 并使用draw_boxes函数将其转换为更好的图形迷宫 在不同颜色的方块中,取决于是否有墙或路径,结束位置 或开始位置示例如果它读取X然后它将放入灰色方块(墙) 白色 - (一条路径),红色方块($(最终位置)等等...

我绘制了迷宫,下面的这个功能可以移动机器人 (只是一个蓝色方块)基于命令(箭头键),但现在我需要 修改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 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);
            }
        }  
    }
}

0 个答案:

没有答案
相关问题