In this project, you will:
A long time ago in a galaxy far, far awayStar Wars, the epic space opera, blasted into the theaters in 1977. Since then, this popular franchise has spawned at least 11 movies plus a variety of animated movies and TV shows. In 2015, it was estimated that the Star Wars franchise was worth an astonishing $42 billion.
In 2006, LucasArts published Star Wars: Empire at War which was a real-time strategy game involving the struggle between the Imperial ships and the Rebel ships. For our project, we are going to create a text-based version of that game.
This project requires a special data structure, a Tqueue, that we are going to build using a dynamically allocated array. The Tqueue is a templated data structure that is designed to handle most types of data which is first-in, first-out (FIFO).
Your assignment is to implement a Tqueue structure that dynamically resizes itself based on how much is enqueued (added) or dequeued (removed). If the Tqueue reaches its maximum capacity during an enqueue call, it doubles the capacity. If the Tqueue reaches half of the capacity during a dequeue call, it halves the capacity. Once the Tqueue structure has been built and very thoroughly tested, the next step will be to use the Tqueues to simulate two ships battling in space.
Class 1 - Tqueue This is a very important class. It is used to manage the teams of ships. It uses a dynamically allocated array to store the information about whatever it is designed to hold (in this case Ships). Importantly, it automatically resizes as needed. During an enqueue (adding to the Tqueue), if the maximum capacity of the Tqueue is met, it doubles the capacity of the Tqueue and copies all current entries into a new array. During a dequeue (removing form the Tqueue), if the size is less than half the capacity, it halves the capacity of the Tqueue and copies all current entries into a new array. It also uses an overloaded [ ] operator to iterate over the array. Finally, you must implement the copy constructor and assignment operator in this class. You should implement this class by itself and then test it completely before using it. Do not forget how we must implement templated classes! Below is a sample of how the Tqueue would grow as enqueue is called. see image.
Class 2: Ship - This is a trivial parent class with little more than a very simple constructor, setters, getters, and a purely virtual attack function (which must be implemented in the two subclasses). The overloaded << operator and the virtual toString function are very similar to project 4. They are used to be able to output a ship (or either subclass).
Classes 3 and 4: Rebel/Imperial - These are the two subclasses to Ship. They are very similar to each other except that in the attack functions they attack each other (Rebels attack Imperials or Imperials attack Rebels). The other function is an overridden toString that displays information about the ships.
Class 5: StarWars - This is a pretty straightforward class that manages the loading of the input files, the creation of the two teams, and the battle. It is called directly from proj5.cpp. Loadships reads in the files and populates the vectors. CreateTeams populates the two Tqueues randomly from their respective vectors (m_myShips from m_rebelShips and m_enemyShips from m_imperialShips). A team can have duplicate ships. Battles will continue until one of the Tqueues is empty. One team will randomly start. One ship will attack another ship resulting in either a ship being damaged or destroyed. If it is damaged, it attacks back. If it is destroyed, it is removed from the Tqueue. If the battle continues, the next ship that attacks is random. This continues until one team is victorious.
This is a list of the requirements of this application. For this project, it is up to you exactly how you want to implement it. For you to earn all the points, however, you will need to meet all the defined requirements.
For this project, input files are very simple. For this project, there are two input files (proj5_imperial.txt and proj5_rebel.txt. Command line arguments have been included (as well as the makefile) so you can use make run or make val1 to test your code. You can add additional test files and rules in your makefile.
Battles with one or two ships are relatively short so they are listed below. Longer runs are included:
/afs/umbc.edu/users/j/d/jdixon/pub/cs202/proj5/
They are called proj5_sample1.txt and proj5_sample2.txt. Below is a sample with one ship: see image.
Below is a sample with two ships: see image.
You will need to implement the makefile for this project.
Once you have compiled using the makefile, enter the command make run1 or ./proj5 proj5_imperial.txt proj5_rebel.txt to run your program. If your executable is not proj5, you will lose points. It should look like the sample output provided above.
Because we are using dynamic memory for this project, you are required to manage any memory leaks that might be created. Anything that you use "new" for needs to be deleted. Remember, in general, for each item that is dynamically created, it should be deleted using a destructor.
One way to test to make sure that you have successfully removed any of the memory leaks is to use the valgrind command.
Since this project makes extensive use of dynamic memory, it is important that you test your program for memory leaks using valgrind:
valgrind ./proj5 proj5_imperial.txt proj5_rebel.txt
Note: If you accidently use valgrind make run1, you may end up with some memory that is still reachable. Do not test this - test using the command above where you include the input file.
If you have no memory leaks, you should see output like the following: see image.
The important part is "in use at exit: 0 bytes 0 blocks," which tells me all the dynamic memory was deleted before the program exited. If you see anything other than "0 bytes 0 blocks" there is probably an error in one of your destructors. We will evaluate this as part of the grading for this project.
Additional information on valgrind can be found here:
http://valgrind.org/docs/manual/quick-start.html