public Map(EnMapData mapData, IEnumerable <EnPoint> ghosts, EnPoint tacman) { for (int j = 0; j < mapData.Rows.Length; j++) { var row = mapData.Rows[j]; { for (int i = 0; i < row.Length; i++) { var symbol = Char.ToUpper(row[i]); var isLast = symbol != WALL && i == row.Length - 1; switch (symbol) { case WALL: break; case SPACE: Add(i, j, Content.Empty, isLast); break; case COOKIE: Add(i, j, Content.Cookie, isLast); break; //case PACMAN: // Add(i, j, Content.Pacman); // break; //case GHOST: // Add(i, j, Content.Ghost); //break; default: break; } } } } FillNeighbours(); foreach (var ghost in ghosts) { _cells[new Point(ghost.Col, ghost.Row)].Content = Content.Ghost; } _cells[new Point(tacman.Col, tacman.Row)].Content = Content.Pacman; }
private static void TestGraph() { var dto = new DTO.EnMapData(); var path = Path.Combine(Directory.GetCurrentDirectory(), "Maps\\map5.txt"); dto.Rows = File.ReadAllLines(path); dto.Width = dto.Rows[0].Length; dto.Height = dto.Rows.Count(); //var ghosts = new EnPoint[] { new EnPoint() { Row = 3, Col = 5 } }; var ghosts = new EnPoint[] { }; var graph = new Graph.MapGraph(dto, new EnPoint() { Row = 1, Col = 10 }, ghosts); // Find some/any cookie var cookieLocation = graph.Vertices.FirstOrDefault(c => c.HasCookie == true); Console.WriteLine($"Cookie location: {cookieLocation.X}; {cookieLocation.Y}"); // Set tacman location to arbitrary location var tacmanLocation = graph.Vertices.FirstOrDefault(v => v.HasTacman); Console.WriteLine($"Tacman location: {tacmanLocation.X}; {tacmanLocation.Y}"); // Find shortest path between tacman and cookie var shortestPath = graph.GetShortestPath(tacmanLocation, cookieLocation); // Print graph PrintGraph(dto, graph); Console.WriteLine(""); Console.WriteLine("========================================="); Console.WriteLine(""); //Print with shortest path PrintGraph(dto, graph, shortestPath); Console.ReadKey(); }
public MapGraph(EnMapData rawMap, EnPoint tacmanPosition, EnPoint[] ghostPositions) { Vertices = new List <Vertex>(); Edges = new List <Edge>(); // Add all vertices for (var j = 0; j < rawMap.Width; j++) { for (var i = 0; i < rawMap.Height; i++) { var x = j; var y = i; var cell = rawMap.Rows[y][x]; switch (cell) { case '#': // Wall cells are not connected to anything break; case '.': case ' ': // Empty or cookie cells are connected to 4 adjesent empty or cookie cells var vertex = new Vertex(x, y, cell == '.', false, false); Vertices.Add(vertex); break; default: break; } } } foreach (var vertex in Vertices) { var vertexAbove = Vertices.FirstOrDefault(v => v.X == vertex.X && v.Y == vertex.Y + 1); var vertexBelow = Vertices.FirstOrDefault(v => v.X == vertex.X && v.Y == vertex.Y - 1); var vertexRight = Vertices.FirstOrDefault(v => v.Y == vertex.Y && v.X == vertex.X + 1); var vertexLeft = Vertices.FirstOrDefault(v => v.Y == vertex.Y && v.X == vertex.X - 1); if (vertexAbove != null) { var edge = new Edge(vertex, vertexAbove, Direction.TopBottom); vertex.Edges.Add(edge); Edges.Add(edge); } if (vertexBelow != null) { var edge = new Edge(vertex, vertexBelow, Direction.TopBottom); vertex.Edges.Add(edge); Edges.Add(edge); } if (vertexRight != null) { var edge = new Edge(vertex, vertexRight, Direction.LeftRight); vertex.Edges.Add(edge); Edges.Add(edge); } if (vertexLeft != null) { var edge = new Edge(vertex, vertexLeft, Direction.LeftRight); vertex.Edges.Add(edge); Edges.Add(edge); } } // Tacman Position var tacmanVertex = Vertices.FirstOrDefault(v => v.X == tacmanPosition.Col && v.Y == tacmanPosition.Row); tacmanVertex.HasTacman = true; // Ghost Positions foreach (var ghostPosition in ghostPositions) { var ghostVertex = Vertices.FirstOrDefault(v => v.X == ghostPosition.Col && v.Y == ghostPosition.Row); ghostVertex.HasGhost = true; } }
private Position GetNextPosition(EnMapData mapRaw, EnPoint ghostPosition, EnPoint tecmanPosition, EnPoint previousGhostPosition, EnPoint previousTecmanPosition, Map map) { var ghostOnMap = map.Cells.Where(c => c.Point.X == ghostPosition.Col && c.Point.Y == ghostPosition.Row).FirstOrDefault(); //first move if (previousGhostPosition.Col == ghostPosition.Col && previousGhostPosition.Row == ghostPosition.Row) { //Return any legal move var destinationCell = ghostOnMap.Neighbours.OrderBy(o => Guid.NewGuid()).FirstOrDefault(); return(new Position() { Col = destinationCell.Point.X, Row = destinationCell.Point.Y }); } var xDirction = ghostPosition.Col - previousGhostPosition.Col; var yDirection = ghostPosition.Row - previousGhostPosition.Row; //Changing X if (xDirction != 0) { var nextX = ghostPosition.Col + xDirction; var nextY = ghostPosition.Row; if (ghostOnMap.Neighbours.Any(c => c.Point.X == nextX && c.Point.Y == nextY)) { return(new Position() { Col = nextX, Row = nextY }); } else { var nextMove = ghostOnMap.Neighbours.OrderBy(o => Guid.NewGuid()).FirstOrDefault(); //TODO: this is not legit move sometimes return(new Position() { Col = nextMove.Point.X, Row = nextMove.Point.Y }); } } //Changing Y else { var nextX = ghostPosition.Col; var nextY = ghostPosition.Row + yDirection; if (ghostOnMap.Neighbours.Any(c => c.Point.X == nextX && c.Point.Y == nextY)) { return(new Position() { Col = nextX, Row = nextY }); } else { var nextMove = ghostOnMap.Neighbours.OrderBy(o => Guid.NewGuid()).FirstOrDefault(); return(new Position() { Col = nextMove.Point.X, Row = nextMove.Point.Y }); } } }
private static PointF ToPointF(EnPoint that) { return(new PointF((float)that.X, (float)that.Y)); }