Optimal path for square map

This commit is contained in:
2020-12-14 18:24:10 -07:00
parent 80ea690330
commit 5bb82f7579
11 changed files with 209 additions and 79 deletions

View File

@@ -6,9 +6,14 @@ namespace ConsoleApp.Maps
public class SquareMap : ISquareMap
{
public Cell[,] Map { get;}
public ICell StartingCell { get; }
public ICell LastCell { get; }
public Cell StartingCell { get; }
public Cell LastCell { get; }
public int Height { get; }
public int Width { get; }
private int _mapWidth;
private int _mapHeight;
public SquareMap(int x, int y)
{
//convert to cm
@@ -18,31 +23,43 @@ namespace ConsoleApp.Maps
var xCellCount = (int)Math.Ceiling((decimal) (x) / 25);
var yCellCount = (int)Math.Ceiling((decimal) (y) / 25);
//set Width and height fields
_mapWidth = xCellCount-1;
_mapHeight = yCellCount-1;
Height = _mapHeight;
Width = _mapWidth;
//Initialize Map
Map = new Cell[xCellCount, yCellCount];
//set last cell;
StartingCell = Map[0, 0];
Map = new Cell[xCellCount, yCellCount];
for (int i = 0; i < x; i++)
for (int i = 0; i < xCellCount; i++)
{
for (int j = 0; j < y; j++)
for (int j = 0; j < yCellCount; j++)
{
Map[i,j] = new Cell(i, j);
}
}
}
public List<ICell> PossibleMoves(ICell currentCell)
public List<GlobalDirection> PossibleMoves(ICell currentCell)
{
var forward = (currentCell.X , currentCell.Y + 1);
var backwards= (currentCell.X , currentCell.Y - 1);
var right = (currentCell.X + 1, currentCell.Y);
var left = (currentCell.X - 1, currentCell.Y);
var possibles = new List<ICell>();
possibles.Add(Map[forward.Item1, forward.Item2]);
possibles.Add(Map[backwards.Item1, backwards.Item2]);
possibles.Add(Map[right.Item1, right.Item2]);
possibles.Add(Map[left.Item1, left.Item2]);
var possibles = new List<GlobalDirection>();
if (currentCell.X != 0)
possibles.Add(GlobalDirection.West);
if (currentCell.X != _mapWidth)
possibles.Add(GlobalDirection.East);
if (currentCell.Y != _mapHeight)
possibles.Add(GlobalDirection.North);
if (currentCell.Y != 0)
possibles.Add(GlobalDirection.South);
return possibles;
}
public Cell this[in int x, in int y] => Map[x, y];
}
}