public Map(int[,] inputMap, PointOnMap[] goals, PointOnMap[] diamonds, PointOnMap robotPosition, Route wholeRoute) { map = inputMap; this.goals = goals; this.diamonds = diamonds; this.robotPosition = robotPosition; this.wholeRoute = wholeRoute; }
/// <summary> /// Constructor of Map /// </summary> public Map(int mapWidth, int mapHeight, int inputDiamonds, int inputGoals, String[] mapString) { map = new int[mapHeight, mapWidth]; // mapHeight = rows mapWidth = cols diamonds = new PointOnMap[inputDiamonds]; goals = new PointOnMap[inputGoals]; wholeRoute = new Route(); int x = 0, y = 0, diamondCounter = 0, goalCounter = 0; foreach (string row in mapString) { foreach (char field in row) { switch (field) { case '.': map[x, y] = (int)FieldType.Walkable; break; case 'J': map[x, y] = (int)FieldType.Diamond; diamonds[diamondCounter] = new PointOnMap(x, y, FieldType.Diamond); diamondCounter++; break; case 'M': map[x, y] = (int)FieldType.Robot; robotPosition = new PointOnMap(x, y, FieldType.Robot); break; case 'G': map[x, y] = (int)FieldType.Goal; goals[goalCounter] = new PointOnMap(x, y, FieldType.Goal); goalCounter++; break; case 'X': map[x, y] = (int)FieldType.Unwalkable; break; case ' ': map[x, y] = (int)FieldType.Unwalkable; break; default: Console.Error.WriteLine("Faulty Input Letter: " + field); Environment.Exit(1); Console.Read(); break; } y++; } y = 0; x++; } }
public Robot(Map currentMap) { map = currentMap; currentPosition = map.robotPosition; diamondPositions = map.diamonds; currentPosition.order = 1; usefulRoutes = calculateUsefulRoutes(calculateShiftPoints(), calculatePossibleRobotPositions()); // foreach (Route r in usefulRoutes) ///{ // Console.Write(r.ToString() + "----------\n\n"); //} //List<Tuple<PointOnMap, PointOnMap>> possibleShiftPoints }
public int[,] calculatePossibleRobotPositions() { int[,] movementMap = (int[, ]) this.map.map.Clone(); // Deep Copy Array.Clear(movementMap, 0, movementMap.Length); List <PointOnMap> stillToInspect = new List <PointOnMap>(); stillToInspect.Add(currentPosition); while (stillToInspect.Count > 0) { PointOnMap currentlyInspectedPosition = stillToInspect[0]; stillToInspect.RemoveAt(0); if ((currentlyInspectedPosition.FieldType == FieldType.Robot || currentlyInspectedPosition.FieldType == FieldType.Goal || currentlyInspectedPosition.FieldType == FieldType.Walkable) && (currentlyInspectedPosition.order < movementMap[currentlyInspectedPosition.Row, currentlyInspectedPosition.Column] || movementMap[currentlyInspectedPosition.Row, currentlyInspectedPosition.Column] == 0)) { movementMap[currentlyInspectedPosition.Row, currentlyInspectedPosition.Column] = currentlyInspectedPosition.order; PointOnMap pNorth = map.GetElementNorth(currentlyInspectedPosition); pNorth.order = currentlyInspectedPosition.order + 1; stillToInspect.Add(pNorth); PointOnMap pEast = map.GetElementEast(currentlyInspectedPosition); pEast.order = currentlyInspectedPosition.order + 1; stillToInspect.Add(pEast); PointOnMap pSouth = map.GetElementSouth(currentlyInspectedPosition); pSouth.order = currentlyInspectedPosition.order + 1; stillToInspect.Add(pSouth); PointOnMap pWest = map.GetElementWest(currentlyInspectedPosition); pWest.order = currentlyInspectedPosition.order + 1; stillToInspect.Add(pWest); } } //printMovementMap(movementMap); return(movementMap); }
private void searchDiamonds() { //Console.WriteLine("Diamonds: "); int diamondCounter = 0; for (int i = 0; i < map.GetLength(0); i++) { for (int j = 0; j < map.GetLength(1); j++) { if ((FieldType)map[i, j] == FieldType.Diamond) { diamonds[diamondCounter] = new PointOnMap(i, j, (FieldType)map[i, j]); //Console.WriteLine(diamonds[diamondCounter].ToString() + " "); diamondCounter++; } } } if (diamondCounter < goals.Length) { throw new Exception("Impossible!"); } }
public void Append(PointOnMap nextStep) { route.Add(nextStep); }
public PointOnMap GetElementWest(PointOnMap current) { return(new PointOnMap(current.Row, current.Column - 1, (FieldType)map[current.Row, current.Column - 1])); }
public PointOnMap GetElementSouth(PointOnMap current) { return(new PointOnMap(current.Row + 1, current.Column, (FieldType)map[current.Row + 1, current.Column])); }
public Map executeRoute(Route routeToBeExecuted) { int[,] result = new int[map.GetLength(0), map.GetLength(1)]; Array.Copy(map, result, map.Length); PointOnMap[] newDiamonds = new PointOnMap[diamonds.Length]; if (diamonds.Length < goals.Length) { throw new Exception("What the f**k!"); } int i = 0; foreach (PointOnMap diamond in diamonds) { newDiamonds[i] = new PointOnMap(diamond.Row, diamond.Column, (FieldType)map[diamond.Row, diamond.Column]); i++; } Map newMap = new Map(result, this.goals, newDiamonds, robotPosition, wholeRoute.Append(routeToBeExecuted)); // Set new shift Position where the robot has to go in order to push the diamond int shiftPostionX = routeToBeExecuted.GetSecondLast().Row; int shiftPostionY = routeToBeExecuted.GetSecondLast().Column; PointOnMap shiftPosition = new PointOnMap(shiftPostionX, shiftPostionY, (FieldType)newMap.map[shiftPostionX, shiftPostionY]); // Set new robot Position where the robot will be after pushing forward the diamond int newRobotPositionX = routeToBeExecuted.GetLast().Row; int newRobotPositionY = routeToBeExecuted.GetLast().Column; // The robot is placed on position of diamond newMap.map[newRobotPositionX, newRobotPositionY] = (int)FieldType.Robot; PointOnMap newRobotPosition = new PointOnMap(newRobotPositionX, newRobotPositionY, (FieldType)newMap.map[newRobotPositionX, newRobotPositionY]); // Set old position of robot to walkable newMap.map[robotPosition.Row, robotPosition.Column] = (int)FieldType.Walkable; // if old robot-position was goal, set to goal instead of walkable foreach (PointOnMap goal in goals) { if (robotPosition == goal) { newMap.map[robotPosition.Row, robotPosition.Column] = (int)FieldType.Goal; } } // Diamond position has to be updated switch (PointOnMap.DirectionOfNeighbor(newMap, shiftPosition, newRobotPosition)) { case Direction.North: newMap.map[GetElementNorth(newRobotPosition).Row, GetElementNorth(newRobotPosition).Column] = (int)FieldType.Diamond; break; case Direction.East: newMap.map[GetElementEast(newRobotPosition).Row, GetElementEast(newRobotPosition).Column] = (int)FieldType.Diamond; break; case Direction.South: newMap.map[GetElementSouth(newRobotPosition).Row, GetElementSouth(newRobotPosition).Column] = (int)FieldType.Diamond; break; case Direction.West: newMap.map[GetElementWest(newRobotPosition).Row, GetElementWest(newRobotPosition).Column] = (int)FieldType.Diamond; break; default: Console.WriteLine("nicht gut!"); throw new Exception("Lost a diamond!"); break; } newMap.robotPosition = newRobotPosition; newMap.searchDiamonds(); // Update Diamondpositions! return(newMap); }
public List <Route> calculateUsefulRoutes(List <Tuple <PointOnMap, PointOnMap> > possibleShiftPoints, int[,] movementMap) { List <Route> allUsefulRoutes = new List <Route>(); // List of Tuples where the first Element specifies the // robot position and the second Element represents the diamond that can be moved foreach (Tuple <PointOnMap, PointOnMap> shiftPoint in possibleShiftPoints) { //Console.WriteLine(shiftPoint.Item1.Row + "|" + shiftPoint.Item1.Column); // Is it possible to move robot to several Shift Points? if (movementMap[shiftPoint.Item1.Row, shiftPoint.Item1.Column] != 0) { //Let's create a route from Robot to shift Position! The final position must be the diamond itself // The diamond must move in the right direction! // The route is equal to following the numbers backwards Route routeToShiftPoint = new Route(); routeToShiftPoint.Append(shiftPoint.Item2); routeToShiftPoint.Append(shiftPoint.Item1); int toRobotCounter = movementMap[shiftPoint.Item1.Row, shiftPoint.Item1.Column]; PointOnMap current = shiftPoint.Item1; while (toRobotCounter != 1) { toRobotCounter -= 1; if (movementMap[map.GetElementNorth(current).Row, map.GetElementNorth(current).Column] == toRobotCounter) { current = map.GetElementNorth(current); routeToShiftPoint.Append(current); continue; } if (movementMap[map.GetElementEast(current).Row, map.GetElementEast(current).Column] == toRobotCounter) { current = map.GetElementEast(current); routeToShiftPoint.Append(current); continue; } if (movementMap[map.GetElementSouth(current).Row, map.GetElementSouth(current).Column] == toRobotCounter) { current = map.GetElementSouth(current); routeToShiftPoint.Append(current); continue; } if (movementMap[map.GetElementWest(current).Row, map.GetElementWest(current).Column] == toRobotCounter) { current = map.GetElementWest(current); routeToShiftPoint.Append(current); continue; } } allUsefulRoutes.Add(routeToShiftPoint.Reverse()); } } return(allUsefulRoutes); }