Exemplo n.º 1
0
        static void Main(string[] args)
        {
            // Ordinary maze, not particularly interesting.
            MazeFactory factory = new MazeFactory();

            MazeGame.CreateMaze(factory);

            MazeFactory enchangedMazeFactory = new EnchangedMazeFactory();
            var         enchantedMaze        = MazeGame.CreateMaze(enchangedMazeFactory);

            if (enchantedMaze.RoomNo(1) is EnchantedRoom)
            {
                Console.WriteLine("The second maze is enchanted, factory did its work.");
            }
            else
            {
                Console.WriteLine("Something wrong with the Absctract factory, the maze is still ordinary.");
            }

            var oneMoreEnchantedMaze = new EnchantedMazeGame().CreateMaze();

            if (oneMoreEnchantedMaze.RoomNo(1) is EnchantedRoom)
            {
                Console.WriteLine("The third maze is enchanted, overriden factory methods did their work.");
            }
            else
            {
                Console.WriteLine("Something wrong with the factory methods, the maze is still ordinary.");
            }
        }
Exemplo n.º 2
0
        static public MazeFactory GetFactroy()
        {
            if (_mazeFactory == null)
            {
                _mazeFactory = new MazeFactory();
            }

            return(_mazeFactory);
        }
Exemplo n.º 3
0
        private void GenRandomMaze()
        {
            factory = new MazeFactory(GridHeight, GridWidth);
            mazeGenerationComplete = ThreadComplete;
            factory.MazeComplete  += MazeComplete;
            ThreadStart threadStart = new ThreadStart(factory.GenerateMaze);

            thread = new Thread(threadStart);
            thread.Start();
        }
Exemplo n.º 4
0
        // Non-flexible implemantation, replaced.
        //public static Maze CreateMaze()
        //{
        //    Maze maze = new Maze();
        //    Room room1 = new Room(1);
        //    Room room2 = new Room(2);
        //    Door door = new Door(room1, room2);

        //    maze.AddRoom(room1);
        //    maze.AddRoom(room2);

        //    room1.SetSide(Direction.North, new Wall());
        //    room1.SetSide(Direction.East, door);
        //    room1.SetSide(Direction.South, new Wall());
        //    room1.SetSide(Direction.West, new Wall());

        //    room2.SetSide(Direction.North, new Wall());
        //    room2.SetSide(Direction.East, new Wall());
        //    room2.SetSide(Direction.South, new Wall());
        //    room2.SetSide(Direction.West, door);

        //    return maze;
        //}

        public static Maze CreateMaze(MazeFactory factory)
        {
            Maze maze  = factory.MakeMaze();
            Room room1 = factory.MakeRoom(1);
            Room room2 = factory.MakeRoom(2);
            Door door  = factory.MakeDoor(room1, room2);

            maze.AddRoom(room1);
            maze.AddRoom(room2);

            ConnectFromEastToWest(room1, room2, door);

            return(maze);
        }
Exemplo n.º 5
0
        // 使用工厂来初始化room和外墙
        public void InitialRooms(MazeFactory factory)
        {
            for (int x = 0; x < _gameSizeH; x++)
            {
                for (int y = 0; y < _gameSizeW; y++)
                {
                    Room newRoom = factory.MakeRoom(new Location(x, y));

                    if (x == 0)
                    {
                        new Wall(newRoom, Direction.North);
                    }
                    else
                    {
                        Room northRoom = GetRoom(x - 1, y);
                        northRoom.SetSite(Direction.South, newRoom);
                        newRoom.SetSite(Direction.North, northRoom);
                        if (x == _gameSizeH - 1)
                        {
                            new Wall(newRoom, Direction.South);
                        }
                    }

                    if (y == 0)
                    {
                        new Wall(newRoom, Direction.West);
                    }
                    else
                    {
                        Room westRoom = GetRoom(x, y - 1);
                        westRoom.SetSite(Direction.East, newRoom);
                        newRoom.SetSite(Direction.West, westRoom);
                        if (y == _gameSizeW - 1)
                        {
                            new Wall(newRoom, Direction.East);
                        }
                    }

                    _rooms[x * _gameSizeW + y] = newRoom;
                }
            }
        }
Exemplo n.º 6
0
        public Maze CreateMaze(MazeFactory factory)
        {
            Maze aMaze = factory.MakeMaze();
            Room r1 = factory.MakeRoom(1);
            Room r2 = factory.MakeRoom(2);
            Door theDoor = factory.MakeDoor(r1,r2);

            aMaze.AddRoom(r1);
            aMaze.AddRoom(r2);

            r1.SetSide(Direction.North, factory.MakeWall());
            r1.SetSide(Direction.East, theDoor);
            r1.SetSide(Direction.South, factory.MakeWall());
            r1.SetSide(Direction.West, factory.MakeWall());

            r2.SetSide(Direction.North, factory.MakeWall());
            r2.SetSide(Direction.East, factory.MakeWall());
            r2.SetSide(Direction.South, factory.MakeWall());
            r2.SetSide(Direction.West, theDoor);

            return aMaze;
        }
Exemplo n.º 7
0
        public Maze CreateMaze(MazeFactory factory)
        {
            Maze aMaze   = factory.MakeMaze();
            Room r1      = factory.MakeRoom(1);
            Room r2      = factory.MakeRoom(2);
            Door theDoor = factory.MakeDoor(r1, r2);

            aMaze.AddRoom(r1);
            aMaze.AddRoom(r2);

            r1.SetSide(Direction.North, factory.MakeWall());
            r1.SetSide(Direction.East, theDoor);
            r1.SetSide(Direction.South, factory.MakeWall());
            r1.SetSide(Direction.West, factory.MakeWall());

            r2.SetSide(Direction.North, factory.MakeWall());
            r2.SetSide(Direction.East, factory.MakeWall());
            r2.SetSide(Direction.South, factory.MakeWall());
            r2.SetSide(Direction.West, theDoor);

            return(aMaze);
        }
Exemplo n.º 8
0
        public override void GenWalls(MazeFactory factory)
        {
            // generate all walls
            for (int x = 0; x < _gameSizeH; x++)
            {
                for (int y = 0; y < _gameSizeW; y++)
                {
                    if (x < _gameSizeH - 1)
                    {
                        new Wall(GetRoom(x, y), GetRoom(x + 1, y));
                    }
                    if (y < _gameSizeW - 1)
                    {
                        new Wall(GetRoom(x, y), GetRoom(x, y + 1));
                    }
                }
            }

            Random rand = new Random();

            int[]           visited = new int[_gameSizeW * _gameSizeH];   // 0:no visted, 1:visited, 2:joined
            List <Location> path    = new List <Location>();

            path.Add(new Location(0, 0));

            while (path.Count != 0)
            {
                Location c = path[rand.Next(path.Count)];
                visited[c.X * _gameSizeW + c.Y] = 1;
                path.Remove(c);
                List <Direction> check = new List <Direction>();

                if (c.Y > 0)
                {
                    if (visited[c.X * _gameSizeW + c.Y - 1] == 1)
                    {
                        check.Add(Direction.West);
                    }
                    else if (visited[c.X * _gameSizeW + c.Y - 1] == 0)
                    {
                        path.Add(new Location(c.X, c.Y - 1));
                        visited[c.X * _gameSizeW + c.Y - 1] = 2;
                    }
                }
                if (c.X > 0)
                {
                    if (visited[(c.X - 1) * _gameSizeW + c.Y] == 1)
                    {
                        check.Add(Direction.North);
                    }
                    else if (visited[(c.X - 1) * _gameSizeW + c.Y] == 0)
                    {
                        path.Add(new Location(c.X - 1, c.Y));
                        visited[(c.X - 1) * _gameSizeW + c.Y] = 2;
                    }
                }
                if (c.Y < _gameSizeW - 1)
                {
                    if (visited[c.X * _gameSizeW + c.Y + 1] == 1)
                    {
                        check.Add(Direction.East);
                    }
                    else if (visited[c.X * _gameSizeW + c.Y + 1] == 0)
                    {
                        path.Add(new Location(c.X, c.Y + 1));
                        visited[c.X * _gameSizeW + c.Y + 1] = 2;
                    }
                }
                if (c.X < _gameSizeH - 1)
                {
                    if (visited[(c.X + 1) * _gameSizeW + c.Y] == 1)
                    {
                        check.Add(Direction.South);
                    }
                    else if (visited[(c.X + 1) * _gameSizeW + c.Y] == 0)
                    {
                        path.Add(new Location(c.X + 1, c.Y));
                        visited[(c.X + 1) * _gameSizeW + c.Y] = 2;
                    }
                }

                if (check.Count > 0)
                {
                    Direction d = check[rand.Next(check.Count)];
                    switch (d)
                    {
                    case Direction.North:
                        Room.Join(GetRoom(c), GetRoom(c.X - 1, c.Y));
                        break;

                    case Direction.South:
                        Room.Join(GetRoom(c), GetRoom(c.X + 1, c.Y));
                        break;

                    case Direction.East:
                        Room.Join(GetRoom(c), GetRoom(c.X, c.Y + 1));
                        break;

                    case Direction.West:
                        Room.Join(GetRoom(c), GetRoom(c.X, c.Y - 1));
                        break;

                    default:
                        break;
                    }
                }
            }
        }
Exemplo n.º 9
0
 // 生成墙
 public virtual void GenWalls(MazeFactory factory)
 {
     GenSimpleTestWalls();
 }
Exemplo n.º 10
0
        public void CreateMaze(MazeFactory factory, MazeType mazeType = MazeType.Prime)
        {
            _maze = factory.MakeMaze(_gameSizeH, _gameSizeW, mazeType);

            SetGameParameters();
        }