Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Name: YOUR NAME
- * Fires Project
- * Course: CSI108 (Fall 2019)
- * Date: December 8, 2019
- * Description: Put out various types of fires in the lab that
- * were caused by escaped engineered worms.
- */
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <cstdlib> // for random numbers
- #include <ctime> // for time
- using namespace std;
- // Fire class definition.
- class Fire
- {
- public:
- // Store an identifier for the fire's "type" and
- // give the fire a random starting "level".
- Fire(const string &initType);
- // Return identifier for type of fire.
- string getType() const;
- // Return current level of fire.
- int getLevel() const;
- // Apply some material to the fire.
- virtual void douseWith(const string &material) = 0;
- protected:
- // Limit on initial level of fire.
- static const int MAX_START_LEVEL = 10;
- string type; // identifier for type of fire
- int level; // level of the fire (higher # means worse)
- };
- class SolidFire : public Fire
- {
- public:
- SolidFire(const string& initType)
- : Fire(initType) {};
- void douseWith(const string& material) override
- {
- if (material== "water")
- {
- level = level - 2;
- }
- else if (material == "foam")
- {
- level = level - 3;
- }
- else if (material == "powder")
- {
- level = level - 7;
- }
- else if (material == "CO2")
- {
- level = level;
- }
- }
- };
- class LiquidFire : public Fire
- {
- public:
- LiquidFire(const string& initType)
- : Fire(initType) {};
- void douseWith(const string& material) override
- {
- if (material == "water")
- {
- level = level + 5;
- }
- else if (material == "foam")
- {
- level = level - 2;
- }
- else if (material == "powder")
- {
- level = level - 3;
- }
- else if (material == "CO2")
- {
- level = level -2;
- }
- }
- };
- class ElectricalFire : public Fire
- {
- public:
- ElectricalFire(const string& initType)
- : Fire(initType) {};
- void douseWith(const string& material) override
- {
- if (material == "water")
- {
- cout << "You receive a large electric shock" << endl;
- exit(0);
- }
- else if (material == "foam")
- {
- level = level + 2;
- }
- else if (material == "powder")
- {
- level = level - 3;
- }
- else if (material == "CO2")
- {
- level = level - 2;
- }
- }
- };
- class MetalFire : public Fire
- {
- public:
- MetalFire(const string& initType)
- : Fire(initType) {};
- void douseWith(const string& material) override
- {
- if (material == "water")
- {
- level = level + 7;
- }
- else if (material == "foam")
- {
- level = level + 3;
- }
- else if (material == "powder")
- {
- level = level - 2;
- }
- else if (material == "CO2")
- {
- level = level + 2;
- }
- }
- };
- template<class T>
- int countNulls(T* list[], int length)
- {
- int counter = 0;
- for (int i = 0; i < length; i++)
- {
- if (list[i] = [NULL])
- counter++;
- }
- return counter;
- }
- int main()
- {
- // Seed the random number generator to get different
- // sequences of numbers each time. When initially
- // testing, easier if always get same sequence.
- srand(static_cast<unsigned int>(time(NULL)));
- // Limit number of fires to how many lab stations there are.
- const int MAX_FIRES = 8;
- // Create array to store fire (if any) at each lab station.
- Fire *stations[MAX_FIRES] = { NULL }; // all NULL pointers
- ifstream inStream("lab.txt");
- if (inStream.fail())
- {
- cout << "Cannot Open the file: Exiting program " << endl;
- exit(0);
- }
- while (inStream.peek != EOF)
- {
- string str;
- int i = 0;
- inStream >> str ;
- if (str == "solid")
- {
- SolidFire f(str);
- stations[i] = &f;
- }
- else if (str == "liquid")
- {
- LiquidFire f(str);
- stations[i] = &f;
- }
- else if (str == "electrical")
- {
- ElectricalFire f(str);
- stations[i] = &f;
- }
- else if (str == "metal")
- {
- MetalFire f(str);
- stations[i] = &f;
- }
- else if (str == "none")
- {
- *stations[i] = { NULL };
- }
- i++;
- }
- inStream.close();
- // Display how many fires were started.
- cout << "The worms have caused "
- << MAX_FIRES - countNulls(stations[MAX_FIRES] , MAX_FIRES)
- << " fires to start." << endl;
- const string EXTINGUISHERS[] = { "water", "foam", "powder", "CO2" };
- // ONLY USE sizeof() WHERE DECLARE STATICALLY-ALLOCATED ARRAY.
- // IF USE PASSED ARRAY, YOU GET THE SIZE OF A POINTER.
- const int EXTINGUISHER_TYPES = sizeof(EXTINGUISHERS) / sizeof(EXTINGUISHERS[0]);
- // Limit on fires above which fire department should be called.
- const int SAFE_TOTAL_FIRE_LEVEL = 50;
- while (true)
- {
- // Display the type of fire (if any) and its level
- // at each station in the lab.
- // Total the level of all fires.
- cout << "\nStations:" << endl;
- int totalLevel = 0;
- for (int i = 0; i < MAX_FIRES; i++)
- {
- cout << "#" << (i + 1) << ": ";
- if (stations[i] == NULL) // no fire started?
- {
- cout << "no fire";
- }
- else
- {
- // Display the type of fire.
- cout << stations[i]->getType() << " fire ";
- int level = stations[i]->getLevel();
- if (level == 0) // is former fire now out?
- {
- cout << "is out";
- }
- else
- {
- // Display the level of fire.
- cout << "(level " << level << ")";
- // Accumulate how much fire.
- totalLevel += level;
- }
- }
- cout << endl;
- }
- // Are all fires now at level zero (i.e., out)?
- if (totalLevel == 0)
- {
- cout << "All fires are out." << endl;
- break;
- }
- // Even though don't want to expose worm experiments to public,
- // shouldn't attempt to take on dangerous fires.
- if (totalLevel > SAFE_TOTAL_FIRE_LEVEL)
- {
- cout << "Call the fire department!" << endl;
- break;
- }
- // Randomly find some extinguisher material in lab.
- string material = EXTINGUISHERS[rand() % EXTINGUISHER_TYPES];
- // Allow user to douse fire at one lab station with material.
- // Use enters station # using a 1-based index.
- cout << "\nYou look around and see some '" << material << "'" << endl;
- cout << "Throw at which station (0=don't use)? ";
- int stationNum;
- cin >> stationNum;
- // User chose not to use material.
- if (stationNum == 0)
- continue;
- // Ensure chosen lab station exists.
- if (stationNum < 0 || stationNum > MAX_FIRES)
- cout << "No such station #" << stationNum << endl;
- }
- return 0;
- }
- // Fire class member definitions.
- // Store an identifier for the fire's "type" and
- // give the fire a random starting "level".
- Fire::Fire(const string &initType)
- : type(initType)
- {
- // Fire level in range 1...MAX_START_LEVEL (inclusive).
- level = 1 + rand() % MAX_START_LEVEL;
- }
- // Return identifier for type of fire.
- string Fire::getType() const
- {
- return type;
- }
- // Return current level of fire.
- int Fire::getLevel() const
- {
- return level;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement