Beispiel #1
0
        public void GetAvailableNeighbours_ValidInputRight()
        {
            GameState gs   = getState();
            Maze      maze = gs.Maze;
            Vector2   pos  = new Vector2(1, 1);

            PacmanLibrary.Structure.Path path1 = new PacmanLibrary.Structure.Path(2, 1, new Pellet());
            PacmanLibrary.Structure.Path path2 = new PacmanLibrary.Structure.Path(1, 2, new Energizer());
            List <Tile> tiles1 = new List <Tile>();

            tiles1.Add(path2);
            tiles1.Add(path1);
            List <Tile> tiles2 = maze.GetAvailableNeighbours(pos, Direction.Right);

            bool expected = true;
            bool actual   = true;

            for (int i = 0; i < tiles1.Count; i++)
            {
                if (tiles1[i].GetType() != tiles2[i].GetType() || tiles1[i].Member?.GetType() != tiles2[i].Member?.GetType())
                {
                    actual = false;
                }
            }
            Assert.AreEqual(expected, actual);
        }
Beispiel #2
0
        public GameState getState()
        {
            String    content = File.ReadAllText(@"ghostAndPacman.csv");
            GameState gstate  = GameState.Parse(content);

            PacmanLibrary.Tile tile = new PacmanLibrary.Structure.Path(3, 3, null);
            gstate.Pen.AddTile(tile);
            return(gstate);
        }
Beispiel #3
0
        public Ghost getGhost()
        {
            GameState gs = getState();

            PacmanLibrary.Tile tile = new PacmanLibrary.Structure.Path(3, 3, null);
            gs.Pen.AddTile(tile);
            Vector2    pos    = new Vector2(3, 1);
            Vector2    target = new Vector2(2, 2);
            GhostState state  = GhostState.Chasing;

            PacmanLibrary.Enums.Color c = PacmanLibrary.Enums.Color.Red;
            Ghost ghost = new Ghost(gs, 3, 1, target, state, c);

            return(ghost);
        }
Beispiel #4
0
        /// <summary>
        /// The Parse method reads from a file and creates a GameState object.
        /// In the process of creation it instantiates all objects necessary to
        /// run the game and sets them inside the Maze. It also automatically makes
        /// the ScoreAndLives object subscribe to the related object events.
        /// </summary>
        /// <param name="file">The text file to read from</param>
        /// <returns></returns>
        public static GameState Parse(String content)
        {
            //string 2d array to hold the elements from the file text.
            string[,] map     = null;
            Tile[,] tileArray = null;

            //Setting GameState Object to hold the state of the game and its properties
            GameState g     = new GameState();
            Pen       pen   = new Pen();
            Maze      maze  = new Maze();
            GhostPack gpack = new GhostPack();

            //g.Pacman = new Pacman(GameState);
            g.Pen       = pen;
            g.Maze      = maze;
            g.GhostPack = gpack;

            ScoreAndLives score  = new ScoreAndLives(g);
            Pacman        pacman = new Pacman(g);
            Vector2       myPac  = new Vector2(1, 1);

            pacman.Position = myPac;

            g.Pacman = pacman;
            g.Score  = score;


            //read text from file
            try
            {
                string[] fileText = content.Split(new String[] { Environment.NewLine }, StringSplitOptions.None);
                int      mapSize  = fileText.Length;
                map       = new string[mapSize, mapSize];
                tileArray = new Tile[mapSize, mapSize];
                //assign elements to the map string array
                Char delimiter = ',';
                for (int i = 0; i < map.GetLength(0); i++)
                {
                    String[] elements = fileText[i].Split(delimiter);
                    for (int j = 0; j < map.GetLength(0); j++)
                    {
                        map[i, j] = elements[j];
                    }
                }
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("Could Not Find File");
            }

            //Iterate through the map 2d array that holds the elements from the file text.

            for (int y = 0; y < map.GetLength(0); y++)
            {
                for (int x = 0; x < map.GetLength(0); x++)
                {
                    switch (map[y, x])
                    {
                    case "w":
                        Wall           wall     = new Wall(x, y);
                        Structure.Path wallPath = new Structure.Path(x, y, null);
                        tileArray[x, y] = wall;
                        break;

                    case "p":
                        Pellet pellet = new Pellet();
                        pellet.CollisionEvent += g.Score.IncrementScore;
                        Structure.Path pelletMazePath = new Structure.Path(x, y, pellet);
                        tileArray[x, y] = pelletMazePath;
                        break;

                    case "e":
                        Energizer energizer = new Energizer();
                        energizer.CollisionEvent += g.Score.IncrementScore;
                        Structure.Path energizerPath = new Structure.Path(x, y, energizer);
                        tileArray[x, y] = energizerPath;
                        break;

                    case "m":
                        Structure.Path emptyMazePath = new Structure.Path(x, y, null);
                        tileArray[x, y] = emptyMazePath;
                        break;

                    case "x":
                        Structure.Path emptyPath = new Structure.Path(x, y, null);
                        tileArray[x, y] = emptyPath;
                        pen.AddTile(emptyPath);
                        break;

                    case "P":
                        Vector2 myPac2 = new Vector2(x, y);
                        tileArray[x, y] = new Structure.Path(x, y, null);
                        pacman.Position = myPac2;
                        Pacman.StartPos = myPac2;
                        break;

                    case "1":
                        Vector2 blinky_target = new Vector2(pacman.Position.X + 2, pacman.Position.Y);
                        Ghost   blinky        = new Ghost(g, x, y, blinky_target, GhostState.Chasing, Enums.Color.Red);
                        Ghost.ReleasePosition   = blinky.Position;
                        blinky.CollisionEvent  += score.IncrementScore;
                        blinky.PacmanDiedEvent += score.DeadPacman;
                        gpack.Add(blinky);
                        tileArray[x, y] = new Structure.Path(x, y, null);
                        break;

                    case "2":
                        Vector2 pinky_target = new Vector2(pacman.Position.X + 4, pacman.Position.Y);
                        Ghost   pinky        = new Ghost(g, x, y, pinky_target, GhostState.Chasing, Enums.Color.Pink);
                        pinky.CollisionEvent  += score.IncrementScore;
                        pinky.PacmanDiedEvent += score.DeadPacman;
                        gpack.Add(pinky);
                        Structure.Path aPath = new Structure.Path(x, y, null);
                        tileArray[x, y] = aPath;
                        pen.AddTile(aPath);
                        pen.AddToPen(pinky);
                        break;

                    case "3":
                        Vector2 inky_target = new Vector2(pacman.Position.X + 6, pacman.Position.Y);
                        Ghost   inky        = new Ghost(g, x, y, inky_target, GhostState.Chasing, Enums.Color.Yellow);
                        inky.CollisionEvent  += score.IncrementScore;
                        inky.PacmanDiedEvent += score.DeadPacman;
                        gpack.Add(inky);
                        Structure.Path aPath1 = new Structure.Path(x, y, null);
                        tileArray[x, y] = aPath1;
                        pen.AddTile(aPath1);
                        pen.AddToPen(inky);
                        break;

                    case "4":
                        Vector2 clyde_target = new Vector2(pacman.Position.X + 1, pacman.Position.Y);
                        Ghost   clyde        = new Ghost(g, x, y, clyde_target, GhostState.Chasing, Enums.Color.Green);
                        clyde.CollisionEvent  += score.IncrementScore;
                        clyde.PacmanDiedEvent += score.DeadPacman;
                        gpack.Add(clyde);
                        Structure.Path aPath2 = new Structure.Path(x, y, null);
                        tileArray[x, y] = aPath2;
                        pen.AddTile(aPath2);
                        pen.AddToPen(clyde);
                        break;
                    }
                }
            }
            //set the Tiles in the Maze object with the tileArray 2d array.
            g.Maze.SetTiles(tileArray);

            return(g);
        }
Beispiel #5
0
 public PacmanClassTest()
 {
     gs = getState();
     PacmanLibrary.Tile tile = new PacmanLibrary.Structure.Path(1, 3, null);
     gs.Pen.AddTile(tile);
 }