public static bool OnOneLine(Node sNode, Node fNode, NodeArray nodesArray) { var destination = FindDestination(sNode, fNode); var finishPoint = new Vector2(Convert.ToInt32(fNode.Position.X), Convert.ToInt32(fNode.Position.Y)); var line = new StraightLine(Convert.ToInt32(sNode.Position.X), Convert.ToInt32(sNode.Position.Y), destination, nodesArray); if (line.Points.Exists(arg => arg == finishPoint)) { for (var i = 1; i < line.Points.Count; ++i) { var pointX = Convert.ToInt32(line.Points[i].X); var pointY = Convert.ToInt32(line.Points[i].Y); if (nodesArray.Array[pointX, pointY].IsObstacle) { return(false); } if (line.Points[i] == finishPoint) { return(true); } if ((destination == Destinations.UpRight || destination == Destinations.DownRight) && !nodesArray.Array[pointX, pointY + 1].IsObstacle) { return(false); } if ((destination == Destinations.UpLeft || destination == Destinations.DownLeft) && !nodesArray.Array[pointX, pointY - 1].IsObstacle) { return(false); } } } return(false); }
public static void InitializeMap(StreamReader file) { var line = file.ReadLine(); var height = 0; var widght = 0; line = file.ReadLine(); if (line != null) { string[] splitLine = line.Split(' '); height = Convert.ToInt32(splitLine[1]); } line = file.ReadLine(); if (line != null) { string[] splitLine = line.Split(' '); widght = Convert.ToInt32(splitLine[1]); } Node[,] nodesArray = new Node[height, widght]; line = file.ReadLine(); for (var i = 0; i < height; ++i) { line = file.ReadLine(); for (var j = 0; j < widght; ++j) { if (line != null) { var symbol = Convert.ToChar(line[j]); nodesArray[i, j] = new Node(i, j, symbol); } } } NodesArray = new NodeArray(height, widght, nodesArray); }
public StraightLine(int x, int y, Destinations destination, NodeArray nodesArray) { Destination = destination; Start = new Vector2(x, y); if (destination == Destinations.Down) { Finish = new Vector2(nodesArray.widght - 1, y); } else if (destination == Destinations.Up) { Finish = new Vector2(0, y); } else if (destination == Destinations.Left) { Finish = new Vector2(x, 0); } else if (destination == Destinations.Right) { Finish = new Vector2(x, nodesArray.height - 1); } else if (destination == Destinations.DownLeft) { var delta1 = nodesArray.widght - 1 - x; var delta2 = y; Finish = delta1 < delta2 ? new Vector2(nodesArray.widght - 1, y - delta1) : new Vector2(x + delta2, 0); } else if (destination == Destinations.UpLeft) { var delta1 = x; var delta2 = y; Finish = delta1 < delta2 ? new Vector2(0, y - delta1) : new Vector2(x - delta2, 0); } else if (destination == Destinations.DownRight) { var delta1 = nodesArray.widght - 1 - x; var delta2 = nodesArray.height - 1 - y; Finish = delta1 < delta2 ? new Vector2(nodesArray.widght - 1, y + delta1) : new Vector2(x + delta2, nodesArray.height - 1); } else if (destination == Destinations.UpRight) { var delta1 = x; var delta2 = nodesArray.widght - 1 - y; Finish = delta1 < delta2 ? new Vector2(0, y + delta1) : new Vector2(x - delta2, nodesArray.widght - 1); } Points = FindMiddlePoints(Start, Finish, destination); }