Ejemplo n.º 1
0
 public Ghost(Location location, ConsoleColor color, IMovingStrategy strategy)
     : base(location, '&', color)
 {
     this.strategy = strategy;
     this.route = new List<Location>();
     this.enumerator = this.route.GetEnumerator();
 }
Ejemplo n.º 2
0
        public IEnumerable<Location> GetRoute(Location source, Location destination, MazeObject[,] maze)
        {
            int rows = maze.GetLength(0);
            int cols = maze.GetLength(1);

            bool[,] visited = new bool[rows, cols];

            Queue<SimpleTreeNode<Location>> treeNodesQueue = new Queue<SimpleTreeNode<Location>>();

            SimpleTreeNode<Location> root = new SimpleTreeNode<Location>(source);

            treeNodesQueue.Enqueue(root);
            visited[source.Row, source.Col] = true;

            while (treeNodesQueue.Count > 0)
            {
                SimpleTreeNode<Location> node = treeNodesQueue.Dequeue();

                if (node.Value == destination)
                {
                    // traverse the tree bottom-up to get the shortest path
                    IEnumerable<Location> route = GetAllPredecessors(node);
                    return route;
                }

                SimpleTreeNode<Location>[] childNodes = new SimpleTreeNode<Location>[]
                {
                    new SimpleTreeNode<Location>(new Location(node.Value.Row, node.Value.Col - 1)), // left
                    new SimpleTreeNode<Location>(new Location(node.Value.Row, node.Value.Col + 1)), // right
                    new SimpleTreeNode<Location>(new Location(node.Value.Row - 1, node.Value.Col)), // up
                    new SimpleTreeNode<Location>(new Location(node.Value.Row + 1, node.Value.Col)) // down
                };

                node.Children.AddRange(childNodes);

                foreach (SimpleTreeNode<Location> child in childNodes)
                {
                    if (child.Value.Row >= 0 && child.Value.Row < rows
                        && child.Value.Col >= 0 && child.Value.Col < cols
                        && !(maze[child.Value.Row, child.Value.Col].Type == MazeObjectType.Brick)
                        && !visited[child.Value.Row, child.Value.Col])
                    {
                        node.Children.Add(child);
                        treeNodesQueue.Enqueue(child);
                        visited[child.Value.Row, child.Value.Col] = true;
                    }
                }
            }

            return new List<Location>();
        }
Ejemplo n.º 3
0
 public void CalcRoute(Location destination, MazeObject[,] maze)
 {
     this.route = this.strategy.GetRoute(this.Location, destination, maze);
     this.enumerator = this.route.GetEnumerator();
 }
Ejemplo n.º 4
0
 protected Actor(Location location, char character, ConsoleColor color)
 {
     this.location = location;
     this.character = character;
     this.color = color;
 }
Ejemplo n.º 5
0
 private void inputDevice_DownArrowKeyPressed(object sender, EventArgs e)
 {
     Location down = new Location(1, 0);
     if (CanMovePacman(down))
     {
         MakeCellEmpty(pacman.Location.Row, pacman.Location.Col);
         pacman.UpdateLocation(down);
         Draw(pacman);
         UpdatePacmanScore();
     }
 }
Ejemplo n.º 6
0
 private void inputDevice_RightArrowKeyPressed(object sender, EventArgs e)
 {
     Location toTheRight = new Location(0, 1);
     if (CanMovePacman(toTheRight))
     {
         MakeCellEmpty(pacman.Location.Row, pacman.Location.Col);
         pacman.UpdateLocation(toTheRight);
         Draw(pacman);
         UpdatePacmanScore();
     }
     else
     {
         TryTeleportPacman(toTheRight);
     }
 }
Ejemplo n.º 7
0
        private bool TryTeleportPacman(Location direction)
        {
            Location nextLocation = pacman.Location + direction;

            if (nextLocation.Row == 10 || nextLocation.Row == 12)
            {
                if (nextLocation.Col == MAZE_COLS)
                {
                    MakeCellEmpty(pacman.Location.Row, pacman.Location.Col);
                    pacman.Location = new Location(nextLocation.Row, 0);
                    Draw(pacman);
                    return true;
                }
                else if (nextLocation.Col == -1)
                {
                    MakeCellEmpty(pacman.Location.Row, pacman.Location.Col);
                    pacman.Location = new Location(nextLocation.Row, MAZE_COLS - 1);
                    Draw(pacman);
                    return true;
                }
            }

            return false;
        }
Ejemplo n.º 8
0
        private bool CanMovePacman(Location direction)
        {
            Location nextLocation = pacman.Location + direction;

            if (nextLocation.Row < 0 || nextLocation.Row >= MAZE_ROWS ||
                nextLocation.Col < 0 || nextLocation.Col >= MAZE_COLS)
            {
                return false;
            }

            if (maze[nextLocation.Row, nextLocation.Col].Type == MazeObjectType.Brick)
            {
                return false;
            }
            return true;
        }