-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.cs
More file actions
70 lines (65 loc) · 1.62 KB
/
Copy pathMap.cs
File metadata and controls
70 lines (65 loc) · 1.62 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
using System;
using System.Collections.Generic;
using System.Drawing;
namespace Dungeon
{
public class Map
{
public readonly MapCell[,] Dungeon;
public readonly Point InitialPosition;
public readonly Point Exit;
public readonly Point[] Chests;
private Map(MapCell[,] dungeon, Point initialPosition, Point exit, Point[] chests)
{
Dungeon = dungeon;
InitialPosition = initialPosition;
Exit = exit;
Chests = chests;
}
public static Map FromText(string text)
{
var lines = text.Split(new[] { "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
return FromLines(lines);
}
public static Map FromLines(string[] lines)
{
var dungeon = new MapCell[lines[0].Length, lines.Length];
var initialPosition = Point.Empty;
var exit = Point.Empty;
var chests = new List<Point>();
for (var y = 0; y < lines.Length; y++)
{
for (var x = 0; x < lines[0].Length; x++)
{
switch (lines[y][x])
{
case '#':
dungeon[x, y] = MapCell.Wall;
break;
case 'P':
dungeon[x, y] = MapCell.Empty;
initialPosition = new Point(x, y);
break;
case 'C':
dungeon[x, y] = MapCell.Empty;
chests.Add(new Point(x, y));
break;
case 'E':
dungeon[x, y] = MapCell.Empty;
exit = new Point(x, y);
break;
default:
dungeon[x, y] = MapCell.Empty;
break;
}
}
}
return new Map(dungeon, initialPosition, exit, chests.ToArray());
}
public bool InBounds(Point point)
{
var bounds = new Rectangle(0, 0, Dungeon.GetLength(0), Dungeon.GetLength(1));
return bounds.Contains(point);
}
}
}