static void PrintGraph(EnMapData mapDto, Graph.MapGraph graph, IList <Graph.Vertex> pathToDraw) { for (int i = 0; i < mapDto.Height; i++) { for (int j = 0; j < mapDto.Width; j++) { char symbol; var vertex = graph.Vertices.FirstOrDefault(v => v.X == j && v.Y == i); if (vertex == null) { symbol = mapDto.Rows[i][j]; } else { if (pathToDraw.Contains(vertex)) { symbol = 'x'; } else { symbol = vertex.GetVisualizingChnar(); } } Console.Write(symbol); } Console.WriteLine(); } }
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; }
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 }); } } }