2014-10-29 6 views
0

텍스트 기반 어드벤처 게임을 만들려고합니다. 각 노드가 별개의 위치에 해당하고 각 방향의 다른 노드를 가리켜 야하는 노드 포인터 변수 (왼쪽, 앞으로 및 오른쪽)가있는 다른 노드로 맵을 나타내기를 원합니다. 연결 목록으로 구현하려고 시도했지만이 데이터 구조로 각 노드가 하나의 다른 노드를 가리킬 수있었습니다. 각 노드가 세 개의 다른 노드를 가리 키도록합니다. 어떤 데이터 구조를 구현할 때 사용할 수 있습니까?C++ 텍스트 기반 게임 - "맵"구현

+1

네 개의 노드 포인터가 왼쪽, 오른쪽, 앞으로 및 다음을 가리 키도록 데이터 구조를 수정하십시오. –

+1

데이터 구조를 구현하기 위해 이름이 필요하지 않습니다. 각 노드가 세 개의 다른 노드를 가리 키도록하십시오. 또는 당신의 캐릭터가 돌아갈 수 있기를 원한다면 4 개가 될 수도 있습니다. –

+0

각 노드에 포인터가 세 개인 경우 목록에 각 노드를 어떻게 추가합니까? 생성 할 때마다지도가 동일해야합니다. – GreatBambino

답변

1

는이 같은지도에 링크 된 위치와 사용자 정의 링크 된 자료 구조를 구현할 수 :

struct Map_Node{ 
    Map_Node *left; 
    Map_Node *right; 
    Map_Node *forward; 
    /* other needed field*/ 
}; 

그런 다음, 당신은 당신의 자신에 메모리 관리를 할 필요가있다. 예를 들어 스마트 포인터를 사용합니다.

std::shared_ptr<Map_Node> entry{ new MapNode }; 
std::shared_ptr<Map_Node> hallway{ new MapNode }; 
entry->forward = &*hallway; 
//and so on 

다음 파일을 가져 오는 것이 쉽지는 않지만 효율적이지는 않습니다. 각 위치 에 고유 ID가있는 경우 (예 : 문자열을 사용하면 이웃 필드의 ID를 저장하고 ID를 사용하여지도에서 자유롭게 이동할 수 있습니다.

struct Map_Node{ 
    std::string name; 
    std::string left; 
    std::string right; 
    std::string forward; 
    /* other needed field*/ 
}; 

std::map<std::string, Map_Node> map; 
Map_Node entry; 
entry.name = "entry"; 
map[entry.name] = entry; 

Map_Node hallway; 
hallway.name = "hallway"; 
map[hallway.name] = hallway; 

//links between: 
map["entry"].forward = "hallway"; 
+0

에서 이것에 만져 봤는데 그리고 난 거의 (N, S, E, W 사용) – Baldrickk

+0

@ Baldrickk N, S, E, W와 같은 단순한 방향으로 생각할 수있다. 간단한 2 차원 데이터 구조 (vector, array 또는 무엇이든)를 사용하는 것에 대해. – tgmath

+0

글쎄, 나는 왼쪽, 오른쪽, 대신에 N, S, E, W를 사용했다는 것을 의미했다. 벡터를 사용하는 것이 좋을 것이다. 예를 들어, 여러 층을 가질 수도 있는데,이 경우에 추가 링크가있을 수있다. 노드 (위아래). 이 경우에는'std :: vector >'를 저장하여 각 연결의 이름을 지정할 수 있습니다. – Baldrickk

2

링크 된 자료 구조는 당신이 원하는 일을 좋은 일을 할 것입니다 :

예 :

location* loc = new location("graveyard"); 
loc->description = "A spooky graveyard on a hill, a cool mist floats amongst the gravestones and monuments"; 
loc->add_link(crypt /*previously defined*/, 
       "An imposing mausoleum with an open door, steps inside lead down into darkness", 
       "Moonlight filters down from the top of some steps, a way out?"); 
loc.add_link(spooky_house /*previously defined*/, 
       "The North gate of the graveyard", 
       "The entrance to the house's spooky graveyard"); 
:

class location 
{ 
    std::string loc_name; 
    std::vector<std::pair<std::string,location*>> connections; 
    std::string description; 
public: 
    bool add_link(location* loc, std::string dicription_to, std::string dicription_from); 
    //other parameters + functions to manage class 
} 

이 사용하면 다음과 같은 위치를 만들 수있는 것

그래도 읽을 수있는지도 파일을 만드는 것을 권해드립니다. 아마도이 같은 템플릿을 사용하여 :

위치 파일 :

/*locations, format = "name; description"*/ 
Spooky House; house_description 
Crypt;  crypt_description 
Graveyard; A spooky graveyard on a hill, a cool mist floats amongst the gravestones and monuments 

링크 파일 :지도를로드하는 모든 위치에서 읽기와 벡터로 밀어처럼 간단 할 것

/*links, format = "index # (from); index # (to); description (from->to); description (to->from)"*/ 
3;2;An imposing mausoleum with an open door, steps inside lead down into darkness; Moonlight filters down from the top of some steps, a way out? 
3;1;The North gate of the graveyard;The entrance to the house's spooky graveyard; 

저장소에 연결 한 다음 링크를 추가하여 연결합니다.

+0

프로그램을 빌드하려고 할 때 'bool location :: add_link (location *)'이 private – GreatBambino

+0

클래스의 멤버가 기본적으로 비공개이므로 'public :'lable을 추가해야합니다. 그들에게 접근 가능하도록 만든다. ('add_link' 함수를 보여주기 위해 업데이트 된 대답) – Baldrickk