1. Overview

In this project, you will:

  • Practice basic C++ syntax including branching structures
  • Write classes and instantiate those classes using a constructor
  • Create a templated data structure (queue or stack)
  • Use simple file input
  • Use overloaded operators to access templated data structure

2. Background

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).

3. Assignment Description

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.

4. Requirements:

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.

  • You must follow the coding standard as defined in the CMSC 202 coding standards (found on Blackboard under course materials). This includes comments as required.
  • The project must be turned in on time by the deadline listed above.
  • The project must be completed in C++. You may not use any libraries or data structures that we have not learned in class. Libraries we have learned include < iostream>, < fstream>, < iomanip>, < vector>, < cmath>, < ctime>, < cstdlib>, < sstream>, and < string>. You should only use namespace std.
  • Using the provided files, Tqueue.cpp, StarWars.h, Ship.h, Imperial.h, Rebel.h, p5_imperial.txt, p5_rebel.txt, and the driver.cpp file, create Star Wars. (Finish Tqueue first though!)
  • As a reminder, Tqueue.cpp is templated and all functions must exist in ONE file (Tqueue.cpp)
  • You can copy the files from my directory in /afs/umbc.edu/users/j/d/jdixon/pub/cs202/proj5.
  • Class member variables must be private for project 5.
  • You must use the provided header files (Tqueue.cpp, StarWars.h, Ship.h, Imperial.h, Rebel.h, and driver.cpp). You must use the Tqueue in StarWars for the two teams and you must use two vectors to store the data read in from the two files.
  • The Tqueue must be templated with all requested functions implemented (including the copy constructor and assignment operator). Please test your Tqueue before starting the Star Wars aspects of this project!
  • All user input must be validated. For example, if a menu allows for 1, 2, or 3 to be entered and the user enters a 4, it will re-prompt the user. However, the user is expected to always enter the correct data type. i.e. If the user is asked to enter an integer, they will. If they are asked to enter a character, they will. You do not need to worry about checking for correct data types.
  • In order to help test your code more efficiently, the command line arguments have been included. You can make the data file smaller if it helps with your testing. Only one data file has been provided but you can copy it and cut it down for testing.
  • You may not hard-code the number of ships in the file or the names of the files (except for testing) A team can hold as many ships as we like (assuming the limits of an integer or memory).
  • We will test your Tqueue file separately from your application (as well as part of it). It must be fully implemented with a destructor, copy constructor, and an assignment operator. Test it first using something simple like ints.
  • After the Tqueue works, write Ship (it is easy). Tqueue is tricky though
  • After Tqueue and Ship are written, write the two sub-classes. They are pretty easy although they are polymorphic.
  • Once Tqueue, Ship/Rebel/Imperial are written, start StarWars. Load the two lists of ships into vectors first. You can assume that file[0] will be the list of imperial ships and file[1] will be the rebel ships. Make the teams using Tqueues next. Finally, work on Battle. Start just calls each of the functions in order.
  • The code must not have memory leaks.

5. Sample Input and Output

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.

6. Compiling and Running

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

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.