-
Notifications
You must be signed in to change notification settings - Fork 1
/
BoardSquare2D.m
298 lines (252 loc) · 10.8 KB
/
BoardSquare2D.m
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//
// BoardSquare2D.m
//
//Copyright 2016 Toby Jennings
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
//
#import "BoardSquare2D.h"
@implementation BoardCell
@synthesize xReference;
@synthesize yReference;
@synthesize xPosition;
@synthesize yPosition;
@synthesize isObstacle;
@synthesize isInPath;
- (NSString *) description {
return [NSString stringWithFormat:@"Board cell at x=%i, y=%i", xReference,yReference];
}
// Makes a BoardCell object with the specified values. It defaults to isObstacle.
- (id) initWithXReference:(int)xR WithYReference:(int)yR WithXPosition:(int)xP WithYPosition:(int)yP {
self = [super init];
if (self) {
xReference = xR;
yReference = yR;
xPosition = xP;
yPosition = yP;
isObstacle = YES;
isInPath = NO;
}
return self;
}
// makePassageForMaze just flips the given BoardCell object from isObstacle to isNotObstacle.
// Feeding in a pointer to the board object exposes the maximum row/column dimensions but
// I could probably do better checking beforehand and make it unnecessary.
- (void) makePassageForMaze:(Board2D *)board {
if (self.xReference != 0 &&
self.xReference != board.boardSizeInX &&
self.yReference != 0 &&
self.yReference != board.boardSizeInY) {
isObstacle = NO;
}
}
- (void) makePassage {
isObstacle = NO;
}
@end
////////////////////////////////////////////////////////////////////////////////
@implementation Board2D
@synthesize boardSizeInX;
@synthesize boardSizeInY;
// getter and setter for boardSizeInX:
- (void) setBoardSizeInX:(int)columns {
boardSizeInX = columns;
}
- (int) boardSizeInX {
return boardSizeInX - 1; //0-indexed array
}
// getter and setter for boardSizeInY:
- (void) setBoardSizeInY:(int)rows {
boardSizeInY = rows;
}
- (int) boardSizeInY {
return boardSizeInY - 1;
}
//setupBoardWithRows sets up a 2-dimensional array with a BoardCell object
// at each intersection. The board starts out with every square an obstacle.
- (void) setupBoardWithRows:(int)rows WithColumns:(int)columns {
contentWidth = 640 / columns;
contentHeight = 960 / rows;
boardSizeInX = columns;
boardSizeInY = rows;
theRows = [NSMutableArray arrayWithCapacity:columns]; //autorelease
for ( int i=0; i < columns; i++ )
{
NSMutableArray * eachColumn = [NSMutableArray arrayWithCapacity:rows]; //autoreleased
for ( int j=0; j < rows; j++ )
{
BoardCell * thisCell = [[BoardCell alloc] initWithXReference:i
WithYReference:j
WithXPosition:i * contentWidth
WithYPosition:j * contentHeight];
[eachColumn addObject:thisCell];
//[BoardCell release]; //Do not need explicit release in ARC
}
[theRows addObject:eachColumn];
}
}
// addToWallList:NeighborsOfCellAtRow:Column does exactly what it says.
// This method is called from createMazeFromBoard to populate its wallList
// as it carves out the maze.
- (void) addToWallList:(NSMutableArray *)wallList NeighborsOfCellAtRow:(int)row Column:(int)column{
BoardCell *up = nil;
BoardCell *down = nil;
BoardCell *left = nil;
BoardCell *right = nil;
if ( row-1 >= 0 ) {
up = [self getCellAtRow:row-1 Column:column];
if ( up.isObstacle ) {
[wallList addObject:[NSArray arrayWithObjects:up, @"up", nil]];
}
}
if ( row+1 < boardSizeInY ) {
down = [self getCellAtRow:row+1 Column:column];
if ( down.isObstacle ) {
[wallList addObject:[NSArray arrayWithObjects:down, @"down", nil]];
}
}
if ( column-1 >= 0 ) {
left=[self getCellAtRow:row Column:column-1];
if ( left.isObstacle ) {
[wallList addObject:[NSArray arrayWithObjects:left, @"left", nil]];
}
}
if ( column+1 < boardSizeInX ) {
right=[self getCellAtRow:row Column:column+1];
if ( right.isObstacle ) {
[wallList addObject:[NSArray arrayWithObjects:right, @"right", nil]];
}
}
}
// createMazeFromBoard uses a modified Prim's algorithm to carve a "maze" out of an
// initialized board. Don't call it before initBoard
- (void) createMazeFromBoard {
//Pick a random initial node
int frx = arc4random_uniform(boardSizeInX-2)+1; //1,max-1
int fry = arc4random_uniform(boardSizeInY-2)+1; //1,max-1
//Initialize a mazeList and a wallList
//make mazeList NSMutableSet, unordered but faster membership testing.
NSMutableSet *mazeList = [NSMutableSet setWithCapacity:boardSizeInX*boardSizeInY];
NSMutableArray *wallList = [NSMutableArray arrayWithCapacity:boardSizeInX*boardSizeInY];
//Make the initial node a passage
[[self getCellAtRow:fry Column:frx] makePassage];
//Put the initial node on the mazeList
[mazeList addObject:[self getCellAtRow:fry Column:frx]];
//Put the neighbors on the wallList
[self addToWallList:wallList NeighborsOfCellAtRow:fry Column:frx];
while ( [wallList count] > 0 ) {
int wallCount = (int)[wallList count]; //Explicit cast to int
int randomWallFromList = arc4random_uniform(wallCount);
NSArray *aRandomWall = [NSArray arrayWithArray:[wallList objectAtIndex:randomWallFromList]];
BoardCell *aRandomCell = [aRandomWall objectAtIndex:0];
NSString *aRandomDirection = [aRandomWall objectAtIndex:1];
BoardCell *nextSquare;
if ( [aRandomDirection isEqual: @"up"] && aRandomCell.yReference-1 >= 0 ) {
//step into this square from the south
nextSquare = [self getCellAtRow:aRandomCell.yReference-1 Column:aRandomCell.xReference];
//make sure nextSquare isn't already on the mazeList
if ( ! [mazeList containsObject:nextSquare] ) {
//Make cell a passage and add to mazeList
[aRandomCell makePassage];
[mazeList addObject:aRandomCell];
[mazeList addObject:nextSquare];
[self addToWallList:wallList NeighborsOfCellAtRow:aRandomCell.yReference Column:aRandomCell.xReference];
}
} else if ( [aRandomDirection isEqual: @"down"] && aRandomCell.yReference+1 < boardSizeInY ) {
//step into this square from the north
nextSquare = [self getCellAtRow:aRandomCell.yReference+1 Column:aRandomCell.xReference];
if ( ! [mazeList containsObject:nextSquare] ) {
[aRandomCell makePassage];
[mazeList addObject:aRandomCell];
[mazeList addObject:nextSquare];
[self addToWallList:wallList NeighborsOfCellAtRow:aRandomCell.yReference Column:aRandomCell.xReference];
}
} else if ( [aRandomDirection isEqual: @"left"] && aRandomCell.xReference -1 >= 0 ) {
//step into this square from the east
nextSquare = [self getCellAtRow:aRandomCell.yReference Column:aRandomCell.xReference-1];
if ( ! [mazeList containsObject:nextSquare] ) {
[aRandomCell makePassage];
[mazeList addObject:aRandomCell];
[mazeList addObject:nextSquare];
[self addToWallList:wallList NeighborsOfCellAtRow:aRandomCell.yReference Column:aRandomCell.xReference];
}
} else if ( [aRandomDirection isEqual: @"right"] && aRandomCell.xReference +1 < boardSizeInX) {
//step into this square from the west
nextSquare = [self getCellAtRow:aRandomCell.yReference Column:aRandomCell.xReference+1];
if ( ! [mazeList containsObject:nextSquare] ) {
[aRandomCell makePassageForMaze:self];
[mazeList addObject:aRandomCell];
[mazeList addObject:nextSquare];
[self addToWallList:wallList NeighborsOfCellAtRow:aRandomCell.yReference Column:aRandomCell.xReference];
}
} //end if
//Remove all instances of aRandomCell from wallList
//This might be a little faster if wallList was NSMutableSet
//and could use [NSMutableSet minusSet:toDelete].
NSMutableArray *toDelete = [NSMutableArray array];
for ( NSArray *a in wallList) {
if ( [a objectAtIndex:0] == aRandomCell ) {
[toDelete addObject:a];
}
}
[wallList removeObjectsInArray:toDelete];
toDelete = nil;
nextSquare = nil;
} // while
}
// getRows returns the main board array
- (NSMutableArray *) getRows {
return theRows;
}
// getCellAtRow:Column returns the object at that intersection
- (BoardCell *) getCellAtRow:(int)row Column:(int)column {
//TODO try/catch this
if ( row > boardSizeInY || column > boardSizeInX ) {
NSLog(@"Coordinate %i, %i is out of bounds", column,row);
}
return [[theRows objectAtIndex:column] objectAtIndex:row];
}
// drawMaze generates an ASCII representation of the maze with NSLog
- (NSString *) drawMaze {
NSMutableString *aString = [NSMutableString stringWithString:@"\n"];
for (int i=0; i < boardSizeInY; i++) {
for (int j=0; j < boardSizeInX; j++) {
NSString *toAppend = ( [self getCellAtRow:i Column:j].isObstacle ) ? @"X" : @".";
[aString appendString:toAppend];
}
[aString appendString:@"\n"];
}
return aString;
}
- (NSString *) drawPathThroughMaze:(NSArray *)path {
NSMutableString *aString = [NSMutableString stringWithString:@"\n"];
for (NSDictionary *pathNode in path){
int theX = [[pathNode valueForKey:@"x"] intValue];
int theY = [[pathNode valueForKey:@"y"] intValue];
[self getCellAtRow:theY Column:theX].isInPath = YES;
}
for (int i=0; i < boardSizeInY; i++) {
for (int j=0; j < boardSizeInX; j++) {
NSString *toAppend = @".";
if ( [self getCellAtRow:i Column:j].isObstacle ) {
toAppend = @"X";
} else if ([self getCellAtRow:i Column:j].isInPath ) {
toAppend = @"!";
}
[aString appendString:toAppend];
}
[aString appendString:@"\n"];
}
return aString;
}
@end