Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with C++ In this assignment, you will implement a class that allows the user to play a game when used in conjunction

I need help with C++

In this assignment, you will implement a class that allows the user to play a game when used in conjunction with the other provided files. In the distant future, the people of earth have solved the problem with the Earth freezing over. In the process of fixing this issue the scientists of Earth invented a form of FTL (faster than light) travel. Before the climate crisis was concluded, many people fled Earth on rockets. They were under the impression that the speed of light was the limit for some reason. You have decided to take advantage of the situation to make some money. Youve procured one of the many rockets that are lying around and fitted it with one of the new FTL engines. However, you have two problems: The first is that the rocket you found can only carry Wolves, Steaks, and Rabbits for some reason. The second is that the navigation program that you need to tour the Galaxy for fun and fortune is broken. You managed to find some bits and pieces of a navigation program, but It looks like you will need to fill in the gaps yourself.

The included main program is a simple interface that initializes an instance of galaxyTrader. Then the user will be presented with a selection of possible actions. The main program will then call appropriate methods of the galaxyTrader object. The user will continue to be presented choices until they select theretire option that ends the program.

To complete this assignment, you will need to edit the provided GalaxyTrader.cpp file. In addition, you will need to implement one missing method in the Planet class to better support a GalaxyTrader class method.

You will need to write the following methods of the GalaxyTrader class to complete this assignment. You will find more detailed descriptions in the comments given in the starter code:

init - initializes the planet array using the information in the given file. It should use the init method of the Planet class to do this. If the input file contains a planet outside the bounds of the array the method should immediately return false. (when accessing the file, you will need to use filename.c_str () in place of filename)

buy - ask the user which good they want to buy and how many they want to buy. Then this calls the buyFrom method of the planet the ship is on. Finally, if the transaction was successful the goods purchased are added to the ship's cargo.

sell - ask the user which good they want to sell and how many they want to buy. Then this calls the sellTo method of the planet the ship is on. Finally, if the transaction was successful the goods purchased are removed from the ship's cargo. o

sellTo will need to be implemented in the Planet class for the Galaxy Trader sell method to work. It should be somewhat like the buyFrom method

refuel - Buys as much fuel as possible from the planet the ship is on, at most enough to fill the reserves to MAX_FUEL. This should update both the current fuel and the number of credits.

move - immediately returns false if the user has no fuel, otherwise it asks the user what direction they want to move. Then the ship's position is updated to reflect the move and fuel is decreased by one (if the ship exceeds the limits of the array it should "reappear" on the other side)

print_status - prints all relevant information concerning the ship itself. (see examples for details)

print_planet - checks to see if the Planet the ship is on is empty. If it is print a message saying so, otherwise call the print method of the planet the ship is on.

scan - returns false if the Planet the ship is at is empty, true if it is not.

Recommendations

The provided main function contains several lines that clear the screen, this makes the game easier to keep track of. The downside of this is that it may make it a little more difficult to debug. Feel free to comment those lines out if they are giving you trouble. (system("clear) ;)

This assignment is designed to be done incrementally. For example, when working on the init method try to get it to initialize a Planet using hard coded values. Then test that to make sure it works. After getting that working try to get it to initialize one planet using values from a file. Then test that to make sure it works. Then you can change it a bit more to make it read all the planets in the file.

hw7.cpp:

#include

#include

#include "GalaxyTrader.h"

using namespace::std;

//function name: main_menu

//parameters: whether or not there is an non-empty Planet nearby

//purpose: print a menu containing all relavent options

// asks the user to select one of the given options

//vaild inputs: any number next to a displayed input

//return: the number the user selected

int main_menu(const bool nearby)

{

int selection = -1;

bool valid = false;

while(!valid)

{

cout << "What do you wish to do captain? " << "\t1) review status ";

if (nearby)

{

cout << "\t2) scan nearby planet " << "\t3) buy cargo from the closest planet "

<< "\t4) sell cargo to the closest planet " << "\t5) refuel at the closest planet ";

}

cout << "\t6) perform a jump " << "\t7) perform a wide scan " << "\t8) retire "

<< "Enter one of the above selections: ";

cin >> selection;

if ((selection >= 1 || selection <= 8) && (nearby || (selection < 2 || selection > 5))){

valid = true;

}

else

cout << "Captain I do not recognize this command. ";

}

return selection;

}

int main()

{

GalaxyTrader GT;

if (!GT.init("space.txt"))

cout << "The input file contains an out of bounds Planet, please edit the \"space.txt\" file ";

else

{

system("clear");

cout << "Welcome captain, and thank you for using the Galaxy Trader simulator. "

<< "Your goal is to make as many credits (money) as possible before retiring. ";

int command = 0;

while(command != 8)

{

command = main_menu(GT.scan());

system("clear"); //if not running in a terminal window comment out the system("clear"); lines

switch(command)

{

case 1:

GT.print_status();

break;

case 2:

GT.print_planet();

break;

case 3:

GT.print_planet();

cout << endl;

if (GT.buy())

{

cout << endl;

GT.print_status();

cout << endl;

GT.print_planet();

}

else

cout << "The trade did not go through Captain. ";

break;

case 4:

if (GT.sell())

{

cout << endl;

GT.print_status();

cout << endl;

GT.print_planet();

}

else

cout << "The trade did not go through Captain. ";

break;

case 5:

if (GT.refuel())

{

cout << endl;

GT.print_status();

}

else

cout << "We can't afford any fuel here ";

break;

case 6:

if (GT.move())

{

cout << endl;

GT.print_status();

cout << endl;

GT.print_planet();

}

else

cout << "We can't jump if we don't have any fuel Captain. ";

break;

case 7:

GT.areaScan();

break;

case 8:

break;

default:

cout << "Command not defined. ";

break;

}

cout << " ";

}

} // end else

return 0;

}

space.txt:

0

Earth

1 1 9999 15 24 1 50

1

Not-so-Cold

1 2 3 4 5 6 7

7

Gas-Station

0 1 0 1 30 1 3

2

Hard-to-Find

0 2 0 10 30 15 5

4

Scarcity

0 0 0 100 100 100 500

GalaxyTrader.h:

#include

#include

#include "Planet.h"

using namespace::std;

class GalaxyTrader

{

public:

GalaxyTrader();

GalaxyTrader(GalaxyTrader& other);

~GalaxyTrader();

bool init(const string filename);

bool buy();

bool sell();

bool refuel();

bool move();

void print_status() const;

void print_planet() const;

bool scan() const;

void areaScan() const;

private:

const static int MAX_FUEL = 5;

const static int SIZE = 10;

Planet planets[SIZE];

int ship_pos;

int wolves;

int steaks;

int rabbits;

int fuel;

int credits;

};

GalaxyTrader.cpp:

#include

#include "GalaxyTrader.h"

using namespace::std;

//constructor

GalaxyTrader::GalaxyTrader()

{

}

//copy constructor

GalaxyTrader::GalaxyTrader(GalaxyTrader& other)

{

}

//destructor

GalaxyTrader::~GalaxyTrader()

{

}

//method name: init

//parameters: the name of the file to read from

//purpose: initializes the planet array using the information in the given file

// should use the init method of the Planet class

// if the input file contains a planet outside the bounds of the array immediately return false

//input file format:

// position of planet

// name of planet

// Number of: Wolves Steaks Rabbits then Prices of: Wolves Steaks Rabbits Fuel

//example: 2

// Mars

// 4 13 15 100 10 20 8

//returns: false if any planets were going to be placed out of bounds, true otherwise

// You may assume that the file format is correct, you do not need to error check anything except

// Planet position

bool GalaxyTrader::init(const string filename)

{

return true;

}

//method name: buy

//parameters: none

//purpose: ask the user which good they want to buy and how many they want to buy

// then this calls the buyFrom method of the planet the ship is on

// finally if the transaction was sucessful the goods purhased are added to the ship's cargo

//valid inputs: w/s/r for goods, any non negative number for quantity

//returns: true if the transaction was successful, false otherwise

bool GalaxyTrader::buy()

{

return true;

}

//method name: sell

//parameters: none

//purpose: ask the user which good they want to sell and how many they want to buy

// then this calls the sellTo method of the planet the ship is on

// finally if the transaction was sucessful the goods purhased are removed from the ship's cargo

//valid inputs: w/s/r for goods, any non negative number up to the number of the good in the ship's cargo for quantity

//returns: true if the transaction was successful, false otherwise

bool GalaxyTrader::sell()

{

return true;

}

//method name: refuel

//parameters: none

//purpose: buys as much fuel as possible from the planet the ship is on, at most enough to fill the reserves to MAX_FUEL

//returns: true if any fuel was purchased, false otherwise

bool GalaxyTrader::refuel()

{

return true;

}

//method name: move

//parameters: none

//purpose: immediately returns false if the user has no fuel

// otherwise it asks the user what direction they want to move

// then the ship's position is updated to reflect the move and fuel is decreased by one

//note: if the ship exceeds the limits of the array it should "reapear" on the other side

//valid inputs: l for left, r for right

//returns: true if the ship's position changed, false otherwise

bool GalaxyTrader::move()

{

return true;

}

//method name: print_status

//parameters: none

//purpose: print the coordinates of the ship

// the credits availble

// currentfuel/MAX_FUEL

// and everything in the ships cargo (should say it is empty if it contains nothing)

//returns: none

void GalaxyTrader::print_status() const

{

}

//method name: print_planet

//parameters: none

//purpose: checks to see if the Planet the ship is on is empty

// if it is print a message saying so

// otherwise call the print method of the planet the ship is on

//returns: none

void GalaxyTrader::print_planet() const

{

}

//method name: scan

//parameters: none

//returns: false if the Planet the ship is at is empty, true if it is not

bool GalaxyTrader::scan() const

{

return true;

}

//method name: areaScan

//parameters: none

//purpose: asks the user how wide a scan they would like to perform

// then areaScan prints out a grid of 1 tall and 2*radius+1 wide

// each spot on the grid contains either an [ ] or a [?]

// [ ] represents a location with no planet

// [?] represents a location with a planet (? should be replaced with the first character of the name of the planet)

// the center of the grid represents where the ship currently is and should have a small arrow indicating where the ship currently i

//valid inputs: a number between 1 and SIZE

//returns: none

void GalaxyTrader::areaScan() const

{

}

Planet.h:

#include

#include

using namespace::std;

const int NUM_DATA = 7; // The number of data items stored about each planet

const int NUM_W = 0; // The number of wolves for sale

const int NUM_S = 1; // The number of steaks for sale

const int NUM_R = 2; // The number of rabbits for sale

const int PRICE_W = 3; // The price for a wolf

const int PRICE_S = 4; // The price for a steak

const int PRICE_R = 5; // The price for a rabbit

const int PRICE_F = 6; // The price for fuel

class Planet

{

public:

Planet();

Planet(Planet& other);

~Planet();

void print() const;

void init(const string n, const int data[7]);

bool buyFrom(const char good, const int amount, int& credits);

// add function prototype for sellTo here

int getFuel() const;

bool isEmpty() const;

string getName() const;

private:

string name;

bool empty;

int wolves;

int steaks;

int rabbits;

int price_wolves;

int price_steaks;

int price_rabbits;

int price_fuel;

};

Planet.cpp:

#include

#include "Planet.h"

using namespace::std;

//constructor

Planet::Planet()

{

empty = true;

name = "default";

wolves = 0;

steaks = 0;

rabbits = 0;

price_wolves = 0;

price_steaks = 0;

price_rabbits = 0;

price_fuel = 99999;

}

//copy constructor

Planet::Planet(Planet& other)

{

empty = other.empty;

name = other.name;

wolves = other.wolves;

steaks = other.steaks;

rabbits = other.rabbits;

price_wolves = other.price_wolves;

price_steaks = other.price_steaks;

price_rabbits = other.price_rabbits;

}

//destructor

Planet::~Planet()

{

}

//method name: init

//parameters: the name of the planet

// an array containing the quantities and prices for goods on the Planet

//purpose: sets a Planet as none empty and populates it's member variables with the given values

//returns: none

void Planet::init(const string n, const int data[])

{

name = n;

wolves = data[NUM_W];

steaks = data[NUM_S];

rabbits = data[NUM_R];

price_wolves = data[PRICE_W];

price_steaks = data[PRICE_S];

price_rabbits = data[PRICE_R];

price_fuel = data[PRICE_F];

empty = false;

}

//method name: print

//parameters: none

//purpose: display all meaningful values the Planet has to the user

// in the case that the planet is uninitialized print only a message indicating this

//returns: none

void Planet::print() const

{

if(!empty)

{

cout << "\tWelcome to planet " << name << endl;

cout << "\tOur " << wolves << " wolves are going for " << price_wolves << " credits each." << endl;

cout << "\tOur " << steaks << " steaks are going for " << price_steaks << " credits each." << endl;

cout << "\tOur " << rabbits << " rabbits are going for " << price_rabbits << " credits each." << endl;

cout << "\tFuel can be purchased for " << price_fuel << "credits." << endl;

}

else

cout << "This planet is uninhabited ";

}

//method name: buyFrom

//parameters: the first letter of the good the user wished to buy

// the quantity the user wishes to purchase

// a reference to the credits the user has

//purpose: check to make sure that the offer makes sense

// if it does update the number of goods on the planet and the credits the user has

//returns: true if the offer made sense, false otherwise

bool Planet::buyFrom(const char good, const int amount, int& credits)

{

bool valid = false;

switch(good)

{

case 'w':

if(amount * price_wolves <= credits && amount <= wolves)

{

wolves -= amount;

credits -= amount * price_wolves;

valid = true;

}

break;

case 's':

if(amount * price_steaks <= credits && amount <= steaks)

{

steaks -= amount;

credits -= amount * price_steaks;

valid = true;

}

break;

case 'r':

if(amount * price_rabbits <= credits && amount <= rabbits)

{

rabbits -= amount;

credits -= amount * price_rabbits;

valid = true;

}

break;

}

return valid;

}

//method name: sellTo

//parameters: the first letter of the good the user wished to buy

// the quantity the user wishes to purchase

// a reference to the credits the user has

//purpose: update the number of goods on the planet and the credits the user has

//returns: false if the given good doesn't exist, true otherwise

//

// ADD YOUR METHOD HERE

//

//method name: getFuel

//parameters: none

//returns: the price of fuel on this planet

int Planet::getFuel() const

{

return price_fuel;

}

//method name: isEmpty

//parameters: none

//returns: true if the planet is empty, false otherwise

bool Planet::isEmpty() const

{

return empty;

}

//method name: getName

//parameters: none

//returns: the name of the Planet

string Planet::getName() const

{

return name;

}

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

Data Visualization A Practical Introduction

Authors: Kieran Healy

1st Edition

0691181624, 978-0691181622

More Books

Students also viewed these Databases questions