Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help on this C++ lab activity. Lab 5 is attached at the bottom. Thank you In this lab youll be writing a Game

I need help on this C++ lab activity. Lab 5 is attached at the bottom. Thank you

In this lab youll 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 (youll 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

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: "<

int main()

{

Player play("AJAY",20);

cout<<"After Parameterized Constructor Initialization Entered Name is:"<getName()<

cout<<"After Parameterized Constructor Initialization Entered Age is:"<getAge()<

obj->print(); //Print Names and Age Values Assigned in parameterized Constructor

Location *obj = &play;

obj->setName("ARUN");

obj->setAge(18);

cout<<"Name Set is:"<getName()<

cout<<"Age Set is:"<getAge()<

obj->print(); //Print Names and Age Values Set

return 0;

}

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

SQL Server Query Performance Tuning

Authors: Sajal Dam, Grant Fritchey

4th Edition

1430267429, 9781430267423

More Books

Students also viewed these Databases questions