Ye’ve been sailing the high seas for some time now. After a treacherous voyage through a raging storm, yer ship is off course afloat through unchartered waters. The morn’ after, the crew wakes up to find themselves amongst ravenous great white sharks1, Titanic-sinking icebergs, and an enormous British battleship loaded to the hilt with soldiers and artillery. Ironically, they’re on patrol looking for plunderin’, pillaging pirate ships. Not that your crew ever plunders or pillages, but these activities will definitely have to be put on hold until ye’ve navigated yer way out of these hostile waters!

1 Think “Jaws”. 2 Pirates certainly have enough gold and jewels to buy high powered canons and return enemy fire if they so desired but they typically spend it on ale and potato chips.

Here is a rundown of your current situation.

  • One British battleship.
  • Very hungry great white sharks that fancy pirates because they tend to taste like ale.
  • Random icebergs. In addition, the entire perimeter of the oceanic area described is made up of icebergs that formed in the night.
  • A single exit point from the wall of surrounding icebergs that will release your ship into the open sea for escape.

In planning, you pull out some clean scroll paper and plot out the immediately surrounding ocean. It’s represented by a grid that is 30 spaces wide and 15 lines long, each point representing a square nautical mile. You will be repeatedly asked what direction you want to move. Each time you move, you must try to avoid hitting, or being hit/eaten by an iceberg or a shark (remember, they’re very big and like to eat pirates), or being blasted by the Brits. You are within range of the Brits if you are in an adjacent square to their war vessel, including diagonal squares. It’s almost certain that your ship will be obliterated by cannon fire2.

Strategy:

  • Ask the pirate for the name of the map file. This may not be hardcoded.
  • Open the file and read in exactly 15 lines with exactly 30 integers per line.
  • The map file is coded as follows:
    • 0 = open sea
    • 1 = iceberg
    • 2 = shark
    • 3 = British battleship
    • 4 = the escape position
    • 5 = your pirate ship
  • Keyboard commands will move your ship as follows (lowercase):
    • ‘n’ means north, or one row above your current position.
    • ‘e’ means east, or one column to the right of your current position.
    • ‘s’ means south, or one row below your current position.
    • ‘w’ means west, or one column to the left of your current position.
    • Any other keyboard entry means your ship stays in the same position as a penalty3.
  • Each time you move, the battleship and all the sharks move one space toward you.
    • Warning, they can also navigate diagonally, i.e., northeast, northwest, southeast and southwest. Your ship cannot do this with its outdated technology.
    • The battleship destroys anything in its path – icebergs4, sharks, and, unfortunately, you. That means if a battleship moves to a position where an iceberg was, when it moves away, that map position becomes open sea. The same goes for sharks.
    • If two sharks collide, only one is left.
  • The ocean map should be displayed on the screen after each time you move and after the battleship and all of the sharks have moved.
  • The scenario continues until you reach the exit point or until the sharks or the Brits have eliminated you5 or you carelessly navigate into an iceberg.
  • Once the scenario has reached a conclusion, print out what happened. (Did the pirate ship escape? Get blown to bits? Become human sushi?)

3 Sometimes pirates just can’t make a decision. 4 The Brits have iceberg crushing thing-a-ma-jiggies on the bow of their ship. 5 The Brits don’t take hostages. They will simply blow up your ship and sail off into the sunset.

Printing the map.

  • You read in the map as a 2-dimensional array of integers for the sole purpose of file reading sanity. However, this is not how it will appear when you display it on the screen.
    • Your ship and mates are represented by a ‘P’.
    • The battle ship is represented by a ‘B’.
    • Sharks are represented by a ‘$’.
    • Icebergs are represented by an asterisk ‘*’.
    • Open sea is represented by a blank ‘ ’.
    • The escape position is represented by an ‘X’.
  • There are different ways to handle this so as long as it makes sense, I won’t interfere. If you want advice, see me during office hours or ask in class.

Code:

  • Use a two-dimensional array to represent the oceanic grid map.
  • At a minimum you need functions to:
    • Give the user playing instructions.
    • Read in the initial map.
    • Print the current ocean grid map.
    • Get a move from the pirate captain (that’s you now)6
    • Move the sharks and battleship.
    • Check to see if you have reached the escape point.
  • Make sure you use call by value and call by reference properly.
  • Use a header comment with your name and the description of the program.
  • Use appropriate code comments.
  • Use appropriate variable names.
  • Use constants where appropriate.
  • Use good coding practices (i.e., spacing, indentation, etc.)
  • Do not use techniques not covered through chapter 8 of Gaddis.
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.