static void Main(string[] args) { var startingPoint = new List<Node>(); var endPoint = new Node(); var closedPositions = new List<Point>(); var chessPiece = new CurrentPiece(); try { string inputFileName = args[0]; string outputFileName = args[1]; if (inputFileName != null) { var startNode = new Node(); FileHelpers.GetDataFromFile(inputFileName, endPoint, startNode, closedPositions, chessPiece); startingPoint.Add(startNode); } if (MoveMethods.FindPath(outputFileName, startingPoint, endPoint, closedPositions, chessPiece)) Console.WriteLine("Paths successfully found."); } catch (Exception e) { Console.WriteLine("Missing or false parameters!"); Console.WriteLine(e.Message); } Console.WriteLine("Press enter to terminate ... "); Console.ReadLine(); }
private static void GenerateMoves(Node currentNode, List<Point> forbiddenCoords, CurrentPiece chessPiece) { var positions = PositionsHelpers.GetChessPiecePositions(chessPiece); foreach (var availableMoveCoordinates in positions) { Point newCoords = currentNode.Coords + (Size)availableMoveCoordinates; bool forbidden = forbiddenCoords.Any(forbiddenCoord => newCoords == forbiddenCoord); if (CancelMoves) { if (availableMoveCoordinates.X == 7 || availableMoveCoordinates.X == -7 || availableMoveCoordinates.Y == 7 || availableMoveCoordinates.X == -7) CancelMoves = false; continue; } if (forbidden) { if (chessPiece.IsQueenRookOrBishop) CancelMoves = true; continue; } if (newCoords.X >= 0 && newCoords.Y >= 0 && newCoords.X < Board.AxisX && newCoords.Y < Board.AxisY) { if (currentNode.Parent != null && currentNode.Parent.Coords == newCoords) continue; var childNode = new Node {Coords = newCoords, Parent = currentNode}; currentNode.Children.Add(childNode); } } }
public static void GetDataFromFile(string inputFileName, Node endPoint, Node startNode, List<Point> closedPositions, CurrentPiece piece) { string[] input = File.ReadAllLines(Path.IsPathRooted(inputFileName) ? inputFileName : Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"SourceFiles\", inputFileName)); var startingPointCoord = CoordinatesHelperMethods.LetterToCoord(input[2]); if (startingPointCoord != null) startNode.Coords = (Point)startingPointCoord; var endingPointCoord = CoordinatesHelperMethods.LetterToCoord(input[3]); if (endingPointCoord != null) endPoint.Coords = (Point)endingPointCoord; if (input[4] != null) { string[] closedCoords = input[4].Split(new[] { ",", " ", ", " }, StringSplitOptions.RemoveEmptyEntries); closedPositions.AddRange(from closedCoord in closedCoords let closedPosition = new Point() let letterToCoord = CoordinatesHelperMethods.LetterToCoord(closedCoord) where letterToCoord != null select (Point)letterToCoord); } piece.ChessPiece = input[5].Contains("-") ? "Knight" : input[5]; try { Board.AxisX = Convert.ToInt32(input[1]); Board.AxisY = Convert.ToInt32(input[1]); } catch (Exception) { Board.AxisX = 8; Board.AxisY = 8; } }
public static bool FindPath(string outputFileName, List<Node> source, Node to, List<Point> closedPositions, CurrentPiece chessPiece) { var nextLevel = new List<Node>(); bool noNextLevel = false; foreach (var child in source) { GenerateMoves(child, closedPositions, chessPiece); if (CoordinatesHelperMethods.CheckDestinationCoords(child, to.Coords, outputFileName, chessPiece)) { noNextLevel = true; } nextLevel.AddRange(child.Children); if (!nextLevel.Any() || to.Coords.X >= Board.AxisX || to.Coords.Y >= Board.AxisY) { Console.WriteLine("Could not find path to specified position."); return false; } } if (noNextLevel) return true; return FindPath(outputFileName, nextLevel, to, closedPositions, chessPiece); }
public static bool CheckDestinationCoords(Node child, Point to, string outputFileName, CurrentPiece chessPiece) { if (child.Coords == to) { MoveMethods.Moves = 0; MoveMethods.CoordinatesToDestination.Clear(); var currentNode = child; do { var parent = currentNode.Parent; MoveMethods.CoordinatesToDestination.Add(currentNode.Coords); currentNode = parent; MoveMethods.Moves++; } while (currentNode.Parent != null); if (MoveMethods.MinMoves == 0) MoveMethods.MinMoves = MoveMethods.Moves; if (MoveMethods.Moves > MoveMethods.MinMoves && MoveMethods.MinMoves > 0) return false; MoveMethods.MinMoves = MoveMethods.Moves; MoveMethods.CoordinatesToDestination.Reverse(); FileHelpers.WriteToFile(outputFileName, MoveMethods.CoordinatesToDestination, chessPiece); return true; } return false; }