-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMazeApp.cs
More file actions
272 lines (228 loc) · 8.73 KB
/
MazeApp.cs
File metadata and controls
272 lines (228 loc) · 8.73 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
using SFML.Graphics;
using SFML.System;
using SFML.Window;
using MazeVisualiser.Generators;
using MazeVisualiser.Solvers;
using MazeVisualiser.State;
namespace MazeVisualiser
{
// Handles maze generation, solving, rendering and input events
internal class MazeApp
{
private readonly RenderWindow window;
private readonly MazeConfig config;
private MazeGenerationState generationState;
private MazeSolvingState? solvingState = null;
private AppState state;
private (ushort X, ushort Y)? startPoint = null;
private (ushort X, ushort Y)? endPoint = null;
private bool paused = false;
private float stepInterval = 0.05f;
private float elapsedTime = 0f;
public MazeApp(MazeConfig config)
{
this.config = config;
window = new RenderWindow(
new VideoMode((uint)(config.Width * config.CellSize), (uint)(config.Height * config.CellSize)),
"MazeVisualiser v1.0.0",
Styles.Titlebar | Styles.Close // Prevent window resizing
);
// Set window icon
var iconImage = new Image("favicon-32x32.png");
window.SetIcon(iconImage.Size.X, iconImage.Size.Y, iconImage.Pixels);
// Set window event handlers
window.Closed += (_, __) => window.Close();
window.KeyPressed += OnKeyPressed;
window.MouseButtonPressed += OnMouseButtonPressed;
generationState = new MazeGenerationState(config.Width, config.Height, new BacktrackingGenerator());
state = AppState.Generating;
}
// Main loop
public void Run()
{
var clock = new Clock();
window.SetFramerateLimit(60);
window.SetActive(true);
while (window.IsOpen)
{
window.DispatchEvents();
window.Clear(Color.Black);
var deltaTime = clock.Restart().AsSeconds();
if (!paused)
Update(deltaTime);
Draw();
window.Display();
}
}
// Initialise maze solving state
private void StartSolving()
{
if (!startPoint.HasValue || !endPoint.HasValue)
return;
solvingState = new MazeSolvingState(generationState.Maze, new BFSSolver(), startPoint.Value, endPoint.Value);
state = AppState.Solving;
paused = false;
elapsedTime = 0f;
}
// Update application logic based on current state
private void Update(float deltaTime)
{
elapsedTime += deltaTime;
switch (state)
{
case AppState.Generating:
StepGeneration();
break;
case AppState.Solving:
StepSolving();
break;
default: break;
}
}
// Advance maze generation by one or more steps, controlled by stepInterval
private void StepGeneration()
{
if (stepInterval <= 0f)
{
while (generationState.Step()) { }
state = AppState.Generated;
elapsedTime = 0f;
paused = false;
}
else
while (elapsedTime >= stepInterval)
{
if (!generationState.Step())
{
state = AppState.Generated;
elapsedTime = 0f;
paused = false;
break;
}
elapsedTime -= stepInterval;
}
}
// Advance maze solving by one or more steps, controlled by stepInterval
private void StepSolving()
{
if (solvingState == null)
return;
if (stepInterval <= 0f)
while (solvingState.Step()) { }
else
while (elapsedTime >= stepInterval)
{
if (!solvingState.Step())
{
state = AppState.Solved;
elapsedTime = 0f;
paused = true;
break;
}
elapsedTime -= stepInterval;
}
}
// Draws maze cells with colours coding for generation and solving states
private void Draw()
{
var rect = new RectangleShape(new Vector2f(config.CellSize, config.CellSize))
{
FillColor = Color.White
};
for (int y = 0; y < generationState.Height; y++)
for (int x = 0; x < generationState.Width; x++)
if (generationState.Maze[y, x])
{
rect.FillColor = Color.White;
if (state == AppState.Generating)
{
if (generationState.CellStates[y, x] == GeneratorStepType.Stack)
rect.FillColor = Color.Blue;
}
else if ((state == AppState.Solving || state == AppState.Solved) && solvingState != null)
switch (solvingState.CellStates[y, x])
{
case SolverStepType.Visited:
rect.FillColor = Color.Blue;
break;
case SolverStepType.Frontier:
rect.FillColor = Color.Cyan;
break;
case SolverStepType.Path:
rect.FillColor = Color.Yellow;
break;
}
rect.Position = new Vector2f(x * config.CellSize, y * config.CellSize);
window.Draw(rect);
}
if (startPoint.HasValue)
{
rect.FillColor = Color.Green;
rect.Position = new Vector2f(startPoint.Value.X * config.CellSize, startPoint.Value.Y * config.CellSize);
window.Draw(rect);
}
if (endPoint.HasValue)
{
rect.FillColor = Color.Red;
rect.Position = new Vector2f(endPoint.Value.X * config.CellSize, endPoint.Value.Y * config.CellSize);
window.Draw(rect);
}
}
// Handles keyboard inputs
private void OnKeyPressed(object? sender, KeyEventArgs e)
{
switch (e.Code)
{
// Pause visualisation
case Keyboard.Key.Space:
paused = !paused;
break;
// Restart generation
case Keyboard.Key.R:
state = AppState.Generating;
generationState.Reset(new BacktrackingGenerator());
solvingState = null;
startPoint = null;
endPoint = null;
paused = false;
elapsedTime = 0f;
break;
// Increase visualisation speed
case Keyboard.Key.Up:
stepInterval = MathF.Max(0f, stepInterval - 0.005f);
break;
// Decrease visualisation speed
case Keyboard.Key.Down:
stepInterval += 0.005f;
break;
}
}
// Handles mouse inputs
private void OnMouseButtonPressed(object? sender, MouseButtonEventArgs e)
{
if (state != AppState.Generated)
return;
var x = (ushort)(e.X / config.CellSize);
var y = (ushort)(e.Y / config.CellSize);
if (x >= config.Width || y >= config.Height || !generationState.Maze[y, x])
return;
// Set solver start point
if (e.Button == Mouse.Button.Left)
{
startPoint = (x, y);
if (startPoint == endPoint)
endPoint = null;
}
// Set solver end point
else if (e.Button == Mouse.Button.Right)
{
endPoint = (x, y);
if (endPoint == startPoint)
startPoint = null;
}
// Start solving when start and end points are defined
if (startPoint.HasValue && endPoint.HasValue)
StartSolving();
}
}
}