-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonsterMazeGame.cs
321 lines (266 loc) · 12.1 KB
/
MonsterMazeGame.cs
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
public class MonsterMazeGame
{
public const int MaxLevel = 3;
// Found by looking at the available options in the "Character Map" windows system app
// viewing the Lucida Console font.
const char WallCharacter = '\u2588';
// Windows 11 Cascadia Code font doesn't have smiley face characters. Sigh.
// So reverting back to using standard text for the player and monsters, rather
// than using smiley faces, etc.
const char PlayerCharacterA = 'O';
const char PlayerCharacterB = 'o';
const char MonsterCharacterA = 'M';
const char MonsterCharacterB = 'm';
const char CaughtCharacter = 'X';
// Game state.
private MazePoint playerPos;
private int numMonsters; // also the level number
private MazePoint?[] monsterPos = new MazePoint?[MaxLevel]; // a point per monster (depending on the level)
private List<MazeStep>[] monsterPath = new List<MazeStep>[MaxLevel]; // a list of steps per monster
private CancellationTokenSource[] monsterPathCalcCancelSources = new CancellationTokenSource[MaxLevel];
private char[,] theMaze = new char[1,1];
private readonly int MaxWidth;
private readonly int MaxHeight;
public MonsterMazeGame(int maxWidth, int maxHeight)
{
MaxWidth = maxWidth;
MaxHeight = maxHeight;
}
public bool PlayLevel(int levelNumber)
{
MakeMaze(MaxWidth, MaxHeight);
// Initial positions
numMonsters = levelNumber;
playerPos = new MazePoint(0, 1);
monsterPos[0] = new MazePoint(theMaze.GetLength(0)-1, theMaze.GetLength(1)-2);
monsterPos[1] = levelNumber > 1 ? new MazePoint(1, theMaze.GetLength(1)-2) : null;
monsterPos[2] = levelNumber > 2 ? new MazePoint(theMaze.GetLength(0)-2, 1) : null;
for(int i = 0; i < levelNumber; i++)
{
StartMonsterPathCalculation(playerPos, i);
}
DisplayMaze(levelNumber: numMonsters);
// returns true if the game is over, or the user wants to quit.
return RunGameLoop();
}
protected bool RunGameLoop()
{
int loopCount = 0;
while(true)
{
// Show the player and the monsters. Using the loopCount as the basis for animation.
ShowEntity(playerPos, loopCount % 20 < 10 ? PlayerCharacterA : PlayerCharacterB, ConsoleColor.Green);
for(int i = 0; i < numMonsters; i++)
{
ShowEntity(monsterPos[i]!.Value, loopCount % 50 < 25 ? MonsterCharacterA : MonsterCharacterB, ConsoleColor.Red);
}
// Check to see if any of the monsters have reached the player.
for(int i = 0; i < numMonsters; i++)
{
if(playerPos.X == monsterPos[i]?.X && playerPos.Y == monsterPos[i]?.Y)
{
return DisplayCaught();
}
}
if(Console.KeyAvailable)
{
var userAction = EntityActionExtensions.FromConsoleKey(Console.ReadKey(true));
if(userAction == EntityAction.Quit)
{
return true;
}
// Soak up any other keypresses (avoid key buffering)
while(Console.KeyAvailable)
{
Console.ReadKey(true);
}
// Try to move the player, and start recalculating monster paths if the player does move
MazePoint playerOldPos = playerPos;
(playerPos, var validPlayerMove) = MoveInDirection(userAction, playerPos);
if(validPlayerMove)
{
Console.SetCursorPosition(playerOldPos.X, playerOldPos.Y);
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write(".");
// If the player is "outside of the border" on the right hand side, they've reached the one gap that is the exit.
if(playerPos.X == theMaze.GetLength(0)-1)
{
return ShowLevelComplete();
}
// Start a new calculation of the monster's path
for(int i = 0; i < numMonsters; i++)
{
StartMonsterPathCalculation(playerPos, i);
}
}
}
// Move the monsters slower than the player can move.
if(loopCount % 10 == 1)
{
// Move the monster towards the player along the path previously calculated from the calculation tasks.
bool validMonsterMove;
for(int i = 0; i < numMonsters; i++)
{
// If there is a path
if(monsterPath[i] != null && monsterPath[i].Count > 0)
{
MazePoint newPos;
ShowEntity(monsterPos[i]!.Value, ' ', ConsoleColor.Black); // Clear where the monster was.
(newPos, validMonsterMove) = MoveInDirection(monsterPath[i].First().Direction, monsterPos[i]!.Value);
monsterPos[i] = newPos;
monsterPath[i].RemoveAt(0);
if(!validMonsterMove)
{
// Um, something went wrong with following the steps (bug in code).
// issue a recalculate
monsterPath[i] = [];
StartMonsterPathCalculation(playerPos, i);
}
}
}
}
loopCount++;
if(loopCount > 100)
loopCount = 0;
Thread.Sleep(50);
}
}
protected void MakeMaze(int maxX, int maxY)
{
bool [,] mazeData;
// Make sure dimensions are odd, as per the requirements of this algorithm
if(maxX % 2 == 0)
maxX--;
if(maxY % 2 == 0)
maxY--;
mazeData = MazeRecursiveGenerator.GenerateMaze(maxX, maxY, MazeRecursiveGenerator.MazeMode.Loops);
theMaze = GameUtils.ConvertToCharMaze(mazeData, WallCharacter);
}
protected static void ShowEntity(MazePoint entityPosition, char displayCharacter, ConsoleColor colour)
{
// A small helper to show either the player, or the monsters (depending on the parameters provided).
Console.ForegroundColor = colour;
Console.SetCursorPosition(entityPosition.X, entityPosition.Y);
Console.Write(displayCharacter);
}
protected void DisplayMaze(int levelNumber)
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.White;
for(int y = 0; y < theMaze.GetLength(1); y++)
{
Console.SetCursorPosition(0,y);
for(int x = 0; x < theMaze.GetLength(0); x++)
{
Console.Write(theMaze[x,y]);
}
}
Console.SetCursorPosition(0, theMaze.GetLength(1));
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($" Lvl: {levelNumber}. WASD or arrow keys to move. Esc to quit.");
}
protected Tuple<MazePoint, bool> MoveInDirection(EntityAction userAction, MazePoint pos)
{
var newPos = userAction switch
{
EntityAction.Up => new MazePoint(pos.X, pos.Y - 1),
EntityAction.Left => new MazePoint(pos.X - 1, pos.Y),
EntityAction.Down => new MazePoint(pos.X, pos.Y + 1),
EntityAction.Right => new MazePoint(pos.X + 1, pos.Y),
_ => new MazePoint(pos.X, pos.Y),
};
if(newPos.X < 0 || newPos.Y < 0 || newPos.X >= theMaze.GetLength(0) || newPos.Y >= theMaze.GetLength(1) || theMaze[newPos.X,newPos.Y] != ' ' )
{
return new (pos, false); // can't move to the new location.
}
return new (newPos, true);
}
protected bool DisplayCaught()
{
ShowEntity(playerPos, CaughtCharacter, ConsoleColor.Red);
Console.SetCursorPosition((Console.WindowWidth-14)/2, Console.WindowHeight/2);
Console.WriteLine(" You were caught! ");
Console.SetCursorPosition((Console.WindowWidth-14)/2, (Console.WindowHeight/2) +2);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Press space to continue");
GameUtils.WaitForEscapeOrSpace();
return true;
}
protected bool ShowLevelComplete()
{
ShowEntity(playerPos, PlayerCharacterA, ConsoleColor.Green); // Show the player at the exit.
if(numMonsters < MaxLevel)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.SetCursorPosition((Console.WindowWidth-40)/2, Console.WindowHeight/2);
Console.WriteLine(" You escaped, ready for the next level? ");
}
else
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.SetCursorPosition((Console.WindowWidth-14)/2, Console.WindowHeight/2);
Console.WriteLine(" You won! ");
}
Console.SetCursorPosition((Console.WindowWidth-38)/2, (Console.WindowHeight/2)+2);
Console.WriteLine("Press space to continue or Esc to exit");
return GameUtils.WaitForEscapeOrSpace();
}
protected void StartMonsterPathCalculation(MazePoint playerPos, int monsterIndex)
{
if(monsterPathCalcCancelSources[monsterIndex] != null)
{
monsterPathCalcCancelSources[monsterIndex].Cancel();
monsterPathCalcCancelSources[monsterIndex].Dispose();
};
monsterPathCalcCancelSources[monsterIndex] = new CancellationTokenSource();
Task.Run(async () => monsterPath[monsterIndex] = await FindPathToTargetAsync(playerPos, monsterPos[monsterIndex]!.Value, monsterPathCalcCancelSources[monsterIndex].Token));
}
// This method should is a background task, ran on a threadpool thread, to calculate where the monsters should move.
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
protected async Task<List<MazeStep>> FindPathToTargetAsync(MazePoint targetPos, MazePoint currentPos,
CancellationToken cancellationToken)
#pragma warning restore CS1998
{
var directions = new List<EntityAction> { EntityAction.Left, EntityAction.Right, EntityAction.Up, EntityAction.Down };
var queue = new Queue<MazeStep>();
var cameFrom = new Dictionary<MazePoint, MazeStep?>(); // To reconstruct the path
var visited = new HashSet<MazePoint>();
queue.Enqueue(new MazeStep(currentPos, EntityAction.None));
visited.Add(currentPos);
while (queue.Count > 0 && !cancellationToken.IsCancellationRequested)
{
var currentStep = queue.Dequeue();
var current = currentStep.Position;
// If we've reached the target, reconstruct the path
if (current.X == targetPos.X && current.Y == targetPos.Y)
return ReconstructPath(cameFrom, currentPos, targetPos);
foreach (var direction in directions)
{
var (nextPos, isValid) = MoveInDirection(direction, current);
if (isValid && !visited.Contains(nextPos))
{
visited.Add(nextPos);
queue.Enqueue(new MazeStep(nextPos, direction));
cameFrom[nextPos] = new MazeStep(current, direction);
}
}
}
return []; // No path found
}
private static List<MazeStep> ReconstructPath(Dictionary<MazePoint, MazeStep?> cameFrom, MazePoint start, MazePoint end)
{
var path = new List<MazeStep>();
var current = end;
while (current != start)
{
var prevStep = cameFrom[current];
if (prevStep == null)
break;
var direction = prevStep.Direction;
path.Add(new MazeStep(current, direction));
current = prevStep.Position;
}
path.Reverse();
return path;
}
}