This repository was archived by the owner on Apr 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGrid.java
More file actions
446 lines (390 loc) · 11.6 KB
/
Grid.java
File metadata and controls
446 lines (390 loc) · 11.6 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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Model;
import Strategy.MazeGenerationStrategy.MazeGenerationStrategy;
import Strategy.PathfindingStrategy.PathfindingStrategy;
import Util.Painter;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.Random;
/**
*
* @author frank
*/
public class Grid extends Observable implements Observer
{
// Dimensions
private int x_size;
private int y_size;
// Grid
private Tile[][] grid;
Tile root, target;
// What change will happen to the tile on click
private Tile.Type clickType;
// Attributes
private final Painter painter;
public Grid()
{
this.root = null;
this.target = null;
this.clickType = Tile.Type.ROOT;
painter = Painter.getInstance();
}
/**
* Runs an algorithm specified as 'pathfindingStrategy' to pathfind from root to target
* @param pathfindingStrategy
* @return
* @throws java.lang.InterruptedException
*/
public boolean executePathfinding(PathfindingStrategy pathfindingStrategy) throws InterruptedException
{
if(root == null || target == null) return false;
this.painter.clearPath(this);
List<Tile> path = new ArrayList<>();
pathfindingStrategy.algorithm(this, path);
return true;
}
/**
* Initializes the grid with all it's tiles in an empty state.
* This method is only meant to be called once before runtime.
* @param x_tiles
* @param y_tiles
* @param tile_size
*/
public void gridInit(int x_tiles, int y_tiles, int tile_size)
{
this.x_size = x_tiles;
this.y_size = y_tiles;
this.grid = new Tile[x_tiles][y_tiles];
// Initializes all tiles
for(int y = 0; y < y_tiles; y++)
{
for(int x = 0; x < x_tiles; x++)
{
Tile tile = new Tile(x, y, tile_size);
tile.addObserver(this);
grid[x][y] = tile;
}
}
}
/**
* Toggles all tiles to show respective coordinates
* @param toAdd toAdd true if it's to add coords, false if it's to remove
*/
public void toggleCoords(boolean toAdd)
{
for(int y = 0; y < this.y_size; y++)
{
for(int x = 0; x < this.x_size; x++)
{
grid[x][y].toggleCoords(toAdd);
}
}
}
/**
* Returns the Tile 2Dimensional vector, representing the grid
* @return Tile[][] grid
*/
public Tile[][] getGrid()
{
return this.grid;
}
/**
* Checks if the grid is ready to run. (E.g. If a root and target node were selected)
* @return true if grid is ready to run an algorithm, false if root and target are not set
*/
public boolean isReady()
{
return !(root == null || target == null);
}
/**
* Returns a list with all the tiles in this grid
* @return all tiles
*/
public List<Tile> getTiles()
{
List<Tile> tiles = new ArrayList<>();
for(int y = 0; y < this.y_size; y++)
{
for(int x = 0; x < this.x_size; x++)
{
tiles.add(grid[x][y]);
}
}
return tiles;
}
/**
* Sets all Tiles in the grid to default (Empty, defaultWeight)!
*/
public void clearGrid()
{
for(int y = 0; y < this.y_size; y++)
{
for(int x = 0; x < this.x_size; x++)
{
grid[x][y].clearTile();
}
}
}
/**
* Sets the border for all tiles
* @param setBorder true to set border, false to remove border
*/
public void addTileBorders(boolean setBorder)
{
for(int y = 0; y < this.y_size; y++)
{
for(int x = 0; x < this.x_size; x++)
{
grid[x][y].setTileStroke(setBorder);
}
}
}
/**
* Returns total amount of walls in the grid
* @return
*/
public int getWallsAmount()
{
int totalWalls = 0;
for(int y = 0; y < this.y_size; y++)
{
for(int x = 0; x < this.x_size; x++)
{
if(grid[x][y].isWall()) totalWalls++;
}
}
return totalWalls;
}
/**
* Return neighbors of 'tile'
* @param tile
* @return
*/
public List<Tile> getTileNeighbors(Tile tile)
{
List<Tile> neighbors = new ArrayList<>();
neighbors.add(this.getNorthTile(tile));
neighbors.add(this.getSouthTile(tile));
neighbors.add(this.getEastTile(tile));
neighbors.add(this.getWestTile(tile));
return neighbors;
}
/**
* Returns tile to the north of 'tile'
* @param tile north tile
* @return Tile
*/
public Tile getNorthTile(Tile tile)
{
return (tile.getY() - 1 >= 0) ? grid[tile.getX()][tile.getY() - 1] : null;
}
/**
* Returns tile to the south of 'tile'
* @param tile south tile
* @return Tile
*/
public Tile getSouthTile(Tile tile)
{
return (tile.getY() + 1 <= y_size - 1) ? grid[tile.getX()][tile.getY() + 1] : null;
}
/**
* Returns tile to the west (left) of 'tile'
* @param tile west tile
* @return Tile
*/
public Tile getWestTile(Tile tile)
{
return (tile.getX() - 1 >= 0) ? grid[tile.getX() - 1][tile.getY()] : null;
}
/**
* Returns tile to the East (right) of 'tile'
* @param tile east tile
* @return Tile
*/
public Tile getEastTile(Tile tile)
{
return (tile.getX() + 1 <= x_size - 1) ? grid[tile.getX() + 1][tile.getY()] : null;
}
/**
* Returns true jf 'tile' is directly north to 'compare' (same x coord)
* @param tile
* @param compare
* @return
*/
public boolean isOnNorth(Tile tile, Tile compare)
{
if(tile.getX() != compare.getX())
return false;
return tile.getY() < compare.getY();
}
/**
* Returns true jf 'tile' is directly south to 'compare' (same x coord)
* @param tile
* @param compare
* @return
*/
public boolean isOnSouth(Tile tile, Tile compare)
{
if(tile.getX() != compare.getX())
return false;
return tile.getY() > compare.getY();
}
/**
* Returns true jf 'tile' is directly west to 'compare' (same y coord)
* @param tile
* @param compare
* @return
*/
public boolean isOnWest(Tile tile, Tile compare)
{
if(tile.getY() != compare.getY())
return false;
return tile.getX() < compare.getX();
}
/**
* Returns true jf 'tile' is directly north to 'compare' (same y coord)
* @param tile
* @param compare
* @return
*/
public boolean isOnEast(Tile tile, Tile compare)
{
if(tile.getY() != compare.getY())
return false;
return tile.getX() > compare.getX();
}
/**
* Returns height of maze
* @return int
*/
public int getYSize()
{
return this.y_size;
}
/**
* Returns width of maze
* @return int
*/
public int getXSize()
{
return this.x_size;
}
/**
* Changes the current click type
* @param type
*/
public void changeClickType(Tile.Type type)
{
this.clickType = type;
}
/**
* Returns start Tile
* @return Tile
*/
public Tile getRoot()
{
return this.root;
}
/**
* Returns target tile
* @return Tile
*/
public Tile getTarget()
{
return this.target;
}
/**
* Adds random weights to all tiles in the grid
* This makes the pathfinding less linear than if all were weighted the same
*/
public void addRandomWeights()
{
this.clearGrid();
for(int y = 0; y < this.y_size; y++)
{
for(int x = 0; x < this.x_size; x++)
{
grid[x][y].randomizeWeight();
}
}
}
/**
* resets previous walls and adds new random walls to some tiles in the grid
*/
public void addRandomWalls()
{
Random random = new Random();
Tile tile;
for(int y = 0; y < this.y_size; y++)
{
for(int x = 0; x < this.x_size; x++)
{
tile = grid[x][y];
if(tile.getType() == Tile.Type.WALL)
tile.setAttributes(Tile.Type.EMPTY, tile.getWeight());
if((random.nextInt(2 - 0 + 1) + 0) == 1)
tile.setAttributes(Tile.Type.WALL, tile.getWeight());
}
}
}
/**
* Generates a random maze using a recursive backtracker algorithm
* @param mazeGenerationStrategy maze generation algorithm
*/
public void generateRandomMaze(MazeGenerationStrategy mazeGenerationStrategy)
{
mazeGenerationStrategy.generate(this);
}
@Override
public void update(Observable o, Object arg)
{
// If the update came from a Tile Object
if(o instanceof Tile)
{
Tile tile = (Tile)o;
// User can't override a wall, unless he's setting it as empty first.
if(tile.isWall())
{
if(this.clickType == Tile.Type.EMPTY)
tile.setAttributes(clickType, tile.getDefaultWeight());
return;
}
// What happens when you click a tile with a specific clickType (I.e. root, target, wall...)
switch(this.clickType)
{
// Tiles that can only have one ocurrence throughout the grid
case ROOT: case TARGET:
// Clear old tiles
if(clickType == Tile.Type.ROOT)
{
if(this.root != null) root.clearTile();
this.root = tile;
}
else if(clickType == Tile.Type.TARGET)
{
if(this.target != null) target.clearTile();
this.target = tile;
}
tile.setAttributes(clickType, tile.getDefaultWeight());
break;
// Tiles that allow multiple ocurrences of themselfs
default:
tile.setAttributes(clickType, tile.getWeight());
break;
}
}
// If the update came from statistics
if(o instanceof PathfindingStatistics)
{
PathfindingStatistics stats = (PathfindingStatistics)o;
setChanged();
notifyObservers(stats);
}
}
}