[go: up one dir, main page]

Menu

[dc7c97]: / src / grid-maze.cc  Maximize  Restore  History

Download this file

133 lines (110 with data), 3.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
* mazen-ng
* Copyright (C) Jacob Zimmermann 2009 <jacob@jzimm.net>
*
* mazen-ng is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* mazen-ng is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma implementation
#include "grid-maze.h"
#include "artwork.h"
#include "tree.h"
#include "normal-walker.h"
using namespace std;
ostream& operator << (ostream& stream, xy p) {
stream << '(' << p.x << ',' << p.y << ')';
return stream;
}
GridMaze::GridMaze (int width, int height): Glib::Object () {
this->_width = width;
this->_height = height;
this->grid = new unsigned[width * height];
for (int k = 0; k < width * height; k++)
grid[k] = 0;
exit_dir = NONE;
}
GridMaze::~GridMaze () {
delete grid;
}
void GridMaze::possible_directions (xy pos, vector<Direction>& dirs) const {
// test up
if (pos.y < height () - 1
&& !vert (pos)
&& !is_no_no (pos)
&& is_clear (pos.up ())
&& ((pos.x == 0) || !horiz (pos.x - 1, pos.y + 1)))
dirs.push_back (UP);
// test down
if (pos.y > 0
&& is_clear (pos.down())
&& (pos.y == 1 || !vert (pos.x, pos.y - 2))
&& (pos.x == 0 || !horiz (pos.x - 1, pos.y - 1)))
dirs.push_back (DOWN);
// test left
if (pos.x > 0
&& is_clear (pos.left ())
&& (pos.x == 1 || !horiz (pos.x - 2, pos.y))
&& (pos.y == 0 || !vert (pos.x - 1, pos.y - 1)))
dirs.push_back (LEFT);
// test right
if (pos.x < width () - 1
&& !horiz (pos)
&& !is_no_no (pos)
&& is_clear (pos.right ())
&& ((pos.y == 0) || !vert (pos.x + 1, pos.y - 1)))
dirs.push_back (RIGHT);
}
void GridMaze::move (xy& pos, Direction dir) {
switch(dir) {
case UP:
set_vert (pos.x, pos.y++);
break;
case DOWN:
set_vert (pos.x, --pos.y);
break;
case RIGHT:
set_horiz (pos.x++, pos.y);
break;
case LEFT:
set_horiz (--pos.x, pos.y);
break;
}
}
void GridMaze::generate () {
// init trees
list<Tree> trees;
list<xy>::const_iterator p;
for (p = tree_positions.begin (); p != tree_positions.end (); ++p) {
if (!is_no_no (*p))
trees.push_back (Tree (*this, *p));
}
// grow trees
bool still_moving;
list<Tree>::iterator tree, end = trees.end ();
do {
still_moving = false;
for (tree = trees.begin (); tree != end; ++tree)
still_moving |= tree->move ();
} while (still_moving);
// precalculate solution
solve ();
}
void GridMaze::run_walker (Walker& walker) {
walker.map (get_exit (), 0);
walker.gen_solution (solution);
}
void GridMaze::solve () {
NormalWalker walker (*this);
run_walker (walker);
}