[go: up one dir, main page]

Menu

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

Download this file

129 lines (102 with data), 3.8 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
/* -*- 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/>.
*/
#ifndef _GRID_MAZE_H_
#define _GRID_MAZE_H_
#pragma interface
#include <glibmm.h>
#include <list>
#include <vector>
#include <iostream>
class xy
{
public:
int x, y;
xy (int x, int y) {
this->x = x;
this->y = y;
}
xy (const xy& p) {
x = p.x;
y = p.y;
}
xy () { x = y = 0; }
void set (int x, int y) { this->x = x; this->y = y; }
xy left () const { return xy (x - 1, y); }
xy right () const { return xy (x + 1, y); }
xy up () const { return xy (x, y + 1); }
xy down () const { return xy (x, y - 1); }
bool operator == (const xy& p) const {
return x == p.x && y == p.y;
}
bool operator != (const xy& p) const {
return x != p.x || y != p.y;
}
};
std::ostream& operator << (std::ostream& stream, xy p);
class Walker;
class GridMaze: public Glib::Object
{
friend class Tree;
public:
virtual ~GridMaze ();
int width () const { return _width; }
int height () const { return _height; }
bool horiz (int x, int y) const { return grid[ix (x, y)] & 1; }
bool horiz (const xy& p) const { return horiz(p.x, p.y); }
bool vert (int x, int y) const { return grid[ix (x, y)] & 2; }
bool vert (const xy& p) const { return vert(p.x, p.y); }
virtual xy get_entrance() const = 0;
virtual xy get_exit() const = 0;;
enum Direction { NONE, LEFT, RIGHT, UP, DOWN };
Direction get_exit_dir () const { return exit_dir; }
typedef std::list<xy> Wall;
const std::list<Wall>& get_walls () const { return walls; }
const std::list<xy>& get_solution () const { return solution; }
private:
int _width, _height;
unsigned* grid;
inline int ix (int x, int y) const { return y * _width + x; }
protected:
Direction exit_dir;
virtual void possible_directions (xy pos, std::vector<Direction>& dirs) const;
virtual void move (xy& pos, Direction dir);
std::list<Wall> walls;
std::list<xy> tree_positions;
std::list<xy> solution;
void set_horiz (int x, int y) { grid[ix (x, y)] |= 1; }
void set_horiz (const xy& p) { set_horiz (p.x, p.y); }
void clear_horiz (int x, int y) { grid[ix (x, y)] &= 0xfe; }
void clear_horiz (const xy& p) { clear_horiz (p.x, p.y); }
void set_vert (int x, int y) { grid[ix (x, y)] |= 2; }
void set_vert (const xy& p) { set_vert (p.x, p.y); }
void clear_vert (int x, int y) { grid[ix (x, y)] &= 0xfd; }
void clear_vert (const xy& p) { clear_vert (p.x, p.y); }
void set_no_no (int x, int y) { grid[ix (x, y)] |= 4; }
void set_no_no (const xy& p) { set_no_no (p.x, p.y); }
bool is_clear (int x, int y) const { return grid[ix (x, y)] == 0; }
bool is_clear (const xy& p) const { return is_clear (p.x, p.y); }
bool is_no_no (int x, int y) const { return grid[ix (x, y)] & 4; }
bool is_no_no (const xy& p) const { return is_no_no (p.x, p.y); }
GridMaze (int width, int height);
void generate ();
void run_walker (Walker& walker);
virtual void solve ();
};
typedef Glib::RefPtr<GridMaze> MazePtr;
#endif // _GRID_MAZE_H_