Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am working on a project to create a roguelike game, So far I am focusing on the grid and player movement. So far I

I am working on a project to create a roguelike game, So far I am focusing on the grid and player movement. So far I have gotten the grid to display and I believe movement logic. However, sometimes the player will be created and sometimes it won't. When it is, the player symbol '@' trails and make duplicates, the player also runs into walls which is not suppose to happen. Could you eveluate the code implemented so far and fix the issue? Thank you!
//Game.h runs the game
#ifndef GAME_INCLUDED
#define GAME_INCLUDED
// You may add data members and other member functions to this class.
class Temple;
class Game
{
public:
Game(int goblinSmellDistance);
~Game();
/* void display() const;
void status(bool& over, bool& win) const;
void playerInput();*/
void play();
private:
Temple* m_temple;
//Game.cpp
};Game::Game(int goblinSmellDistance)
{
int rows = MAXROWS;
int cols = MAXCOLS;
m_temple = new Temple(rows,cols);
int rPlayer = randInt(1, cols);
int cPlayer = randInt(1, rows);
m_temple->addPlayer(rPlayer, cPlayer);
}
Game::~Game()
{
delete m_temple;
}
void Game::play()
{
m_temple->Create_Temple();
m_temple->display();
Player* p = m_temple->player();
if (p == nullptr)
{
std::cout << "player not made" << std::endl;
return;
}
while (!p->isDead())
{
std::cout << "Level:" <<", Hit points: "<<", Armor:
";
char command = getCharacter();
switch (command)
{
default: // if bad move, nobody moves
cout <<'\a'; // beep
continue;
case 'q':
return;
case 'h':
case 'j':
case 'k':
case 'l':
p->move(command);
break;
}
m_temple->display();
}
std::cout << "game runs" << endl;
}
#define TEMPLE_H grid and player display
#include
#include "Nglobals.h"
#include "NUtilities.h"
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;
void isPlayerAt(int r, int c) const;
bool determineNewPosition(int& r, int& c, int dir) const;
void Create_Temple();
void displayPlayer(Player* player);
void display();
bool isInBounds(int r, int c) const;
bool addPlayer(int r, int c);
private:
std::vectorrooms;
char grid[MAXROWS][MAXCOLS];
int num_ofRooms;
int m_rows;
int m_cols;
Player* m_player;
};#include "NTemple.h"
#include "Nglobals.h"
#include "NPlayer.h"
#include "NUtilities.h"
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]='#';
}
Temple::~Temple()
{delete m_player;}
int Temple::rows() const
{ return m_rows;}
int Temple::cols() const
{return m_cols;}
Player* Temple::player() const
{
return m_player;
}
bool Temple::determineNewPosition(int& r, int& c, int dir) const
{
switch (dir)
{
case ARROW_UP: if (r <=1) return false; else r--; break;
case ARROW_DOWN: if (r >= rows()) return false; else r++; break;
case ARROW_LEFT: if (c <=1) return false; else c--; break;
case ARROW_RIGHT: if (c >= cols()) return false; else c++; break;
default: return false;
}
return true;
}
void Temple::Create_Temple(){//creates rooms and corridors that connect}void Temple::display()//display map only
{
clearScreen();
if (m_player != nullptr)
{
// Set the char to '@', unless there's also a Tooter there
//(which should never happen), in which case set it to '*'.
char& gridChar = grid[m_player->getRow()-1][m_player->getCols()-1];
if (gridChar =='')
gridChar ='@';
}
for (int r =0; r < rows(); r++
{
for (int c =0; c < cols(); c++)
std::cout << grid[r][c];
std::cout << std::endl;
}
}
bool Temple::addPlayer(int r, int c)
{
if (!isInBounds(r, c))
return false;
// Don't add a player if one already exists
if (m_player != nullptr)
return false;
//go through for monsters
m_player = new Player(this, r, c);
return true;
}
bool Temple::isInBounds(int r, int c) const
{
return (r >=1 && r <= m_rows && c >=1 && c <= m_cols);
}
class Player??player implementation
{
public:
Player(Temple* t, int r, int c):m_Temple(t),m_row(r),m_col(c),symbol(symbol)
{}
~Player(){}
char getSymbol() const { return symbol;}
//char setSymbol() const {}
//get and set Coords
int getRow() const { return m_row; }
int getCols() const { return m_col; }
//track movement
int Tracker() const { return track; }
bool isDead(){ return hp <=0; }
void move(int dir){code for movement }??assume all private member are there Also How would I go about like changing my temple code to have a vector for the grid and a char vector tha holds all the available coordinates so i can easily pick from to place the character

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

Step: 3

blur-text-image

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

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

Students also viewed these Databases questions

Question

Identify sustainable HRM practices in an organization.

Answered: 1 week ago

Question

How would you describe the new culture?

Answered: 1 week ago