Exemple #1
0
bool Maze::findMazePath(int x, int y)

{   
    Cell* cell;
    
    Row* row = rowAt(y);
    
    if (x < 0 || y < 0 || x >= row->size() || y >= rows.size()) 
    {
        return false;
    }
    cell = cellAt(x, y);
    if (cell->isFinishPoint()) 
    {
        cell->setInsidePath(true);
        return true;
    } else if (!cell->isInsideMaze() || cell->isInsidePath()) 
    {
        return false;
        
        // Recursive case.
        // Attempt to find a path from each neighbour.
        // Tentatively mark cell as on path.
        
    } else 
    {
        cell->setInsidePath(true);
        if (findMazePath(x - 1, y) 
            || findMazePath(x + 1, y) 
            || findMazePath(x, y - 1)
            || findMazePath(x, y + 1)) 
        {
            return true;
        } else 
        {
            cell->setInsidePath(false);
            return false;
        }
    }
}
Exemple #2
0
bool Maze::isValid()
{
    int sumStartPoint = 0;
    int sumFinishPoint = 0;
    
    for (int y = 0; y < rows.size(); y++) 
    {
        int x = 0;
        
        Row* row = rowAt(y);
        
        /* Checking errors on cells outside the maze (hasn't met its first
         wall) */
        
        for (int i = 0; i < row->size() && !cellAt(i, y)->isWall(); i++) 
        {
            Cell* cell = cellAt(i, y);
            cell->setInsideMaze(false);       //Set cell as outside the maze
            if (!cell->isValid()) 
            {
                cout << "Invalid character/s"; 
                cout << endl;
                return false;
            }
            if (cell->isStartPoint()) 
            {
                cout << "Error - start declared outside of maze\n";
                return false;
            }
            if (cell->isFinishPoint()) 
            {
                cout << "Error - finish declared outside of maze\n";
                return false;
            }
            x++;      // Keep x-coordinate moving
        }
        
        /* Checking invalid chars on cells inside the maze and counts on start
         and finish points */
        
        for (; x < row->size(); x++) 
        {
            Cell* cell = cellAt(x, y); 
            
            if (!cell->isWall()) 
            {
                cell->setInsideMaze(true);        //Set cell as inside the maze
                cell->setInsidePath(false);       //Set cell as inside the path
            }
            
            if (!cell->isValid()) 
            {
                cout << "Invalid character/s" << endl;
                return false;
            } else if (cell->isStartPoint()) 
            {
                cellStartX = x;       //Store start cell coordinate
                cellStartY = y;
                sumStartPoint++;
            } else if (cell->isFinishPoint()) 
            {
                sumFinishPoint++;
            }
        } 
    }
    
    return (sumStartPointCheck(sumStartPoint) || 
        sumFinishPointCheck(sumFinishPoint))? 1 : 0;
}
Exemple #3
0
int Row::size() const
{
    return cells.size();
}