-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMazeWorld.java
More file actions
151 lines (122 loc) · 4.01 KB
/
MazeWorld.java
File metadata and controls
151 lines (122 loc) · 4.01 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
import javalib.colors.Black;
import javalib.colors.Green;
import javalib.colors.IColor;
import javalib.colors.Red;
import javalib.colors.White;
import javalib.funworld.World;
import javalib.worldimages.LineImage;
import javalib.worldimages.OverlayImages;
import javalib.worldimages.OverlayImagesXY;
import javalib.worldimages.Posn;
import javalib.worldimages.RectangleImage;
import javalib.worldimages.WorldImage;
/**
* Represents the current state of the world
*
* @author Greg & Isaac
*
*/
public class MazeWorld extends World{
Maze maze;
Solver solver;
Graph graph;
int height;
int width;
int cellSize = 10;
MazeWorld(int width, int height) {
if (width <= 1 || height <= 1) {
throw new RuntimeException("Invalid Dimensions");
}
this.width = width;
this.height = height;
this.graph = new Graph(this.width, this.height);
this.maze = this.graph.toMaze();
this.solver = new Solver(this.maze.map);
}
public MazeWorld onTick(){
return this;
}
WorldImage drawCell(Cell cell) {
IColor clr = new White();
if (cell.visited) {
clr = new Green();
}
// WorldImage base = new RectangleImage(new Point(0,0), this.cellSize, this.cellSize, clr);
WorldImage base = new RectangleImage(new Posn(cell.p.x*this.cellSize + this.cellSize/2, cell.p.y*this.cellSize+ this.cellSize/2),
this.cellSize, this.cellSize, clr);
int s = this.cellSize;
WorldImage leftWall =
new LineImage(new Posn(s*cell.p.x,s*cell.p.y), new Posn(s*cell.p.x,s*cell.p.y+s), new Black());
WorldImage upWall =
new LineImage(new Posn(s*cell.p.x,s*cell.p.y), new Posn(s*cell.p.x+s,s*cell.p.y), new Black());
WorldImage rightWall =
new LineImage(new Posn(s*cell.p.x+s,s*cell.p.y), new Posn(s*cell.p.x+s,s*cell.p.y+s), new Black());
WorldImage downWall =
new LineImage(new Posn(s*cell.p.x,s*cell.p.y+s), new Posn(s*cell.p.x+s,s*cell.p.y+s), new Black());
if (!cell.left) {
base = new OverlayImages(base, leftWall);
}
if (!cell.up) {
base = new OverlayImages(base, upWall);
}
if (!cell.right) {
base = new OverlayImages(base, rightWall);
}
if (!cell.down) {
base = new OverlayImages(base, downWall);
}
return base;
}
WorldImage drawMaze() {
WorldImage base = new RectangleImage(new Posn((this.width)*this.cellSize/2,(this.height)*this.cellSize/2), (this.width)*this.cellSize, (this.height)*this.cellSize, new Red());
for (int x = 0; x < this.width; x += 1) {
for(int y = 0; y < this.height; y += 1) {
base = new OverlayImages(base, this.drawCell(maze.map.get(new Point(x,y))));
}
}
return base;
}
@Override
public WorldImage makeImage() {
return this.drawMaze();
// WorldImage base = new RectangleImage(new Posn(175,150), 300 , 300, new Red());
// base = new OverlayImages(base,
// new RectangleImage(new Posn(100,50), 50 ,50, new Black())
// );
// return base;
}
}
/**
* Represents a position
*
* @author Greg & Isaac
*
*/
class Point extends Posn {
Point(int x, int y) {
super(x, y);
}
public boolean equals(Object obj) {
if (!(obj instanceof Point)) {
return false;
}
else {
Point nPoint = (Point) obj;
return nPoint.x == this.x && nPoint.y == this.y;
}
}
public int hashCode() {
return this.x;
}
}
class NewWorld extends World{
Posn p = new Posn(10,10);
@Override
public WorldImage makeImage() {
WorldImage base = new RectangleImage(new Posn(175,150), 300 , 300, new Red());
base = new OverlayImages(base,
new RectangleImage(new Posn(100,50), 50 ,50, new Black())
);
return base;
}
}