Programming Project Five
Pirates of the Chesapeake
Out: Monday 05/01/06
Due Before Midnight, Sunday 05/14/06
The design document for this project, design5.txt
is due: Before Midnight, Sunday 05/07/05
The objective of this assignment is to give you experience working with
linked lists. You'll be using command-line arguments, doing file handling,
dynamic memory allocation, passing by reference, and using random numbers. You
will also continue to practice using good program design and implementation
'Twas a clash of two captains on the high seas of the Chesapeake. Each
captain controls a number of ships, each ship carrying one type of cargo.
The two fleets, or sets of ships, and all of the sailors on board battle,
but 'tis the captain with more sailors that wins.
As a spectator going out for your daily row in the Chesapeake Bay, your job
is to represent these captain's fleets in a program as two linked lists.
You'll show the transfer of ships between fleets before and after a battle,
and even print a portrait of the captain's ships you saw from your little
rowboat as ascii art.
Design and code a project that uses command-line arguments to build two linked
lists: one for each fleet in the bay. Based on the total number of sailors in
the battle, generate a random number of ships the losing captain must forfeit,
and append those ships to the winning Captain's fleet. Print the contents
of both fleets before and after the battle, as well as an ascii-art depiction
of the each fleet's Captain's ship, containing the captain's name, number
of sailors on board, and where the captain is from.
Using the files pirate1.dat and
pirate2.dat, this output was generated.
Although your output need not be identical to the above, all information
(including the greeting) must be present.
The input files will always have:
- A captain's name of 14 or less characters
- The captain's home of 10 or less characters
- A series of boats that has the cargo (<= 49 charcters),
weight (in tons), and number of sailors.
- One blank line between fleets, designating another captain's
- While developing your project, work on the ascii art sailboats last.
Make sure the linked lists have been properly constructed and battles
have appropriated the movement of ships correctly before you print the
- The winner of the battle is based solely on which captain has more
sailors in his/her fleet.
- The amount of ships the losing captain must give up is a random number
from 0 to the total amount of ships in the losing captain's fleet.
Remove these ships from the front of the losing fleet's linked-list,
and add them to the back of the winning fleet's linked-list.
- If a captain loses all of his ships, he/she has to walk the plank.
See sample output for an example.
- If a captain loses none of his ships, he/she sails home safey.
See sample output for an example.
- You must print information about each fleet BEFORE and AFTER the battle
to the output file entered at the command line.
- You must use separate compilation for this project. You may have as
many .c and .h files as you want to fit your project design. Your file
that contains main() should be called proj5.c You must have at least 3
- You must use linked-lists for this project and you must use two of
them. You must manage these linked-lists using head nodes that
contain the captain's name, captain's home, total
number of sailors in the fleet, total number of ships in the
fleet, and a pointer to the rest of the fleet (the head
of the list of ships). You may have an array headNodes of
size 2 (one for each fleet), but you may not have an
array to manage the list of a fleet's ships.
- You may use the linked list implementation given in lecture 21. Some
of the functions will need minor modification, but some will work as
they are (if you choose your data structures carefully when designing).
- You MUST use dynamic memory allocation (malloc) to allocate the
memory for the nodes of the linked lists. Of course, you must check to
see that the space was actually given to you. You must also destroy
each list after you are finished using it.
- Even though you must keep track of how many ships are in each fleet,
you MAY NOT traverse your lists using these numbers and counting
nodes. Always traverse lists to their end by examining the next pointer.
- You must write your own DestroyList() function, which will free all of
the nodes of a linked list that is passed to it.
- You may get copies of the data files from my account as in previous
projects. The files are called pirate1.dat and pirate2.dat and they are
in the /afs/umbc.edu/users/s/b/sbogar1/pub directory.
- The filename of the input file must be given on the command line, as
well as the the name of the output file. An optional fourth argument
can be entered to seed the random number generator.
- If the user runs the program with 3 command-line arguments, then
you should seed the random number generator with a call to time().
- If the user runs the program with 4 command-line arguments, then
the value of the fourth argument should be used as the seed.
Hint: You'll need to use atoi().
- Regardless of how the random number generator is seeded, it
should only be seeded once.
- If the program is run with the wrong number of command line arguments,
an appropriate message must be printed to the screen using stderr
informing the user how to correctly run the program and then then
program must exit.
- If a file fails to open, you should print an appropriate message to
the screen using stderr and exit the program.
- You must close the data file as soon as you are finished reading
- You must guard your header files.
Submitting the Program
Here is a sample submission command. Since you may have different file names
you'll have to adjust it to fit your needs. As always, be sure to submit all
of the files necessary for your project to compile.
Note that the project name starts with uppercase 'P'.
submit cs201 Proj5 proj5.c pirate.c pirate.h
To verify that your project was submitted, you can execute the
following command at the Unix prompt. It will show the file that
you submitted in a format similar to the Unix 'ls' command.
submitls cs201 Proj5