/************************************************************************/ // 建立一个迷宫(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; }
时间: 2024-11-04 16:35:50