Exemple #1
0
 /// <summary>
 /// Checks for PacMan before moving ghost.
 /// </summary>
 /// <param name="count">Number of ghost in array.</param>
 /// <param name="direction">Direction of ghost movement.</param>
 /// <param name="ghostCoordX">Current X coordinate of ghost</param>
 /// <param name="ghostCoordY">Current Y coordinate of ghost</param>
 private static void CheckForPacManBeforeGhostMovement(int count, string direction, int ghostCoordX, int ghostCoordY)
 {
     if (pacMan.IsTherePacMan(ghostCoordX, ghostCoordY))
     {
         PacManGhostMeetingManager(count);
     }
     else
     {
         MoveCurrentGhost(count, direction, ghostCoordX, ghostCoordY);
     }
 }
Exemple #2
0
        static void MoveGhosts()
        {
            System.Media.SoundPlayer death = new System.Media.SoundPlayer(musicDeath);
            string[] colors = new string[] { "red", "blue", "green", "cyan" };
            Dictionary <String, int[]> izmestvane = new Dictionary <string, int[]>()
            {
                { "Down", new int[] { 0, -1 } },
                { "Up", new int[] { 0, 1 } },
                { "Right", new int[] { 1, 0 } },
                { "Left", new int[] { -1, 0 } },
                { "Stay", new int[] { 0, 0 } }
            };
            Dictionary <String, String> opposite = new Dictionary <string, string>()
            {
                { "Down", "Up" },
                { "Up", "Down" },
                { "Right", "Left" },
                { "Left", "Right" }
            };

            for (int i = 0; i < ghosts.Length; i++)
            {
                String   choice      = "Stay";
                int      ghostCoordX = ghosts[i].PosX;
                int      ghostCoordY = ghosts[i].PosY;
                String[] moves       = { "Stay" };
                switch (i)
                {
                case 0:
                    moves = Chase1(maze, ghosts[i].PosX, ghosts[i].PosY, pacMan.PosX, pacMan.PosY);
                    break;

                case 1:
                    moves = Chase2(maze, ghosts[i].PosX, ghosts[i].PosY, pacMan.PosX, pacMan.PosY);
                    break;

                case 2:
                    moves = Chase3(maze, ghosts[i].PosX, ghosts[i].PosY, pacMan.PosX, pacMan.PosY);
                    break;

                case 3:
                    moves = Chase4(maze, ghosts[i].PosX, ghosts[i].PosY, pacMan.PosX, pacMan.PosY);
                    break;

                default:
                    break;
                }
                for (int j = 0; j < 4; j++)
                {
                    int[] izm = izmestvane[moves[j]];
                    if ((maze.IsThereTunnel(ghosts[i].PosX + izm[0], ghosts[i].PosY + izm[1])) ||
                        maze.IsItPrison(ghosts[i].PosX + izm[0], ghosts[i].PosY + izm[1]) &&
                        (!(moves[j] == opposite[ghosts[i].Direction])) &&
                        (!AreThereGhosts(ghosts[i].PosX + izm[0], ghosts[i].PosY + izm[1])))
                    {
                        choice = moves[j];
                        break;
                    }
                }

                switch (choice)
                {
                case "Up":
                    if (maze.IsThereTunnel(ghostCoordX, ghostCoordY + 1) || maze.IsItPrison(ghostCoordX, ghostCoordY + 1))
                    {
                        if (pacMan.IsTherePacMan(ghostCoordX, ghostCoordY + 1))
                        {
                            if (lives > 0)
                            {
                                if (withCherry)
                                {
                                    ghosts[i].RemoveFromPreviousPosition();
                                    ghosts[i].PosX = 11;
                                    ghosts[i].PosY = 15;
                                    ghosts[i].DrawAtPosition();
                                }
                                else
                                {
                                    death.Play();
                                    lives--;
                                    Restart();
                                }
                            }
                            else
                            {
                                GameOverByGhost();
                            }
                        }
                        else
                        {
                            ghosts[i].Move(choice);

                            DrawTokenAfterGhost(ghostCoordX, ghostCoordY);
                        }
                    }

                    break;

                case "Down":
                    if (maze.IsThereTunnel(ghostCoordX, ghostCoordY - 1) || maze.IsItPrison(ghostCoordX, ghostCoordY - 1))
                    {
                        if (pacMan.IsTherePacMan(ghostCoordX, ghostCoordY - 1))
                        {
                            if (lives > 0)
                            {
                                if (withCherry)
                                {
                                    ghosts[i].RemoveFromPreviousPosition();
                                    ghosts[i].PosX = 11;
                                    ghosts[i].PosY = 15;
                                    ghosts[i].DrawAtPosition();
                                }
                                else
                                {
                                    death.Play();
                                    lives--;
                                    Restart();
                                }
                            }
                            else
                            {
                                GameOverByGhost();
                            }
                        }
                        else
                        {
                            ghosts[i].Move(choice);

                            DrawTokenAfterGhost(ghostCoordX, ghostCoordY);
                        }
                    }

                    break;

                case "Right":
                    if (maze.IsThereTunnel(ghostCoordX + 1, ghostCoordY) || maze.IsItPrison(ghostCoordX + 1, ghostCoordY))
                    {
                        if (pacMan.IsTherePacMan(ghostCoordX + 1, ghostCoordY))
                        {
                            if (lives > 0)
                            {
                                if (withCherry)
                                {
                                    ghosts[i].RemoveFromPreviousPosition();
                                    ghosts[i].PosX = 11;
                                    ghosts[i].PosY = 15;
                                    ghosts[i].DrawAtPosition();
                                }
                                else
                                {
                                    death.Play();
                                    lives--;
                                    Restart();
                                }
                            }
                            else
                            {
                                GameOverByGhost();
                            }
                        }
                        else
                        {
                            ghosts[i].Move(choice);

                            DrawTokenAfterGhost(ghostCoordX, ghostCoordY);
                        }
                    }

                    break;

                case "Left":
                    if (maze.IsThereTunnel(ghostCoordX - 1, ghostCoordY) || maze.IsItPrison(ghostCoordX - 1, ghostCoordY))
                    {
                        if (pacMan.IsTherePacMan(ghostCoordX - 1, ghostCoordY))
                        {
                            if (lives > 0)
                            {
                                if (withCherry)
                                {
                                    ghosts[i].RemoveFromPreviousPosition();
                                    ghosts[i].PosX = 11;
                                    ghosts[i].PosY = 15;
                                    ghosts[i].DrawAtPosition();
                                }
                                else
                                {
                                    death.Play();
                                    lives--;
                                    Restart();
                                }
                            }
                            else
                            {
                                GameOverByGhost();
                            }
                        }
                        else
                        {
                            ghosts[i].Move(choice);

                            DrawTokenAfterGhost(ghostCoordX, ghostCoordY);
                        }
                    }

                    break;

                case "Stay":
                    ghosts[i].Move(choice);
                    continue;

                default:
                    break;
                }
            }

            /*
             * for (int i = 0; i < ghosts.Length; i++)
             * {
             *  int[] positions = Chase2(maze.MatrixGenerator(), ghosts[i].XPos, ghosts[i].YPos, pacMan.PosX, pacMan.PosY);
             *  int best_possible = 0;
             *  int position = positions[best_possible];
             *  int row = ghosts[i].XPos;
             *  int col = ghosts[i].YPos;
             *  int[] izmestvane = new int[] { 0, 0 };
             *  while (true)
             *  {
             *      switch (position)
             *      {
             *          case 0:
             *              izmestvane = new int[] { 0, 1 };
             *              break;
             *          case 1:
             *              izmestvane = new int[] { 0, -1 };
             *              break;
             *          case 2:
             *              izmestvane = new int[] { 1, 0 };
             *              break;
             *          case 3:
             *              izmestvane = new int[] { -1, 0 };
             *              break;
             *          default:
             *              break;
             *      }
             *      int newrow = row + izmestvane[0];
             *     // if (newrow < 0)
             *     // {
             *        //  newrow = 0;
             *     // }
             *
             *      int newcol = col + izmestvane[1];
             *     // if (newcol < 0)
             *     // {
             *        //  newcol = 0;
             *     // }
             *
             *      if ((maze.MatrixGenerator()[newrow, newcol] != "│") &&
             *              (maze.MatrixGenerator()[newrow, newcol] != "─") &&
             *              (maze.MatrixGenerator()[newrow, newcol] != "┌") &&
             *              (maze.MatrixGenerator()[newrow, newcol] != "┐") &&
             *              (maze.MatrixGenerator()[newrow, newcol] != "└") &&
             *              (maze.MatrixGenerator()[newrow, newcol] != "┘"))
             *      {
             *          if (maze.MatrixGenerator()[newrow, newcol] == "*")
             *          {
             *              maze.MatrixGenerator()[newrow, newcol] = "☻";
             *              maze.MatrixGenerator()[row, col] = "*";
             *              ghosts[i].XPos = newrow;
             *              ghosts[i].YPos = newcol;
             *          }
             *          else if (maze[newrow, newcol] == "☺")
             *          {
             *              maze.FieldGenerator();
             *              Console.WriteLine("Game over!");
             *              Environment.Exit(0);
             *          }
             *          else
             *          {
             *              maze.MatrixGenerator()[newrow, newcol] = "☻";
             *              maze.MatrixGenerator()[row, col] = " ";
             *              ghosts[i].XPos = newrow;
             *              ghosts[i].YPos = newcol;
             *          }
             *          break;
             *      }
             *      else
             *      {
             *          best_possible++;
             *        //  if (best_possible > 3)
             *        //  {
             *         //     best_possible = 3;
             *        //  }
             *          position = positions[best_possible];
             *      }
             *  }
             * }
             * */
        }
Exemple #3
0
        static void MoveGhosts()
        {
            string[] directions = { "Down", "Up", "Right", "Left" };

            for (int i = 0; i < ghosts.Length; i++)
            {
                int choice      = 0;
                int ghostCoordX = ghosts[i].PosX;
                int ghostCoordY = ghosts[i].PosY;

                choice = rand.Next(4);

                switch (choice)
                {
                case 0:
                    if (maze.IsThereTunnel(ghostCoordX, ghostCoordY + 1) || maze.IsItPrison(ghostCoordX, ghostCoordY + 1))
                    {
                        if (pacMan.IsTherePacMan(ghostCoordX, ghostCoordY + 1))
                        {
                            if (lives > 0)
                            {
                                lives--;
                                Restart();
                            }
                            else
                            {
                                GameOverByGhost();
                            }
                        }
                        else
                        {
                            ghosts[i].Move(directions[choice]);

                            DrawTokenAfterGhost(ghostCoordX, ghostCoordY);
                        }
                    }

                    break;

                case 1:
                    if (maze.IsThereTunnel(ghostCoordX, ghostCoordY - 1) || maze.IsItPrison(ghostCoordX, ghostCoordY - 1))
                    {
                        if (pacMan.IsTherePacMan(ghostCoordX, ghostCoordY - 1))
                        {
                            if (lives > 0)
                            {
                                lives--;
                                Restart();
                            }
                            else
                            {
                                GameOverByGhost();
                            }
                        }
                        else
                        {
                            ghosts[i].Move(directions[choice]);

                            DrawTokenAfterGhost(ghostCoordX, ghostCoordY);
                        }
                    }

                    break;

                case 2:
                    if (maze.IsThereTunnel(ghostCoordX + 1, ghostCoordY) || maze.IsItPrison(ghostCoordX + 1, ghostCoordY))
                    {
                        if (pacMan.IsTherePacMan(ghostCoordX + 1, ghostCoordY))
                        {
                            if (lives > 0)
                            {
                                lives--;
                                Restart();
                            }
                            else
                            {
                                GameOverByGhost();
                            }
                        }
                        else
                        {
                            ghosts[i].Move(directions[choice]);

                            DrawTokenAfterGhost(ghostCoordX, ghostCoordY);
                        }
                    }

                    break;

                case 3:
                    if (maze.IsThereTunnel(ghostCoordX - 1, ghostCoordY) || maze.IsItPrison(ghostCoordX - 1, ghostCoordY))
                    {
                        if (pacMan.IsTherePacMan(ghostCoordX - 1, ghostCoordY))
                        {
                            if (lives > 0)
                            {
                                lives--;
                                Restart();
                            }
                            else
                            {
                                GameOverByGhost();
                            }
                        }
                        else
                        {
                            ghosts[i].Move(directions[choice]);

                            DrawTokenAfterGhost(ghostCoordX, ghostCoordY);
                        }
                    }

                    break;

                default:
                    break;
                }
            }

            /*
             * for (int i = 0; i < ghosts.Length; i++)
             * {
             *  int[] positions = Chase2(maze.MatrixGenerator(), ghosts[i].XPos, ghosts[i].YPos, pacMan.PosX, pacMan.PosY);
             *  int best_possible = 0;
             *  int position = positions[best_possible];
             *  int row = ghosts[i].XPos;
             *  int col = ghosts[i].YPos;
             *  int[] izmestvane = new int[] { 0, 0 };
             *  while (true)
             *  {
             *      switch (position)
             *      {
             *          case 0:
             *              izmestvane = new int[] { 0, 1 };
             *              break;
             *          case 1:
             *              izmestvane = new int[] { 0, -1 };
             *              break;
             *          case 2:
             *              izmestvane = new int[] { 1, 0 };
             *              break;
             *          case 3:
             *              izmestvane = new int[] { -1, 0 };
             *              break;
             *          default:
             *              break;
             *      }
             *      int newrow = row + izmestvane[0];
             *     // if (newrow < 0)
             *     // {
             *        //  newrow = 0;
             *     // }
             *
             *      int newcol = col + izmestvane[1];
             *     // if (newcol < 0)
             *     // {
             *        //  newcol = 0;
             *     // }
             *
             *      if ((maze.MatrixGenerator()[newrow, newcol] != "│") &&
             *              (maze.MatrixGenerator()[newrow, newcol] != "─") &&
             *              (maze.MatrixGenerator()[newrow, newcol] != "┌") &&
             *              (maze.MatrixGenerator()[newrow, newcol] != "┐") &&
             *              (maze.MatrixGenerator()[newrow, newcol] != "└") &&
             *              (maze.MatrixGenerator()[newrow, newcol] != "┘"))
             *      {
             *          if (maze.MatrixGenerator()[newrow, newcol] == "*")
             *          {
             *              maze.MatrixGenerator()[newrow, newcol] = "☻";
             *              maze.MatrixGenerator()[row, col] = "*";
             *              ghosts[i].XPos = newrow;
             *              ghosts[i].YPos = newcol;
             *          }
             *          else if (maze[newrow, newcol] == "☺")
             *          {
             *              maze.FieldGenerator();
             *              Console.WriteLine("Game over!");
             *              Environment.Exit(0);
             *          }
             *          else
             *          {
             *              maze.MatrixGenerator()[newrow, newcol] = "☻";
             *              maze.MatrixGenerator()[row, col] = " ";
             *              ghosts[i].XPos = newrow;
             *              ghosts[i].YPos = newcol;
             *          }
             *          break;
             *      }
             *      else
             *      {
             *          best_possible++;
             *        //  if (best_possible > 3)
             *        //  {
             *         //     best_possible = 3;
             *        //  }
             *          position = positions[best_possible];
             *      }
             *  }
             * }
             * */
        }