patterncppModerate
Maze generation
Viewed 0 times
mazegenerationstackoverflow
Problem
I have been working on a maze generator in C++ in an effort to learn the language and brush up on some long lost knowledge. I want to ensure that I am using best practices, and really doing things the way that they should be done.
My code is safely tucked into a GitHub repository. I would like to have two specific classes reviewed: Maze.h (154 lines) (and MazeConstants.h if possible), as well as MazePosition.h (45 lines).
I also had a couple questions. Is it advisable to overload operator!= in addition to
File: MazePosition.h (45 Lines)
``
My code is safely tucked into a GitHub repository. I would like to have two specific classes reviewed: Maze.h (154 lines) (and MazeConstants.h if possible), as well as MazePosition.h (45 lines).
I also had a couple questions. Is it advisable to overload operator!= in addition to
operator==? I overload operator== in the MazePosition class, but not operator!=. Is it good practice to provide copy constructors and overloaded assignment operators as well? I am mostly confused about best practices in these areas, and the info I have found online has been sparse and mostly just pages with illustrated examples. I plan on disabling any sort of copying ability in the Maze.h class by doing something like this: (boost.org/doc/libs/1_47_0/boost/noncopyable.hpp) because it does not make sense to copy that object for any reason (too slow anyway, right?).File: MazePosition.h (45 Lines)
``
/*
* File: MazePosition.h
* Author: mattosaurus
*
* Created on August 6, 2011, 4:26 PM
*/
#ifndef MAZEPOSITION_H
#define MAZEPOSITION_H
class MazePosition{
public:
MazePosition(int row, int col) : _row(row), _col(col){}
int getRow() const{
return _row;
}
int getCol() const{
return _col;
}
MazePosition operator+(MazePosition & p2) const{
int newX = _row + p2.getRow();
int newY = _col + p2.getCol();
MazePosition local(newX, newY);
return local;
}
friend bool operator==(MazePosition const&p1, MazePosition const&p2);
private:
int _row;
int _col;
};
bool operator==(MazePosition const&p1, MazePosition const&p2) {
if(p1.getRow() == p2.getRow()){
return (p1.getCol() == p2.getCol());
}
return false;
}
#endif / MAZEPOSITION_H /
`Solution
My biggest bugbear:
Why are you exposing internal members via getters!!!
Do other people need to know what position the MazeLocation points at?
And
Exposing them tightly binds your object to anything that uses them. As far as I can tell the only thing that will ever need it to know the maze position is a Maze. Thus you should Make 'Maze' a friend of 'MazePosition' and remove the getters (if you are going to tightly bind stuff together make the number of bindings as small as possible.
As pointed out elsewhere: you only need to use
But I would rather user iterators:
This makes the code easier to translate for the standard algorithms when you learn how to use them.
Why are you exposing internal members via getters!!!
Do other people need to know what position the MazeLocation points at?
int getRow() const{
return _row;
}
int getCol() const{
return _col;
}And
int getHeight(){
return _height;
}
int getWidth(){
return _width;
}Exposing them tightly binds your object to anything that uses them. As far as I can tell the only thing that will ever need it to know the maze position is a Maze. Thus you should Make 'Maze' a friend of 'MazePosition' and remove the getters (if you are going to tightly bind stuff together make the number of bindings as small as possible.
As pointed out elsewhere: you only need to use
at() if you have unvalidated input and are using it to index the array. Your code is using validated input (as you are looping from start to end), unfortunately you make the mistake of using capacity() rather than size() which is why it was throwing.for(int j = 0; j < maze[i].size(); j++)
{ // ^^^^^^
maze[i][j] = MazeConstants::INVALID;
// ^^^^^^ guranteed to be goodBut I would rather user iterators:
for(IntMaze::iterator loop = maze.begin(); loop != maze.end(); ++loop)
{ // ^^ Prefer pre-increment
for(IntRow::iterator inner = loop->begin(); inner != loop->end(); ++inner)
{This makes the code easier to translate for the standard algorithms when you learn how to use them.
Code Snippets
int getRow() const{
return _row;
}
int getCol() const{
return _col;
}int getHeight(){
return _height;
}
int getWidth(){
return _width;
}for(int j = 0; j < maze[i].size(); j++)
{ // ^^^^^^
maze[i][j] = MazeConstants::INVALID;
// ^^^^^^ guranteed to be goodfor(IntMaze::iterator loop = maze.begin(); loop != maze.end(); ++loop)
{ // ^^ Prefer pre-increment
for(IntRow::iterator inner = loop->begin(); inner != loop->end(); ++inner)
{Context
StackExchange Code Review Q#4541, answer score: 10
Revisions (0)
No revisions yet.