如何在c++中操纵/移动一个网格中的对象
How to manipulate/move an object across a grid in c++
我有一些麻烦,试图移动一个球周围的障碍朝着一个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);
}
任何帮助将不胜感激!
试试这个:
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;
}
}
以下代码
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
}
相关文章:
- 检查 2D 网格的某个元素是否与另一个元素共享对角线、水平线或垂直线
- 如何使用条件计算 3D 网格中从一个点到另一个点的所有路径
- 无法在一个OpenVDB文件中获得两个网格
- 仅在另一个网眼后面才能渲染网格
- 找出一个正方形逻辑中可能的网格数量,但代码不接受超过六个输入,它停止并说.exe停止工作
- 从源开始,在C++中找到网格上最接近目标的下一个点
- 如何在C++中创建一个网格,这将允许我将每个索引设置为一个向量,以便以后操作该向量,然后绘制它
- 如何创建一个具有空值、递减编号系统和.txt文件输入值的二维阵列网格
- 如何得出结论,如果一个单位的士兵在网格地图上连接
- 有人能建议我如何在网格游戏中移动士兵时解决一个牢房中的旁路问题吗
- 如何在另一个网格(如蒙版)后面渲染网格
- 在一个圆内找到网格的所有点,按范数排序
- 试图显示一个编号的网格,陷入无限循环
- 用2d数组制作一个6x6的网格
- 检测和删除一个网格的隐藏表面
- 沿着二维网格移动一个值
- 如何在c++中操纵/移动一个网格中的对象
- 如何确定一个顶点是否在边界上的网格在CGAL
- 如何在c++中创建一个可变尺寸的网格
- 在环形网格内创建一个3x3长方体