我正在尝试创建一个游戏,图形表示为角色。我正在上一个提供地图的课程。我正处于程序成功随机放置随机数量的房间的地步,现在,我需要制作将这些房间连接在一起的走廊。
请记住,我是新手,如果我的代码看起来很愚蠢,请记住这是我能用我所拥有的知识做的全部:)。
基本上在这一点上,我不知道如何解决通过走廊一起创造房间的问题,我真的可以使用一个想法。
这是我写的代码。
注意:randInt(int参数)只是我在其他地方使用的函数,它返回 介于0和参数-1之间的数字。
Dungeon.h文件--------------------
#ifndef DUNGEON_H
#define DUNGEON_H
#include <string>
#include <iostream>
#include <vector>
#include "utilities.h"
using namespace std;
struct Room
{
int leftUpPosition_row;
int leftUpPosition_col;
int length;
int heigth;
};
class Dungeon
{
public:
Dungeon();
~Dungeon();
void display(string msg) const;
void testDisplay(int whichOne) const;
private:
vector<Room> m_rooms;
int m_numOfRooms;
};
#endif
Dungeon.cpp文件--------------------
#include "Dungeon.h"
Dungeon::Dungeon()
{
m_numOfRooms=4+randInt(3);
//Note that randInt(3) is just a function I use somewhere else and it returns
//a number between 0 and argument-1.
cout<<"Max rooms is: "<<m_numOfRooms<<endl;
bool stop=false;
int roomsCreated=0;
for(int i=0;i<m_numOfRooms && stop==false ;i++)
{
m_rooms.push_back(Room());
m_rooms[i].heigth=3+randInt(6);
m_rooms[i].length=6+randInt(13);
//Creating the first room
if(i==0)
{
m_rooms[i].leftUpPosition_row=1+randInt(16-m_rooms[i].heigth);
m_rooms[i].leftUpPosition_col=1+randInt(68-m_rooms[i].length);
roomsCreated++;
}
else //Creating the rooms that are not the first.
{
int attemptingToCreateRoom=0;
bool goodPos=false;
bool goon=true;
////////////////////////////////////////////////////////////////////////////////////////////////////////
while(goodPos==false)
{
goon=true;
m_rooms[i].leftUpPosition_row=1+randInt(16-m_rooms[i].heigth);
m_rooms[i].leftUpPosition_col=1+randInt(68-m_rooms[i].length);
for(int k=0;k<roomsCreated && goon==true;) //Compares with created rooms to make sure that they do not overlap
{
if( (m_rooms[i].leftUpPosition_row + m_rooms[i].heigth) < (m_rooms[k].leftUpPosition_row) || (m_rooms[i].leftUpPosition_row) > (m_rooms[k].leftUpPosition_row+ m_rooms[k].heigth) || (m_rooms[i].leftUpPosition_col + m_rooms[i].length) < (m_rooms[k].leftUpPosition_col) || (m_rooms[i].leftUpPosition_col) > (m_rooms[k].leftUpPosition_col+ m_rooms[k].length))
{
k++;
}
else
{
goon=false;
}
}
if(goon ==true)
{
roomsCreated++;
goodPos=true;
}
else
{
attemptingToCreateRoom++;
if(attemptingToCreateRoom ==100000)
{
m_rooms.pop_back();
roomsCreated--;
i--;
stop=true;
break;
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
}
cout<<"Rooms Created So Far: "<<roomsCreated<<endl;
}
m_numOfRooms=roomsCreated;
}
void Dungeon::display(string msg) const
{
char grid[17][69];
int r,c;
for (r = 0; r < 17; r++)
for (c = 0; c < 69; c++)
{
grid[r][c]='#';
}
for(int i=0;i<m_numOfRooms;i++)
{
for (r =m_rooms[i].leftUpPosition_row ; r < m_rooms[i].leftUpPosition_row+m_rooms[i].heigth; r++)
for (c = m_rooms[i].leftUpPosition_col; c < m_rooms[i].leftUpPosition_col+m_rooms[i].length; c++)
{
grid[r][c]=' ';
}
}
//////////////////////////////////////////////////
for(int i=0;i<m_numOfRooms;i++)
{
r =m_rooms[i].leftUpPosition_row;
c = m_rooms[i].leftUpPosition_col;
grid[r][c]=(char) i+49;
}
/////////////////////////////////////////////////
for (r = 0; r < 17; r++)
{
for (c = 0; c < 69; c++)
cout << grid[r][c];
cout << endl;
}
cout << endl;
}
以下是我的输出现在的样子
以下是与走廊完成的游戏将如何显示