码迷,mamicode.com
首页 > 其他好文 > 详细

迷宫 工厂模式练习 编译通过

时间:2016-01-20 12:51:18      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

/************************************************************************/
// 建立一个迷宫(Maze),迷宫的元素是墙、房间、门。它们都是MapSite的子类。
// 同时一个房间有四个方向,有可能是墙也有可能是门。                                                                     */
/************************************************************************/

#include "iostream"
using namespace std;

enum Direction
{
    North,
    East,
    South,
    West,
};

class MapSite
{
public:
    virtual void Enter() = 0;
};

class Room : public MapSite
{
public:
    //类给成员变量赋值的方法,初始化列表,更适用于成员变量的常量const型
    Room(int roomNO) : _roomNumber(roomNO){};

    MapSite *GetSide(Direction) const;

    virtual void SetSide(Direction direction, MapSite*)
    {
        cout << "Set the " << _roomNumber << "room" << direction << "side" <<endl;
    };

    int GetRoomNo()
    {
        return _roomNumber;
    };

    virtual void Enter()
    {
        cout << "Enter the Room" << endl;
    };

private:
    MapSite *_sides[4];
    int _roomNumber;
};

class Wall : public MapSite
{
public:
    Wall(){};
    
    virtual void Enter()
    {
        cout << "Enter the wall." <<endl;
    }
};

class Door : public MapSite
{
public :
    Door(Room* = 0, Room* = 0){};

    virtual void Enter()
    {
        cout << "Enter the door." <<endl;
    };

    Room *OtherSideFrom(Room*){};

private:
    Room *_room1;
    Room *_room2;
    bool _isOpen;
};

class Maze
{
public:
    Maze(){};

    void AddRoom(Room* room)
    {
        cout << "Add the room" << room->GetRoomNo() <<"."<<endl;
    };

    Room* RoomNo(int) const{};
};

class MazeGame
{
public:
    Maze *CreateMaze();
};

Maze* MazeGame::CreateMaze()
{
    Maze* aMaze = new Maze();
    Room *r1 = new Room(1);
    Room *r2 = new Room(2);
    Door *theDoor  = new Door(r1, r2);

    aMaze->AddRoom(r1);
    aMaze->AddRoom(r2);

    r1->SetSide(North, new Wall);
    r1->SetSide(East,theDoor);
    r1->SetSide(South, new Wall);
    r1->SetSide(West, new Wall);

    return aMaze;
}

int main()
{
    MazeGame mygame;
    mygame.CreateMaze();
    system("pause");
    return 0;
}

 

 

#include <iostream> 
using namespace std; 

enum Direction { 
    North, 
    East, 
    South, 
    West, 
}; 

class MapSite { 
public: 
    virtual void Enter() = 0; 
}; 

class Room : public MapSite { 
public: 
    Room(int roomNo):_roomNumber(roomNo) {}; 

    MapSite *GetSide(Direction) const; 

    virtual void SetSide(Direction direction, MapSite *) { 
        cout << "Set the " 
            << _roomNumber 
            << " room " 
            << direction 
            << " side." 
            << endl; 
    }; 

    int GetRoomNo() { 
        return _roomNumber; 
    }; 

    virtual void Enter() { 
        cout << "Enter the Room." << endl; 
    }; 

private: 
    MapSite *_sides[4]; 
    int _roomNumber; 
}; 

class Wall : public MapSite { 
public: 
    Wall() {}; 

    virtual void Enter() { 
        cout << "Enter the wall." << endl; 
    }; 

}; 

class Door : public MapSite { 
public: 
    Door(Room* = 0, Room* = 0) {}; 

    virtual void Enter() { 
        cout << "Enter the door." << endl; 
    }; 
    Room *OtherSideFrom(Room*) {}; 

private: 
    Room *_room1; 
    Room *_room2; 
    bool _isOpen; 
}; 

class Maze { 
public: 
    Maze() {}; 

    void AddRoom(Room* room) { 
        cout << "Add the room" << room->GetRoomNo() << "." << endl; 
    }; 
    Room* RoomNo(int) const{}; 
}; 

//以下为工厂模式不同地方
class MazeFactory { 
public: 
    MazeFactory() {}; 

    virtual Maze* MakeMaze() const { 
        return new Maze; 
    }; 

    virtual Wall* MakeWall() const { 
        return new Wall; 
    }; 

    virtual Room* MakeRoom(int n) const { 
        return new Room(n); 
    }; 

    virtual Door* MakeDoor(Room* r1, Room* r2) const { 
        return new Door(r1, r2); 
    }; 
}; 

class MazeGame { 
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(North, factory.MakeWall()); 
        r1->SetSide(East, theDoor); 
        r1->SetSide(South, factory.MakeWall()); 
        r1->SetSide(West, factory.MakeWall()); 

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

        return aMaze; 
    }; 
}; 

int main() { 
    MazeGame mygame; 
    MazeFactory factory; 
    mygame.CreateMaze(factory); 
    system("pause");
    return 0; 
}

 

迷宫 工厂模式练习 编译通过

标签:

原文地址:http://www.cnblogs.com/chenzhen1213/p/5144506.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!