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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
/*
* ksokoban - a Sokoban game for KDE
* Copyright (C) 1998 Anders Widell <d95-awi@nada.kth.se>
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include "Map.h"
Map::Map() : xpos_(-1), ypos_(-1), width_(0), height_(0), objectsLeft_(-1) {
}
void
Map::map (int x, int y, int val) {
assert (x>=0 && x<=MAX_X && y>=0 && y<=MAX_Y);
if ((map (x, y) & (OBJECT | GOAL)) == OBJECT) objectsLeft_--;
if ((val & (OBJECT | GOAL)) == OBJECT) objectsLeft_++;
currentMap_[y+1][x+1] = val;
if (val != 0) {
if (width_ <= x) width_ = x+1;
if (height_ <= y) height_ = y+1;
}
}
void
Map::setMap (int x, int y, int bits) {
assert ((map (x, y) & bits) == 0);
if (goal (x, y) && ((bits & OBJECT) == OBJECT)) objectsLeft_--;
assert (objectsLeft_ >= 0);
currentMap_[y+1][x+1] |= bits;
if (bits != 0) {
if (width_ <= x) width_ = x+1;
if (height_ <= y) height_ = y+1;
}
}
void
Map::clearMap (int x, int y, int bits) {
assert ((map (x, y) & bits) == bits);
if (goal (x, y) && ((bits & OBJECT) == OBJECT)) objectsLeft_++;
currentMap_[y+1][x+1] &= ~bits;
}
void
Map::clearMap () {
memset (currentMap_, 0, (MAX_Y+3)*(MAX_X+3)*sizeof (char));
objectsLeft_ = 0;
width_ = height_ = 0;
}
bool
Map::fillFloor (int x, int y) {
if (badCoords (x, y)) return false;
if ((currentMap_[y+1][x+1] & (WALL|FLOOR)) != 0) return true;
currentMap_[y+1][x+1] |= FLOOR;
bool a = fillFloor (x, y-1);
bool b = fillFloor (x, y+1);
bool c = fillFloor (x-1, y);
bool d = fillFloor (x+1, y);
return a && b && c && d;
}
bool
Map::step (int _x, int _y) {
assert (!badCoords (xpos_, ypos_));
assert (empty (xpos_, ypos_));
int xd=0, yd=0;
if (_x < xpos_) xd = -1;
if (_x > xpos_) xd = 1;
if (_y < ypos_) yd = -1;
if (_y > ypos_) yd = 1;
if (badDelta (xd, yd) || badCoords (_x, _y)) return false;
int x=xpos_, y=ypos_;
do {
x += xd;
y += yd;
if (!empty (x, y)) return false;
} while (!(x==_x && y==_y));
xpos_ = _x;
ypos_ = _y;
return true;
}
bool
Map::push (int _x, int _y) {
assert (!badCoords (xpos_, ypos_));
assert (empty (xpos_, ypos_));
int xd=0, yd=0;
if (_x < xpos_) xd = -1;
if (_x > xpos_) xd = 1;
if (_y < ypos_) yd = -1;
if (_y > ypos_) yd = 1;
if (badDelta (xd, yd) || badCoords (_x+xd, _y+yd)) return false;
int x=xpos_+xd, y=ypos_+yd;
if (!object (x, y)) return false;
if (!empty (_x+xd, _y+yd)) return false;
while (!(x==_x && y==_y)) {
x += xd;
y += yd;
if (!empty (x, y)) return false;
}
clearMap (xpos_+xd, ypos_+yd, OBJECT);
setMap (_x+xd, _y+yd, OBJECT);
xpos_ = _x;
ypos_ = _y;
return true;
}
bool
Map::unstep (int _x, int _y) {
return Map::step (_x, _y);
}
bool
Map::unpush (int _x, int _y) {
assert (!badCoords (xpos_, ypos_));
assert (empty (xpos_, ypos_));
int xd=0, yd=0;
if (_x < xpos_) xd = -1;
if (_x > xpos_) xd = 1;
if (_y < ypos_) yd = -1;
if (_y > ypos_) yd = 1;
if (badDelta (xd, yd) || badCoords (_x+xd, _y+yd)) return false;
int x=xpos_, y=ypos_;
if (!object (x-xd, y-yd)) return false;
do {
x += xd;
y += yd;
if (!empty (x, y)) return false;
} while (!(x==_x && y==_y));
clearMap (xpos_-xd, ypos_-yd, OBJECT);
setMap (_x-xd, _y-yd, OBJECT);
xpos_ = _x;
ypos_ = _y;
return true;
}
void
Map::printMap(void) {
for (int y=0; y<height_; y++) {
for (int x=0; x<width_; x++) {
switch (map (x, y) & ~FLOOR) {
case WALL:
printf("#");
break;
case GOAL:
printf("%c", x==xpos_ && y==ypos_ ? '+' : '.');
break;
case OBJECT:
printf("$");
break;
case OBJECT|GOAL:
printf("*");
break;
case 0:
printf("%c", x==xpos_ && y==ypos_ ? '@' : ' ');
break;
default:
printf("<%X>", map(x,y)&FLOOR);
break;
}
}
printf ("\n");
}
}
|