Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am creating a roguelike game and Having trouble creating the player and making sure it prints in the empty spaces of a random dungeon.

I am creating a roguelike game and Having trouble creating the player and making sure it prints in the empty spaces of a random dungeon. I also am having trouble with the player symbol '@' duplicating and trailing behind it. what changes to my code do i need to make so that I can fix these issues
//grid and display header
struct room
{
int Row, Cols, Old_centerR, Old_centerC, centerR, centerC, length, height, symbol;
};
class Player;
class Temple
{
public:
// Constructor/destructor
Temple(int nRows, int nCols);
~Temple();
// Accessors
int rows() const;
int cols() const;
Player* player() const;
bool isPlayerAt(int r, int c) const;
void displayPlayer(Player* player);
bool determineNewPosition(int& r, int& c, int dir) const;
void display();
bool isInBounds(int r, int c) const;
bool addPlayer(int r, int c);
bool WallsAreAt(int r, int c);
private:
std::vectorrooms;
char grid[MAXROWS][MAXCOLS];
int num_ofRooms;
int m_rows;
int m_cols;
Player* m_player;
};
//grid and display cpp
Temple::Temple(int rows, int cols):m_rows(MAXROWS),m_cols(MAXCOLS),m_player(nullptr)
{
num_ofRooms =0;
for (int r =0; r < m_rows; r++)
for (int c =0; c < m_cols; c++)
grid[r][c]='#';
num_ofRooms =4+ randInt(3); //randomizes the number of rooms 3-4
bool stop = false; //used to stop adding rooms
int rooms_made =0; //keeps track of rooms created
for (int i =0; i < num_ofRooms && stop == false; i++)
{
rooms.push_back(room());
//randomizing the height and length of rooms
rooms[i].height =4+ randInt(5);
rooms[i].length =6+ randInt(13);
//Creating the first room
if (i ==0)
{
rooms[i].Row =1+ randInt((MAXROWS -1)- rooms[i].height);
rooms[i].Cols =1+ randInt((MAXCOLS -1)- rooms[i].length);
rooms_made++;
}
else //Creating the rooms that are not the first.
{
int attemptingToCreate =0;
bool goodPos = false;
bool go_on = true;
while (goodPos == false)
{
go_on = true;
rooms[i].Row =1+ randInt((MAXROWS -1)- rooms[i].height);
rooms[i].Cols =1+ randInt((MAXCOLS -1)- rooms[i].length);
for (int k =0; k < rooms_made && go_on == true;)//Checking for overlap
{
if ((rooms[i].Row + rooms[i].height)<(rooms[k].Row)||
(rooms[i].Row)>(rooms[k].Row + rooms[k].height)||
(rooms[i].Cols + rooms[i].length)<(rooms[k].Cols)||
(rooms[i].Cols)>(rooms[k].Cols + rooms[k].length))
{
k++;
}
else
{
go_on = false;
}
}
if (go_on == true)
{
rooms_made++;
goodPos = true;
}
else
{
attemptingToCreate++;
if (attemptingToCreate ==100000)//number of attempts
{
rooms.pop_back();
rooms_made--;
i--;
stop = true;
break;
}
}
}
}
}
num_ofRooms = rooms_made;
for (int i =0; i < num_ofRooms; i++)
{
for (int r = rooms[i].Row; r < rooms[i].Row + rooms[i].height; r++)
for (int c = rooms[i].Cols; c < rooms[i].Cols + rooms[i].length; c++)
{
grid[r][c]='';
}
}
////////////////////////Create and Connect Corridors////////////////////////
//updating room centers
for (int i =0; i < rooms.size(); i++)
{
rooms[i].Old_centerR = rooms[i].centerR;
rooms[i].Old_centerC = rooms[i].centerC;
rooms[i].centerR = rooms[i].Row +(rooms[i].height /2);
rooms[i].centerC = rooms[i].Cols +(rooms[i].length /2);
}
// Generate corridors between rooms
for (int i =0; i < rooms.size()-1; i++)
{
int start_c = rooms[i].centerC;
int start_r = rooms[i].centerR;
int end_c = rooms[i +1].centerC;
int end_r = rooms[i +1].centerR;
// Generate horizontal corridor
for (int x = std::min(start_c, end_c); x <= std::max(start_c, end_c); ++x)
{
grid[start_r][x]='';
}
// Generate vertical corridor
for (int y = std::min(start_r, end_r); y <= std::max(start_r, end_r); ++y)
{
grid[y][end_c]=''
}
Temple::~Temple()
{
delete m_player;
}
int Temple::rows() const
{

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine M Ricardo, Susan D Urban

3rd Edition

1284056945, 9781284056945

More Books

Students also viewed these Databases questions

Question

Describe the different sources of demand.

Answered: 1 week ago