【问题标题】:How to manipulate/move an object across a grid in c++如何在 C++ 中跨网格操作/移动对象
【发布时间】:2016-09-14 04:49:06
【问题描述】:

我在尝试将球绕过障碍物朝 2x2 阵列上的目标移动时遇到了一些麻烦。我有一个函数,它接受两个整数作为参数。然后该函数创建一个大小为 10 的 2x2 数组,用空闲空间填充它,创建一个设置障碍物(障碍物每次都保持不变)。然后它创建目标点(每次都将其分配到同一个位置)和球点(位于数组位置 [x][y] 中)。

目标是让球朝着球门移动,直到撞到障碍物,然后绕过障碍物,同时跟踪障碍物周围每个空间距离球门的距离,然后返回最近的点并继续朝着目标前进。

我正在尝试开发一个 moveToGoal 函数,该函数将球移向目标,因为路上没有障碍物,但我在访问 printGrid 函数之外的网格时遇到了很多麻烦。如果我在打印网格函数之外创建网格,我无法访问它,因为它超出了范围。我知道这可能看起来令人困惑,但我会尝试回答有关它的任何问题。到目前为止,这是我所拥有的:

#include <stdio.h>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <fstream>
#include <string>
#include <unistd.h>

using namespace std;

void printGrid(int x, int y)        //Used to clear old grids, and print a
{                                   //new grid with input location for ball
    system("CLS");
    string grid [10][10];           //Initialize grid array
    for (int i=0; i<10; i++)
    {
        for (int j=0; j<10; j++)
        {
            grid[i][j] = ' ';       //Constructs grid with freespace
        }
    }
    for (int i=2; i<8; i++)         //Constructs obstacle(s)
    {
        grid[5][i]='O';
    }
    grid[2][5] = 'G';               //Sets Goal location
    grid[x][y] = 'B';               //Sets current ball location(starts 8,5)
    for (int i=0; i<10; i++)        //Prints finished grid
    {
        for (int j=0; j<10; j++)
        {
            cout<<grid[i][j];
        }
        cout<<endl;
    }   
}

void moveToGoal(int x, int y)
{
    printGrid(x-1, y);
}

int main()
{
    moveToGoal(8,5);
    sleep(1);
    moveToGoal(7,5);
    sleep(1);
    moveToGoal(6,5);
    sleep(1);
    moveToGoal(5,5);
    sleep(1);
    moveToGoal(4,5);
    sleep(1);
    moveToGoal(3,5); 
}

任何帮助将不胜感激!

【问题讨论】:

  • 为什么需要sleep 电话?
  • 一个“2x2”数组是一个长度为2,宽度为2的数组。你的意思是“二维数组”吗?
  • 您的网格是 printGrid 函数的本地网格。如果您需要在此函数之外对其进行调整,则需要将其作为函数参数来回传递或将其作为全局变量使用。
  • string 是一个重要的对象,您的网格是单个字符,而不是字符串。使用char 而不是string
  • 看看A star 寻路算法(光栅/地图/网格版本不是图形)。您可以计算一次路径(或者如果地图中的条件随时间变化,则偶尔计算一次),然后遍历它...

标签: c++ arrays algorithm multidimensional-array


【解决方案1】:

试试这个:

string grid [10][10];           // the grid array is now global

void printGrid(int x, int y)        //Used to clear old grids, and print a
{                                   //new grid with input location for ball
    system("CLS");
    for (int i=0; i<10; i++)  // clean the old grid
    {
        for (int j=0; j<10; j++)
        {
            grid[i][j] = ' ';       //Constructs grid with freespace
        }
    }
    for (int i=2; i<8; i++)         //Constructs obstacle(s)
    {
        grid[5][i]='O';
    }
    grid[2][5] = 'G';               //Sets Goal location
    grid[x][y] = 'B';               //Sets current ball location(starts 8,5)
    for (int i=0; i<10; i++)        //Prints finished grid
    {
        for (int j=0; j<10; j++)
        {
            cout<<grid[i][j];
        }
        cout<<endl;
    }   
}

【讨论】:

    【解决方案2】:

    在下面的代码中

    int f() {
        int i = 1;
        i++;
        std::cout << i << "\n";
    }
    
    int main() {
        f();
        f();
    }
    

    函数f中的变量i是一个局部变量。这意味着每次调用f() 都会创建一个它的新实例。所以上面代码的输出是2,两次,不是2,然后是3

    在您的代码中,网格是 printGrid 的本地网格,因此每次调用 printGrid 时都会创建网格。

    解决方案是使网格成为您传递给它的参数或全局变量。将网格作为参数传递是首选,您可以考虑将其放在结构或类中以使其更容易执行。

    #include <array>
    #include <utility>
    #include <iostream>
    
    class Grid
    {
        // type alias for a grid of 10x10 chars
        using grid_t = std::array<std::array<char, 10>, 10>;
    
        grid_t grid_;  // our actual grid data: note grid_[y][x].
        int ballX_ = -1, ballY_ = -1;
    
    public:
        // Constructor
        Grid(int goalX, int goalY)
        {
            // populate the grid with free space
            for (auto&& row: grid_) {
                std::fill(row.begin(), row.end(), ' ');
            }
            // add obstacles
            for (size_t i = 2; i < 8; ++i)
                grid_[i][5] = 'O';
            // place the goal
            grid_[goalY][goalX] = 'G';
        }
    
        // place, move or remove the ball on the grid
        void placeBall(int x, int y)
        {
            // Remove the ball from any previous location
            if (ballX_ != -1 && ballY != -1)
                grid_[ballY_][ballX_] = ' ';
    
            // update location
            ballX_ = x, ballY = y;
    
            // Place ball on board if location is not -1,-1
            if (ballX_ != -1 && ballY != -1)
                grid_[ballY_][ballX_] = 'B';
        }
    
        // Return the character at a given location
        char at(int x, int y) const
        {
            return grid_[y][x];
        }
    
        // Query ball position
        int ballX() const { return ballX_; }
        int ballY() const { return ballY_; }
    
        // print the grid
        friend ostream& operator << (ostream&, const Grid&);
    };
    
    ostream& operator<<(ostream& stream, const Grid& grid)
    {
        for (auto&& row: grid.grid_) {
            for (auto&& col: row) {
                stream << col << ' ';
            }
            stream << '\n';
        }
        return stream;
    }
    
    // helper for your cls/sleep thing around printing the grid.
    void printGrid(const Grid& grid)
    {
        system("CLS");
        std::cout << grid;
        sleep(1);
    }
    
    void manipulateGrid(Grid& grid)  // reference to the caller's grid
    {
        printGrid(grid);
        grid.placeBall(8, 5);
        printGrid(grid);
        grid.placeBall(7, 5);
        // ...
    }
    
    int main()
    {
        Grid g(2, 5);  // create grid with [2,5] as the goal
        manipulateGrid(g);  // to demonstrate passing as parameter
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-22
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2023-03-22
      • 2021-04-26
      • 2021-12-30
      相关资源
      最近更新 更多