• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

samsmithnz / PuzzleSolver / 4064748535

pending completion
4064748535

push

github

GitHub
Merge pull request #28 from samsmithnz/AddingMapAndPathFinding

806 of 854 branches covered (94.38%)

Branch coverage included in aggregate %.

342 of 342 new or added lines in 8 files covered. (100.0%)

1165 of 1194 relevant lines covered (97.57%)

1263151.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.76
/src/PuzzleSolver/Map/MapCore.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Numerics;
5
using System.Text;
6

7
namespace PuzzleSolver.Map
8
{
9
    public static class MapCore
10
    {
11
        /// <summary>
12
        /// Initialize a map array
13
        /// </summary>
14
        /// <param name="xMax">x size</param>
15
        /// <param name="zMax">z size</param>
16
        /// <param name="initialString">The initial string to initialize the map with - usually ""</param>
17
        /// <returns>The populated map/array</returns>
18
        public static string[,] InitializeMap(int xMax, int yMax, string initialString = "")
19
        {
3✔
20
            string[,] map = new string[xMax, yMax];
3✔
21

22
            //Initialize the map
23
            for (int y = 0; y < yMax; y++)
36✔
24
            {
15✔
25
                for (int x = 0; x < xMax; x++)
180✔
26
                {
75✔
27
                    map[x, y] = initialString;
75✔
28
                }
75✔
29
            }
15✔
30

31
            return map;
3✔
32
        }
3✔
33

34

35
        //public static List<Vector3> GetMapArea(string[,,] map, Vector3 sourceLocation, int range, bool lookingForFOV = true, bool includeSourceLocation = false)
36
        //{
37
        //    int startingX = (int)sourceLocation.X;
38
        //    int startingZ = (int)sourceLocation.Z;
39

40
        //    //Use the range to find the borders in each primary direction from the starting location
41
        //    int minX = startingX - range;
42
        //    if (minX < 0)
43
        //    {
44
        //        minX = 0;
45
        //    }
46
        //    int maxX = startingX + range;
47
        //    if (maxX > map.GetLength(0) - 1)
48
        //    {
49
        //        maxX = map.GetLength(0) - 1;
50
        //    }
51
        //    int minZ = startingZ - range;
52
        //    if (minZ < 0)
53
        //    {
54
        //        minZ = 0;
55
        //    }
56
        //    int maxZ = startingZ + range;
57
        //    if (maxZ > map.GetLength(2) - 1)
58
        //    {
59
        //        maxZ = map.GetLength(2) - 1;
60
        //    }
61

62
        //    //Get a list of all border squares
63
        //    HashSet<Vector3> borderTiles = new HashSet<Vector3>();
64
        //    //Add the top and bottom rows
65
        //    for (int x = minX; x <= maxX; x++)
66
        //    {
67
        //        borderTiles.Add(new Vector3(x, 0, minZ));
68
        //        borderTiles.Add(new Vector3(x, 0, maxZ));
69
        //    }
70
        //    //Add the left and right sides
71
        //    for (int z = minZ; z < maxZ; z++)
72
        //    {
73
        //        borderTiles.Add(new Vector3(minX, 0, z));
74
        //        borderTiles.Add(new Vector3(maxX, 0, z));
75
        //    }
76

77
        //    //For each border tile, draw a line from the starting point to the border
78
        //    HashSet<Vector3> results = new HashSet<Vector3>();
79
        //    //foreach (Vector3 borderItem in borderTiles)
80
        //    //{
81
        //    //    List<Vector3> singleLineCheck = FieldOfView.GetPointsOnLine(new Vector3(startingX, 0, startingZ), borderItem);
82
        //    //    if (singleLineCheck.Count > 0 &&
83
        //    //        singleLineCheck[singleLineCheck.Count - 1].X == startingX &&
84
        //    //        singleLineCheck[singleLineCheck.Count - 1].Z == startingZ)
85
        //    //    {
86
        //    //        //Reverse the list, so that items are in order from source to destination
87
        //    //        singleLineCheck.Reverse();
88
        //    //    }
89
        //    //    double lineLength = GetLengthOfLine(singleLineCheck[0], singleLineCheck[singleLineCheck.Count - 1], 1);
90
        //    //    double lineSegment = lineLength / singleLineCheck.Count;
91
        //    //    double currentLength = 0;
92
        //    //    for (int i = 0; i < singleLineCheck.Count; i++)
93
        //    //    {
94
        //    //        currentLength += lineSegment;
95
        //    //        Vector3 fovItem = singleLineCheck[i];
96
        //    //        if (fovItem.X >= 0 && fovItem.Y >= 0 && fovItem.Z >= 0)
97
        //    //        {
98
        //    //            //If we find an object, stop adding tiles
99
        //    //            if (lookingForFOV && map[(int)fovItem.X, (int)fovItem.Y, (int)fovItem.Z] == CoverType.FullCover)
100
        //    //            {
101
        //    //                //Add the wall
102
        //    //                results.Add(fovItem);
103
        //    //                //Then break!
104
        //    //                break;
105
        //    //            }
106
        //    //            else if ((int)fovItem.X == startingX && (int)fovItem.Z == startingZ)
107
        //    //            {
108
        //    //                //Don't add this one, it's the origin/ where the character is looking from
109
        //    //            }
110
        //    //            else
111
        //    //            {
112
        //    //                results.Add(fovItem);
113
        //    //            }
114
        //    //        }
115
        //    //        //We don't round, so this will extend the range a tiny part - but I think that is ok.
116
        //    //        if (currentLength >= range)
117
        //    //        {
118
        //    //            break;
119
        //    //        }
120
        //    //    }
121
        //    //}
122
        //    if (includeSourceLocation)
123
        //    {
124
        //        results.Add(sourceLocation);
125
        //    }
126

127
        //    return results.ToList();
128
        //}
129

130
        //public static double GetLengthOfLine(Vector3 start, Vector3 end, int decimals = 0)
131
        //{
132
        //    double lineLength = Math.Sqrt(Math.Pow((end.X - start.X), 2) + Math.Pow((end.Z - start.Z), 2));
133
        //    return Math.Round(lineLength, decimals);
134
        //}
135

136
        public static string GetMapString(string[,] map, bool stripOutBlanks = false)
137
        {
2✔
138
            int xMax = map.GetLength(0);
2✔
139
            int yMax = map.GetLength(1);
2✔
140
            StringBuilder sb = new StringBuilder();
2✔
141
            sb.Append(Environment.NewLine);
2✔
142
            for (int y = yMax - 1; y >= 0; y--)
24✔
143
            {
10✔
144
                StringBuilder sbLine = new StringBuilder();
10✔
145
                for (int x = 0; x < xMax; x++)
120✔
146
                {
50✔
147
                    if (map[x, y] != "")
50✔
148
                    {
2✔
149
                        sbLine.Append(map[x, y] + " ");
2✔
150
                    }
2✔
151
                    else
152
                    {
48✔
153
                        sbLine.Append(". ");
48✔
154
                    }
48✔
155
                }
50✔
156
                sbLine.Append(Environment.NewLine);
10✔
157
                //If there is nothing on the map line, don't display it.
158
                //This optimizes the ASCII maps to remove white space
159
                if (stripOutBlanks)
10!
160
                {
×
161
                    int dotCount = sbLine.ToString().Split('.').Count() - 1;
×
162
                    if (dotCount < xMax)
×
163
                    {
×
164
                        sb.Append(sbLine.ToString());
×
165
                    }
×
166
                }
×
167
                else
168
                {
10✔
169
                    sb.Append(sbLine.ToString());
10✔
170
                }
10✔
171
            }
10✔
172
            return sb.ToString();
2✔
173
        }
2✔
174

175
        //public static string[,,] ApplyListToMap(string[,,] map, List<Vector3> list, string tile)
176
        //{
177
        //    foreach (Vector3 item in list)
178
        //    {
179
        //        //Check that the square is empty - we don't want to overwrite something that exists and only put a tile on an unused tile
180
        //        if (map[(int)item.X, (int)item.Y, (int)item.Z] == "")
181
        //        {
182
        //            map[(int)item.X, (int)item.Y, (int)item.Z] = tile;
183
        //        }
184
        //    }
185
        //    return map;
186
        //}
187

188
        //public static string[,,] ApplyListToExistingMap(string[,,] map, List<Vector3> list, string tile)
189
        //{
190
        //    foreach (Vector3 item in list)
191
        //    {
192
        //        map[(int)item.X, (int)item.Y, (int)item.Z] = tile;
193
        //    }
194
        //    return map;
195
        //}
196

197
        //public static string GetMapStringWithItems(string[,,] map, List<Vector3> list)
198
        //{
199
        //    string[,,] mapNew = MapCore.ApplyListToMap((string[,,])map.Clone(), list, "o");
200
        //    string mapString = MapCore.GetMapString(mapNew);
201
        //    return mapString;
202
        //}
203

204
        //public static string GetMapStringWithItemLayers(string[,,] map, List<Vector3> baseList, List<Vector3> overlayList)
205
        //{
206
        //    string[,,] mapNew = MapCore.ApplyListToMap((string[,,])map.Clone(), baseList, "o");
207
        //    mapNew = MapCore.ApplyListToExistingMap(mapNew, overlayList, "O");
208

209
        //    string mapString = MapCore.GetMapString(mapNew);
210
        //    return mapString;
211
        //}
212

213
        //public static string GetMapStringWithAIValuesFirst(string[,,] mapTemplate, List<KeyValuePair<Vector3, int>> list)
214
        //{
215
        //    string[,,] map = (string[,,])mapTemplate.Clone();
216
        //    foreach (KeyValuePair<Vector3, int> item in list)
217
        //    {
218
        //        map[(int)item.Key.X, (int)item.Key.Y, (int)item.Key.Z] = item.Value.ToString();
219
        //    }
220
        //    return MapCore.GetMapString(map);
221
        //}
222

223
        //public static string GetMapStringWithAIValuesSecond(string[,,] mapTemplate, List<KeyValuePair<Vector3, AIAction>> list)
224
        //{
225
        //    string[,,] map = (string[,,])mapTemplate.Clone();
226
        //    if (list != null)
227
        //    {
228
        //        foreach (KeyValuePair<Vector3, AIAction> item in list)
229
        //        {
230
        //            map[(int)item.Key.X, (int)item.Key.Y, (int)item.Key.Z] = item.Value.Score.ToString();
231
        //        }
232
        //    }
233
        //    return MapCore.GetMapString(map, true);
234
        //}
235

236
        //public static string GetMapStringWithMapMask(string[,,] map, string[,,] mapMask)
237
        //{
238
        //    int xMax = map.GetLength(0);
239
        //    //int yMax = map.GetLength(1);
240
        //    int zMax = map.GetLength(2);
241
        //    int xMaskMax = map.GetLength(0);
242
        //    //int yMaskMax = map.GetLength(1);
243
        //    int zMaskMax = map.GetLength(2);
244
        //    if (xMax != xMaskMax && zMax != zMaskMax)
245
        //    {
246
        //        throw new Exception("Mask map is not the same size as the parent map");
247
        //    }
248

249
        //    StringBuilder sb = new StringBuilder();
250
        //    sb.Append(Environment.NewLine);
251
        //    int y = 0;
252
        //    for (int z = zMax - 1; z >= 0; z--)
253
        //    {
254
        //        for (int x = 0; x < xMax; x++)
255
        //        {
256
        //            if (mapMask[x, y, z] != "")
257
        //            {
258
        //                sb.Append(mapMask[x, y, z] + " ");
259
        //            }
260
        //            else
261
        //            {
262
        //                if (map[x, y, z] != "")
263
        //                {
264
        //                    sb.Append(map[x, y, z] + " ");
265
        //                }
266
        //                else
267
        //                {
268
        //                    sb.Append(". ");
269
        //                }
270
        //            }
271
        //        }
272
        //        sb.Append(Environment.NewLine);
273
        //    }
274
        //    return sb.ToString();
275
        //}
276

277
        //public static List<Vector3> FindAdjacentTile(string[,,] map, Vector3 currentLocation, string tileToFind)
278
        //{
279
        //    int width = map.GetLength(0);
280
        //    //int height = map.GetLength(1);
281
        //    int breadth = map.GetLength(2);
282
        //    List<Vector3> result = new List<Vector3>();
283

284
        //    //Make adjustments to ensure that the search doesn't go off the edges of the map
285
        //    int xMin = Convert.ToInt32(currentLocation.X) - 1;
286
        //    if (xMin < 0)
287
        //    {
288
        //        xMin = 0;
289
        //    }
290
        //    int xMax = Convert.ToInt32(currentLocation.X) + 1;
291
        //    if (xMax > width - 1)
292
        //    {
293
        //        xMax = width - 1;
294
        //    }
295
        //    int zMin = Convert.ToInt32(currentLocation.Z) - 1;
296
        //    if (zMin < 0)
297
        //    {
298
        //        zMin = 0;
299
        //    }
300
        //    int zMax = Convert.ToInt32(currentLocation.Z) + 1;
301
        //    if (zMax > breadth - 1)
302
        //    {
303
        //        zMax = breadth - 1;
304
        //    }
305

306
        //    //Get possible tiles, within constraints of map, including only square titles from current position (not diagonally)
307
        //    if (map[Convert.ToInt32(currentLocation.X), 0, zMax] == tileToFind)
308
        //    {
309
        //        result.Add(new Vector3(currentLocation.X, 0f, zMax));
310
        //    }
311
        //    if (map[xMax, 0, Convert.ToInt32(currentLocation.Z)] == tileToFind)
312
        //    {
313
        //        result.Add(new Vector3(xMax, 0f, currentLocation.Z));
314
        //    }
315
        //    if (map[Convert.ToInt32(currentLocation.X), 0, zMin] == tileToFind)
316
        //    {
317
        //        result.Add(new Vector3(currentLocation.X, 0f, zMin));
318
        //    }
319
        //    if (map[xMin, 0, Convert.ToInt32(currentLocation.Z)] == tileToFind)
320
        //    {
321
        //        result.Add(new Vector3(xMin, 0f, currentLocation.Z));
322
        //    }
323
        //    return result;
324
        //}
325
    }
326
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc