Rally X
ELEN3009 Project by Justin Wernick and David Schneider
source/logic/Maze.cpp
Go to the documentation of this file.
00001 #include "Maze.h"
00002 
00003 Maze::Maze()
00004     :_width(0),
00005     _height(0)
00006 {
00007 }
00008 
00009 void Maze::generateMaze(const vector<pair<int,int> >& walls, int maxObjectX, int maxObjectY)
00010 {
00011     //find bounds so that rectangular vector can be generated
00012     int maxX = maxObjectX;
00013     int maxY = maxObjectY;
00014     for (vector<pair<int,int> >::const_iterator iter = walls.begin(); iter!=walls.end(); ++iter)
00015     {
00016         if (iter->first > maxX) maxX = iter->first;
00017         if (iter->second > maxY) maxY = iter->second;
00018     }
00019     _width = maxX+1; //need to convert from highest index to required size
00020     _height = maxY+1;
00021 
00022 
00023     _wallLocations.clear();
00024 
00025     for (int x=0; x<_width; ++x)
00026     {
00027         _wallLocations.push_back(vector<bool>());
00028         for (int y=0; y<_height; ++y)
00029         {
00030             _wallLocations.back().push_back(false);
00031         }
00032     }
00033 
00034     for (vector<pair<int,int> >::const_iterator iter = walls.begin(); iter!=walls.end(); ++iter)
00035     {
00036         _wallLocations.at(iter->first).at(iter->second) = true;
00037     }
00038 }
00039 
00040 bool Maze::getSolid(const int& x, const int& y) const
00041 {
00042     if (x<0 || y<0) return true;
00043     if (x>=width() || y>=height()) return true;
00044     //bounds have already been checked, can use more efficient, less safe, indexing
00045     return _wallLocations[x][y];
00046 }
00047 
00048 int Maze::width() const
00049 {
00050     return _width;
00051 }
00052 int Maze::height() const
00053 {
00054     return _height;
00055 }
00056 
00057 Maze::Direction Maze::backwards(Direction forwards)
00058 {
00059     Direction backwards;
00060     switch (forwards)
00061     {
00062         case LEFT:
00063             backwards = RIGHT;
00064             break;
00065         case RIGHT:
00066             backwards = LEFT;
00067             break;
00068         case UP:
00069             backwards = DOWN;
00070             break;
00071         case DOWN:
00072             backwards = UP;
00073             break;
00074     }
00075 
00076     return backwards;
00077 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator