그래서 나는 Hunt the Wumpus을 C++로 작성하고 있습니다. 유일한 진짜 차이점은 12 면체 형태의 동굴에 대해 걱정하지 않는다는 것입니다.Wuntus 사냥 - Room Connection
지금까지 나는 동굴 창조와 영웅, 박쥐, 우렁찬, 구덩이의 무작위 삽입을 구현했습니다.
// Hunt the Wumpus
#include "std_lib_facilities.h"
#include "time.h"
class Room{
bool is_occupied;
bool has_wumpus;
bool has_bat;
bool has_pit;
public:
Room() // default constructor
{
is_occupied = false;
has_wumpus = false;
has_bat = false;
has_pit = false;
}
void random_insert(vector<Room>& v);
};
void Room::random_insert(vector<Room>& v)
{
srand(time(NULL));
int random_room = 0;
bool crowded = true;
random_room = rand() % 20; // insert hero
v[random_room].is_occupied = true;
while(crowded) // insert...THE WUMPUS!
{
random_room = rand() % 20;
if(v[random_room].is_occupied) ;
else if(!v[random_room].is_occupied)
{
v[random_room].has_wumpus = true;
crowded = false;
}
}
crowded = true;
while(crowded) // insert bat
{
random_room = rand() % 20;
if(v[random_room].is_occupied || v[random_room].has_wumpus) ;
else if(!v[random_room].is_occupied && !v[random_room].has_wumpus)
{
v[random_room].has_bat = true;
crowded = false;
}
}
crowded = true;
while(crowded) // insert pit
{
random_room = rand() % 20;
if(v[random_room].is_occupied || v[random_room].has_wumpus || v[random_room].has_bat) ;
else if(!v[random_room].is_occupied && !v[random_room].has_wumpus && !v[random_room].has_bat)
{
v[random_room].has_pit = true;
crowded = false;
}
}
}
vector<Room> create(Room& r)
{
vector<Room> c;
for(int i = 0; i < 20; ++i)
c.push_back(r);
return c;
}
int main()
{
Room r;
vector<Room> cave = create(r); // create cave
r.random_insert(cave); // randomly insert things
}
나는
그러나, I 등을 해요, 나는 박쥐는 현재의 상황을 출력, 촬영, 임의의 장소에 영웅을 삭제하여 다른 모든 것들을 구현하는거야 방법의 좋은 생각이있어 동굴에서 객실을 무작위로 연결하는 방법을 잘 모릅니다. 나는 벡터에서 일종의 무작위 정렬을 수행하고 방을 포인터로 왼쪽과 오른쪽에 연결하는 것을 고려해 보았습니다.하지만 그것은 동굴이 아닌 긴 복도 일뿐입니다. 어쩌면 좌표계를 만들 수 있을까요? 누구든지 추천 사항이 있습니까? 감사.
질문과 관련이 없지만 생성자에 대한 초기화 프로그램 목록을 사용하는 것이 더 빠르고 안전합니다. – jkeys
나는 속도에 대해 정말로 염려하지 않는다. 나는 이미 그것을 끝냈지 만, 어쨌든 그것을 바꿀 것이다. – trikker