I need help on this C++ lab activity. Lab 5 is attached at the bottom. Thank you
ID: 3754810 • Letter: I
Question
I need help on this C++ lab activity. Lab 5 is attached at the bottom. Thank you
In this lab you’ll be writing a Game class to use for Project 3. A Game object will be composed of all of the elements that make up the game (such as the player playing the game and the locations in the world that the player can visit). For this lab, open up the Visual Studio project you made for Lab 5 and add files called game.h and game.cpp. Write your Game class so that it has the following attributes:
Location ***world;
Player p;
int rows;
int cols;
int playerRow;
int playerCol;
Your Game class will need to have the following methods:
1. a default constructor that initializes world, rows, cols, playerRow, and playerCol to 0.
2. a method called setUpGame that first sets rows and cols to the number of rows and columns you want to have in your game world (I recommend starting with something no bigger than 8x8) and initializes playerRow and playerCol to the location in the world where you want the player to start. Next, world should be dynamically allocated into a two dimensional array of type Location * with rows and cols as its dimensions. The method should then initialize every location in this array to 0 (or NULL). Finally, this method should create objects of each of your location classes (that you made for Lab 5) at appropriate positions in the world array (you can decide what makes an appropriate position). Place Location objects at each position not filled by an object of one of your other classes.
3. a method called drawGame that loops over the world array and prints a “P” at the location of the Player and calls the draw method for every other location in the world array.
4. a method called playGame that starts by calling the setUpGame method and then begins a do while loop that runs as long as the user wants to continue exploring (you’ll need to come up with a way to ask the user if he/she wants to keep exploring the game world at the bottom of the loop). At the top of the loop call the drawGame method and ask the user what direction he/she wants to move. If the entered move indicates a valid move (i.e. will not move him/her out of the game world), then update playerRow and playerCol appropriately. Finally, call the visit method for the location in the world that the player just entered.
5. a destructor that correctly deletes the dynamically allocated world array.
Once you have written the appropriate code for your game class, have your main function create a Game object and then call its playGame method. You should see the initial state of the game appropriately displayed on the screen and the program should allow you to move around and explore the game world. A sample display is shown on the next page (yours does not have to look like mine).
NOTE: If you want to try to make your display a little more aesthetically pleasing, you can place system(“cls”) calls in your code whenever you want to clear the screen.
Lab 5//
#include <iostream>
using namespace std;
class Crystal:Public Location
{
//empty class
};
class Player:Public Location
{
private:
string name;
int age;
public:
Player(); //default constructor declaration
Player(string n, int a); //parameterized constructor declaration
void setName(string n);
void setAge(int a);
string getName();
int getAge();
void print();
};
Player::Player()
{
name = "";
age = 0;
}
Player::Player(string n,int a)
{
name = n;
age = a;
}
void Player::setName(string n)
{
name = n;
}
void Player::setAge(int a)
{
age = a;
}
string Player::getName()
{
return name;
}
int Player::getAge()
{
return age;
}
void Player::print()
{
cout<<"Name: "<<name<<", Age: "<<age<<endl;
}
int main()
{
Player play("AJAY",20);
cout<<"After Parameterized Constructor Initialization Entered Name is:"<<obj->getName()<<endl;
cout<<"After Parameterized Constructor Initialization Entered Age is:"<<obj->getAge()<<endl;
obj->print(); //Print Names and Age Values Assigned in parameterized Constructor
Location *obj = &play;
obj->setName("ARUN");
obj->setAge(18);
cout<<"Name Set is:"<<obj->getName()<<endl;
cout<<"Age Set is:"<<obj->getAge()<<endl;
obj->print(); //Print Names and Age Values Set
return 0;
}
Explanation / Answer
This book contains numerous sample programs that demonstrate the concepts it teaches. In order to view the programs, you must first compile them. The instructions for compiling them are slightly different for each program. To make things more convenient for you, I've written instructions for how to compile every program. You'll find the compilation instructions on the CD. To see them, insert the CD into your CD/DVD-ROM drive. On the HTML page that appears, you'll find a list item called Compilation Instructions. Click that link. At this point, an HTML page appears called Compiling the Sample Programs. This page contains a list of all of the sample programs grouped by chapter. To see how to compile a particular program, click on its link in the list. The compilation instructions for that program will appear. In addition, I've provided a compiled version of each program. They're in the Bin (short for binary) folder for their respective chapters. You'll see a link for each Bin folder on the Compiling the Sample Programs page.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.