I've been trying to write a game in Xcode where a game board is drawn in the terminal using a multidimensional array of type char. The game is supposed to be like a dungeon where there are doors that you can step on and when you do a new room in generated. But sometimes I get this really annoying "(11db)" error thing where one of my lines gets highlighted saying:
"Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)"
It's not the first time I've had the (11db) error in Xcode and previously I've been able to fix it but I can't seem to this time. What does that error mean? What I find the most annoying part it that it doesn't happen every time. Sometimes it'll happen as soon as I run the program when the room is created, other times it happens after you go through a door and I've even been able to go without it. Here's the code where it's happening:
door_x = randomNumber(height - 1, 1);
door_y = randomNumber(width - 1, 1);
//Check to see if door location is the same an an enemy or the player
for (int i = 0; i < enemies.size(); i++){
while ((door_x == enemies.at(i).getX() && door_y == enemies.at(i).getY()) || (door_x == player->getX() && door_y == player->getY())){
door_x = randomNumber(height - 1, 1);
door_y = randomNumber(width - 1, 0);
}
}
room[door_y][door_x] = DOOR;//Place door on map
This error is happening on the last line of the above C++ code. What exactly is wrong and why?
Here's the whole function that creates a new room and my random number function.
int randomNumber(int max, int min){
int randomNumber = rand() %(max - min) + min;
return randomNumber;
}
void create_new_room(){
//Get random room height and room width
height = randomNumber(settings[0], settings[1]);
width = randomNumber(settings[2], settings[3]);
room = new char*[height];
for (int iter = 0; iter != height; iter++) {
room[iter] = new char[width];
}
for (int i = 0; i < height; i++){
for (int j = 0; j < width; j++){
room[i][j] = EMPTY;
}
}
enemies.clear();//Delete every object from the vector
//Create the enemies
int numEnemies = randomNumber(settings[4], settings[5]);
for (int i = 0; i < numEnemies; i++){
int randomHeight = randomNumber(height, 0);
int randomWidth = randomNumber(width, 0);
Enemy *e;//Create pointer to class to allow vector class push back
e = new Enemy(randomWidth, randomHeight, HEALTH);
enemies.push_back(*e);
delete e;//Delete pointer e to free memory and avoid any memory leak.
room[enemies.at(i).getY()][enemies.at(i).getX()] = ENEMY;//Place enemy on board
}
//Create Player
int player_y = randomNumber(height, 0);
int player_x = randomNumber(width, 0);
for (int i = 0; i < enemies.size(); i++){
while (player_x == enemies.at(i).getX()){
player_y = randomNumber(height, 0);
player_x = randomNumber(width, 0);
}
}
player = new Player(player_x, player_y, ATTACK);
//Place player on board
room[player->getY()][player->getX()] = PLAYER;
//Create a door
door_x = randomNumber(height - 1, 1);
door_y = randomNumber(width - 1, 1);
//Check to see if door location is the same an an enemy or the player
for (int i = 0; i < enemies.size(); i++){
while ((door_x == enemies.at(i).getX() && door_y == enemies.at(i).getY()) || (door_x == player->getX() && door_y == player->getY())){
door_x = randomNumber(height - 1, 1);
door_y = randomNumber(width - 1, 1);
}
}
room[door_y][door_x] = DOOR;//Place door on map
}
Settings[] is an array of setting that I have which reads in values from a text file with user settings for the max and min random values. 0 = Max height 1 = Min height 2 = Max width 3 = Min width 4 = Max enemy 5 = Min enemy
Also I use srand(time(0))
at the top of my main function.
My code is a bit messy but here it is.