From 98ba22e7064db57316dfff1ae127feb3dceeb73e Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Thu, 31 Jul 2014 13:58:22 +0200 Subject: Initial commit --- source/logic/Maze.cpp | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 source/logic/Maze.cpp (limited to 'source/logic/Maze.cpp') diff --git a/source/logic/Maze.cpp b/source/logic/Maze.cpp new file mode 100644 index 0000000..ab24035 --- /dev/null +++ b/source/logic/Maze.cpp @@ -0,0 +1,77 @@ +#include "Maze.h" + +Maze::Maze() + :_width(0), + _height(0) +{ +} + +void Maze::generateMaze(const vector >& walls, int maxObjectX, int maxObjectY) +{ + //find bounds so that rectangular vector can be generated + int maxX = maxObjectX; + int maxY = maxObjectY; + for (vector >::const_iterator iter = walls.begin(); iter!=walls.end(); ++iter) + { + if (iter->first > maxX) maxX = iter->first; + if (iter->second > maxY) maxY = iter->second; + } + _width = maxX+1; //need to convert from highest index to required size + _height = maxY+1; + + + _wallLocations.clear(); + + for (int x=0; x<_width; ++x) + { + _wallLocations.push_back(vector()); + for (int y=0; y<_height; ++y) + { + _wallLocations.back().push_back(false); + } + } + + for (vector >::const_iterator iter = walls.begin(); iter!=walls.end(); ++iter) + { + _wallLocations.at(iter->first).at(iter->second) = true; + } +} + +bool Maze::getSolid(const int& x, const int& y) const +{ + if (x<0 || y<0) return true; + if (x>=width() || y>=height()) return true; + //bounds have already been checked, can use more efficient, less safe, indexing + return _wallLocations[x][y]; +} + +int Maze::width() const +{ + return _width; +} +int Maze::height() const +{ + return _height; +} + +Maze::Direction Maze::backwards(Direction forwards) +{ + Direction backwards; + switch (forwards) + { + case LEFT: + backwards = RIGHT; + break; + case RIGHT: + backwards = LEFT; + break; + case UP: + backwards = DOWN; + break; + case DOWN: + backwards = UP; + break; + } + + return backwards; +} -- cgit v1.2.3