When I run my program, everything compiles until the Maze
class is instantiated. I get a warning saying Code will never be executed
in my main.cpp. The compiler (Xcode) redirects me to The LLVM Compiler Infrastructure
, where on line 1590
it says Thread 1: EXC_BAD_ACCESS (code=1, address=0x8)
. The block of code where this is indicated is:
template <class _Tp, class _Allocator>
inline _LIBCPP_INLINE_VISIBILITY
void
vector<_Tp, _Allocator>::push_back(const_reference __x)
{
if (this->__end_ != this->__end_cap()) //Thread 1: EXC_BAD_ACCESS (code=1, address=0x8)
{
__RAII_IncreaseAnnotator __annotator(*this);
__alloc_traits::construct(this->__alloc(),
_VSTD::__to_raw_pointer(this->__end_), __x);
__annotator.__done();
++this->__end_;
}
else
__push_back_slow_path(__x);
}
main.cpp:
#include "Maze.h"
#include <iostream>
#include <string>
int main(int argc, char* argv[])
{
std::cout << "-------------\tMaze\t-------------\n\n";
Maze maze(10);
std::string direction;
while(!true)
{
//Code will never be executed warning HERE
std::cout << "Input a character to move your avatar in the maze and press RETURN\n";
std::cout << "(w = UP, s = DOWN, a = LEFT, d = RIGHT)\n";
std::cin >> direction;
maze.takeTurn(direction);
maze.printMe();
}
return 0;
}
My header file:
#ifndef MAZE_H
#define MAZE_H
#include <vector>
#include <string>
extern const std::string Up;
extern const std::string Down;
extern const std::string Left;
extern const std::string Right;
class Maze
{
private:
char avatarToken; //character that represents the avatar
char emptyToken; //character that represents empty space in the maze
char wallToken; //character that represents a wall
char exitToken; //character that represents an exit - where you quit the game
char stairsUpToken; //character that represents stairs that take you to an UPPER level
char stairsDownToken; //character that represents a Hole that takes you a LOWER level
char bossToken; //character that represents a boss that kills you (you lose/exit the game)
int dim; //width of this square maze
std::vector< std::vector<char> > grid; //a vector of vectors of characters
int avatarRow; //row position of our avatar from zero to dim-1 (NOT from 1 to dim)
int avatarCol; //column position of our avatar from zero to dim-1 (NOT from 1 to dim)
std::vector< std::string > levels; //BONUS: our N-strings that each represent a level except for level[ 0 ]
//see MazeData.h
public:
//constructor
Maze(int newDim = 4,
char _avatarToken = 'A',
char _emptyToken = ' ',
char _wallToken = '*',
char _exitToken = 'E',
char _stairsUpToken = '^',
char _stairsDownToken = 'V',
char _bossToken = 'B');
void printMe();
bool takeTurn(std::string direction); //move the avatar in the requested direction if possible
};
#endif
My implementation file:
#include "Maze.h"
#include "Keyboard.h" //contains global variables: Up, Down, Left, Right
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
//1st constructor
Maze::Maze(int newDim,
char _avatarToken,
char _emptyToken,
char _wallToken,
char _exitToken,
char _stairsUpToken,
char _stairsDownToken,
char _bossToken) : dim(newDim),
avatarToken(_avatarToken),
emptyToken(_emptyToken),
wallToken(_wallToken),
exitToken(_exitToken),
stairsUpToken(_stairsUpToken),
stairsDownToken(_stairsDownToken),
bossToken(_bossToken)
{
//place avatar location in center of maze
this->avatarRow = newDim / 2;
this->avatarCol = newDim / 2;
//initialize top wall of grid
for(int i = 0; i < this->dim; i++)
this->grid[0].push_back(this->wallToken);
//initialize middle of grid
for(int j = 1; j < this->dim - 1; j++)
for(std::vector<char>::iterator it = this->grid[j].begin(); it < this->grid[j].end(); it++)
it == this->grid[j].begin() ||
it == this->grid[j].end() ? this->grid[j].push_back(this->wallToken) :
this->grid[j].push_back(this->emptyToken);
//initialize bottom wall of grid
for(int l = 0; l < this->dim; l++)
this->grid[dim - 1].push_back(this->wallToken);
//place exit token in random spot in the maze
srand( (unsigned int) time(0)); //random seed for exit token placement in maze
int rand_wall = rand() % 4 + 1; //determine which wall exit token is on
int rand_placement = rand() % this->dim + 1; //placement of exit on wall
switch(rand_wall)
{
case 1:
this->grid[0][rand_placement] = this->exitToken;
break;
case 2:
this->grid[rand_placement][0] = this->exitToken;
break;
case 3:
this->grid[this->dim - 1][rand_placement] = this->exitToken;
break;
case 4:
this->grid[rand_placement][this->dim - 1] = this->exitToken;
break;
}
//place stairs up token in random spot in the maze
srand( (unsigned int) time(0)); //random seed for stairs up token placement in maze
rand_wall = rand() % 4 + 1; //determine which wall stairs up token is on
rand_placement = rand() % this->dim + 1; //placement of stairs up on wall
switch(rand_wall)
{
case 1:
//check to see if element is occupied by exit token
if(this->grid[0][rand_placement] != this->exitToken)
this->grid[0][rand_placement] = this->stairsUpToken;
break;
case 2:
//check to see if element is occupied by exit token
if(this->grid[rand_placement][0] != this->exitToken)
this->grid[rand_placement][0] = this->stairsUpToken;
break;
case 3:
//check to see if element is occupied by exit token
if(this->grid[this->dim - 1][rand_placement] != this->exitToken)
this->grid[this->dim - 1][rand_placement] = this->stairsUpToken;
break;
case 4:
//check to see if element is occupied by exit token
if(this->grid[rand_placement][this->dim - 1] != this->exitToken)
this->grid[rand_placement][this->dim - 1] = this->stairsUpToken;
break;
}
//place stairs down token in random spot in the maze
srand( (unsigned int) time(0)); //random seed for stairs down token placement in maze
rand_wall = rand() % 4 + 1; //determine which wall stairs down token is on
rand_placement = rand() % this->dim; //placement of stairs down on wall
switch(rand_wall)
{
case 1:
//check to see if element is occupied by exit token or stairs up token
if(this->grid[0][rand_placement] != this->exitToken ||
this->grid[0][rand_placement] != this->stairsUpToken)
{
this->grid[0][rand_placement] = this->stairsDownToken;
}
break;
case 2:
//check to see if element is occupied by exit token or stairs up token
if(this->grid[rand_placement][0] != exitToken ||
this->grid[rand_placement][0] != this->stairsUpToken)
{
this->grid[rand_placement][0] = this->stairsDownToken;
}
break;
case 3:
//check to see if element is occupied by exit token or stairs up token
if(this->grid[this->dim - 1][rand_placement] != this->exitToken ||
this->grid[this->dim - 1][rand_placement] != this->stairsUpToken)
{
this->grid[this->dim - 1][rand_placement] = this->stairsDownToken;
}
break;
case 4:
//check to see if element is occupied by exit token or stairs up token
if(this->grid[rand_placement][this->dim - 1] != this->exitToken ||
this->grid[rand_placement][this->dim - 1] != this->stairsUpToken)
{
this->grid[rand_placement][this->dim - 1] = this->stairsDownToken;
}
break;
}
//place boss token in random spot inside the maze
srand( (unsigned int) time(0)); //random seed for boss token placement in maze
rand_placement = rand() % this->dim; //placement of boss token
switch(rand_wall)
{
case 1:
//check to see if element is occupied by exit token or stairs up token
if(this->grid[0][rand_placement] != this->exitToken ||
this->grid[0][rand_placement] != this->stairsUpToken)
{
this->grid[0][rand_placement] = this->stairsDownToken;
}
break;
case 2:
//check to see if element is occupied by exit token or stairs up token
if(this->grid[rand_placement][0] != exitToken ||
this->grid[rand_placement][0] != this->stairsUpToken)
{
this->grid[rand_placement][0] = this->stairsDownToken;
}
break;
case 3:
//check to see if element is occupied by exit token or stairs up token
if(this->grid[this->dim - 1][rand_placement] != this->exitToken ||
this->grid[this->dim - 1][rand_placement] != this->stairsUpToken)
{
this->grid[this->dim - 1][rand_placement] = this->stairsDownToken;
}
break;
case 4:
//check to see if element is occupied by exit token or stairs up token
if(this->grid[rand_placement][this->dim - 1] != this->exitToken ||
this->grid[rand_placement][this->dim - 1] != this->stairsUpToken)
{
this->grid[rand_placement][this->dim - 1] = this->stairsDownToken;
}
break;
}
}
//print maze row by row
void Maze::printMe()
{
//rows
for(int m = 0; m < this->dim; m++)
{
//columns
for(int n = 0; n < this->dim; n++)
{
//print avatar token
if(m == this->avatarRow && n == this->avatarCol)
std::cout << this->avatarToken;
else
std::cout << this->grid[m][n];
}
std::cout << std::endl;
}
}
//update location of the avatar
bool Maze::takeTurn(std::string _direction)
{
if(_direction == Up && this->grid[this->avatarRow - 1][this->avatarCol] != this->wallToken)
{
--this->avatarRow;
if(this->grid[this->avatarRow - 1][this->avatarCol] == this->exitToken ||
this->grid[this->avatarRow - 1][this->avatarCol] == this->bossToken ||
this->grid[this->avatarRow - 1][this->avatarCol] == this->stairsUpToken ||
this->grid[this->avatarRow - 1][this->avatarCol] == this->stairsDownToken)
{
std::cout << "You exited the maze.\n";
return true;
}
else return false;
}
else if(_direction == Down && this->grid[this->avatarRow + 1][this->avatarCol] != this->wallToken)
{
++this->avatarRow;
if(this->grid[this->avatarRow + 1][this->avatarCol] == this->exitToken ||
this->grid[this->avatarRow + 1][this->avatarCol] == this->bossToken ||
this->grid[this->avatarRow + 1][this->avatarCol] == this->stairsUpToken ||
this->grid[this->avatarRow + 1][this->avatarCol] == this->stairsDownToken)
{
std::cout << "You exited the maze.\n";
return true;
}
else return false;
}
else if(_direction == Left && this->grid[this->avatarRow][this->avatarCol - 1] != this->wallToken)
{
--this->avatarCol;
if(this->grid[this->avatarRow][this->avatarCol - 1] != this->exitToken ||
this->grid[this->avatarRow][this->avatarCol - 1] != this->bossToken ||
this->grid[this->avatarRow][this->avatarCol - 1] != this->stairsUpToken ||
this->grid[this->avatarRow][this->avatarCol - 1] != this->stairsDownToken)
{
std::cout << "You exited the maze.\n";
return true;
}
else return false;
}
else if(_direction == Right && this->grid[this->avatarRow][this->avatarCol + 1] != this->wallToken)
{
++this->avatarCol;
if(this->grid[this->avatarRow][this->avatarCol + 1] != this->exitToken ||
this->grid[this->avatarRow][this->avatarCol + 1] != this->bossToken ||
this->grid[this->avatarRow][this->avatarCol + 1] != this->stairsUpToken ||
this->grid[this->avatarRow][this->avatarCol + 1] != this->stairsDownToken)
{
std::cout << "You exited the maze.\n";
return true;
}
return false;
}
else return false;
}
Aucun commentaire:
Enregistrer un commentaire