Optimal path for square map
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
namespace ConsoleApp.Maps
|
||||
{
|
||||
public enum Direction
|
||||
public enum GlobalDirection
|
||||
{
|
||||
|
||||
North,
|
||||
South,
|
||||
East,
|
||||
West
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,3 @@
|
||||
using System.Net.NetworkInformation;
|
||||
|
||||
namespace ConsoleApp.Maps
|
||||
{
|
||||
public class Oreientation
|
||||
|
||||
@@ -6,8 +6,8 @@ namespace ConsoleApp.Maps
|
||||
public class HexMap : IHexMap
|
||||
{
|
||||
public Cell[,] Map { get; }
|
||||
public ICell StartingCell { get; }
|
||||
public ICell LastCell { get; }
|
||||
public Cell StartingCell { get; }
|
||||
public Cell LastCell { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Generate Hex map with cells of 25cm X 25cm
|
||||
@@ -23,9 +23,11 @@ namespace ConsoleApp.Maps
|
||||
var xCellCount = (int)Math.Ceiling((decimal) (x) / 25);
|
||||
var yCellCount = (int)Math.Ceiling((decimal) (y) / 25);
|
||||
|
||||
//Initialize Map
|
||||
Map = new Cell[xCellCount, yCellCount];
|
||||
|
||||
//set last cell;
|
||||
StartingCell = Map[0, 0];
|
||||
Map = new Cell[xCellCount, yCellCount];
|
||||
for (int r = 0; r < yCellCount; r++) {
|
||||
int r_offset = Convert.ToInt32(Math.Floor(Convert.ToDouble(r)/2));
|
||||
for (int q = r_offset; q < xCellCount - r_offset; q++) {
|
||||
@@ -35,7 +37,7 @@ namespace ConsoleApp.Maps
|
||||
}
|
||||
}
|
||||
|
||||
public List<ICell> PossibleMoves(ICell currentCell)
|
||||
public List<GlobalDirection> PossibleMoves(ICell currentCell)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@ namespace ConsoleApp.Maps
|
||||
public interface IMap
|
||||
{
|
||||
public Cell[,] Map { get; }
|
||||
ICell StartingCell { get; }
|
||||
ICell LastCell { get; }
|
||||
public List<ICell> PossibleMoves(ICell currentCell);
|
||||
Cell StartingCell { get; }
|
||||
Cell LastCell { get; }
|
||||
public List<GlobalDirection> PossibleMoves(ICell currentCell);
|
||||
}
|
||||
}
|
||||
@@ -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];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user