gpt4 book ai didi

c++ - 运行时检查失败 #2 变量 'maze' 周围的堆栈已损坏

转载 作者:行者123 更新时间:2023-11-28 02:13:36 26 4
gpt4 key购买 nike

运行时检查失败 #2 变量“迷宫”周围的堆栈已损坏。每当我编译并运行我的程序时,只要程序完成运行,我就会收到此错误。我相信问题出在我的实现中的 addPaths 函数中。为了以防万一,我发布了所有代码。该程序正在创建一个迷宫,而 addPaths 函数正在“挖掘”供用户移动的路径。路径方向是随机选择的,路径只在迷宫的偶数个空间绘制。

标题:

const int HEIGHT = 3;
const int WIDTH = 5;

class Coordinate
{
public:
int row, column;
};

class Maze
{
public:
Maze();
~Maze();
void buildMaze();
void displayMaze();
void addPaths();
void startGame();
void movePlayer(int);
bool solved();
int getKey();
void addDestinationToGrid();
private:
char grid[WIDTH][HEIGHT];
Coordinate player;

const static char PLAYER = 'P';
const static char DESTINATION = 'X';
const static char START = 'S';
const static char PATH = ' ';
const static char WALL = (char)219;
};

实现:

#include <iostream>
#include "maze.h"
#include <windows.h>
#include <stack>
using std::cout;
using std::endl;


Maze::Maze()
{
buildMaze();
}

Maze::~Maze()
{
cout << "yay";
}

void Maze::buildMaze()
{
for (int x = 0; x <= HEIGHT-1; x++)
{
for (int y = 0; y <= WIDTH-1; y++)
{
grid[x][y] = WALL;
//SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
}
}
}

void Maze::displayMaze()
{
for (int x = 0; x <= HEIGHT-1; x++)
{
for (int y = 0; y <= WIDTH-1; y++)
{
cout << grid[x][y];
}
cout << endl;
}
}
void Maze::startGame()
{
int input;
do
{
input = getKey();
movePlayer(input);
} while (!solved());
}

bool Maze::solved()
{
return true;
}

void Maze::movePlayer(int direction)
{
if (direction == VK_UP || direction == VK_DOWN || direction == VK_LEFT || direction == VK_RIGHT)
{
COORD newCoord = { player.column + 1, player.row + 1 };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), newCoord);
if (grid[player.row][player.column] == START)
{
cout << START;
}
else
{
cout << PATH;
}

}
}

int Maze::getKey()
{
int result = 0;
while (!solved() && result == 0)
{
short MAX_SHORT = 0x7FFF; //111111111111111
if (GetAsyncKeyState(VK_LEFT) & MAX_SHORT)
{
result = VK_LEFT;
}
else if (GetAsyncKeyState(VK_UP) & MAX_SHORT)
{
result = VK_UP;
}
else if (GetAsyncKeyState(VK_RIGHT) & MAX_SHORT)
{
result = VK_RIGHT;
}
else if (GetAsyncKeyState(VK_DOWN) & MAX_SHORT)
{
result = VK_DOWN;
}
}
return result;
}

void Maze::addPaths()
{
Coordinate currentLocation;
Coordinate startLocation;
//Coordinate endLocation; //not used yet
std::stack<Coordinate> myStack;

currentLocation.row = (((rand() % HEIGHT) / 2) * 2);
currentLocation.column = (((rand() % WIDTH) / 2) * 2);

startLocation = currentLocation;
grid[currentLocation.row][currentLocation.column] = START;
player = currentLocation;
do
{

bool canMoveUp = !(currentLocation.row == 0 || grid[currentLocation.row - 2][currentLocation.column] != WALL);
bool canMoveDown = !(currentLocation.row == HEIGHT - 1 || grid[currentLocation.row + 2][currentLocation.column] != WALL);
bool canMoveLeft = !(currentLocation.column == 0 || grid[currentLocation.row][currentLocation.column - 2] != WALL);
bool canMoveRight = !(currentLocation.column == WIDTH - 1 || grid[currentLocation.row][currentLocation.column + 2] != WALL);

if (canMoveUp || canMoveDown || canMoveLeft || canMoveRight)
{
myStack.push(currentLocation);

//choose random location to dig
bool moveFound = false;
while (moveFound != true)
{
int direction = rand() % 4;
if (direction == 0 && canMoveUp)
{
moveFound = true;
grid[currentLocation.row - 2][currentLocation.column] = PATH;
grid[currentLocation.row - 1][currentLocation.column] = PATH;
currentLocation.row -= 2;
}
else if (direction == 1 && canMoveDown)
{
moveFound = true;
grid[currentLocation.row + 2][currentLocation.column] = PATH;
grid[currentLocation.row + 1][currentLocation.column] = PATH;
currentLocation.row += 2;
}
else if (direction == 2 && canMoveLeft)
{
moveFound = true;
grid[currentLocation.row][currentLocation.column - 2] = PATH;
grid[currentLocation.row][currentLocation.column - 1] = PATH;
currentLocation.column -= 2;
}
else if (direction == 3 && canMoveRight)
{
moveFound = true;
grid[currentLocation.row][currentLocation.column + 2] = PATH;
grid[currentLocation.row][currentLocation.column - 2] = PATH;
currentLocation.column += 2;
}
}
}
else if (!myStack.empty())
{
currentLocation = myStack.top();
myStack.pop();
}

}
while (!myStack.empty());
addDestinationToGrid();
}

void Maze::addDestinationToGrid()
{
int randomRow = rand() % HEIGHT;
int randomColumn = rand() % WIDTH;
while (grid[randomRow][randomColumn] != PATH)
{
randomRow = rand() % HEIGHT;
randomColumn = rand() % WIDTH;
}

grid[randomRow][randomColumn] = DESTINATION;
}

主要内容:

#include <iomanip>
#include <iostream>
#include "maze.h"
using namespace std;

int main()
{
Maze maze;

maze.addPaths();
maze.displayMaze();
maze.startGame();

/*if (maze.solved())
{
cout << " You Win!";
}*/
}

最佳答案

有两个问题。一是您访问 grid 元素的顺序不一致。它被声明为 grid[WIDTH][HEIGHT] 但大多数(但不是全部)访问首先使用基于 HEIGHT 的索引。这不会导致您的问题,因为 WIDTH 大于 HEIGHT 并且您在对它进行正常访问时留在对象的内存中。

问题是这一行:

grid[currentLocation.row][currentLocation.column - 2] = PATH;

在 moveRight 处理程序中。列偏移量应该是+ 1,而不是- 2。这种方式可能会导致您在 grid 的第一个元素之前写入内存。

关于c++ - 运行时检查失败 #2 变量 'maze' 周围的堆栈已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34821456/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com