Anda di halaman 1dari 5

1

ET-575 - EC - Sailing the Seas (Function/Array)

Create the following program.

a) Create a two dimensional 10x10 character array.


b) Create two global Boolean variables, isLost and isSunk.

b) Create an initMap function that does the following:

- Populates the array with tilde ‘~’, period ‘.’ and the ‘S’ character
so that if you output the array (nested loop) it looks like this:

~ . . . . . . . . .
~ ~ . . . . . . . .
~ ~ ~ . . . . . . .
~ ~ ~ ~ . . . . . .
~ ~ ~ ~ ~ . . . . .
~ ~ ~ ~ ~ S . . . .
~ ~ ~ ~ ~ ~ ~ . . .
~ ~ ~ ~ ~ ~ ~ ~ . .
~ ~ ~ ~ ~ ~ ~ ~ ~ .
~ ~ ~ ~ ~ ~ ~ ~ ~ ~

- This display is a map, where ‘~’ represents the ocean, ‘.’ Represents
land and the ‘S’ represents a ship that is sailing on the ocean.
- The ‘S’ should be at located in array 6 at position 6 (5,5).
Note that coordinate 5,5 on this display is location 6,6 in the
array. There is an offset that you need to account for.

c) Create a displayMap function that does the following:

- Uses a nested loop to outputs the updated map to console.


- Pads the map image with coordinate values.
- The initial display grid should appear exactly as follows:

0 ~ . . . . . . . . .
1 ~ ~ . . . . . . . .
2 ~ ~ ~ . . . . . . .
3 ~ ~ ~ ~ . . . . . .
4 ~ ~ ~ ~ ~ . . . . .
5 ~ ~ ~ ~ ~ S . . . .
6 ~ ~ ~ ~ ~ ~ ~ . . .
7 ~ ~ ~ ~ ~ ~ ~ ~ . .
8 ~ ~ ~ ~ ~ ~ ~ ~ ~ .
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
0 1 2 3 4 5 6 7 8 9

Introduction to C++ Programming Design and Implementation S. Trowbridge 2017


2

d) Create a validate function that does the following:

- Accepts a character as input.


- If the char is a lower case letter return the input value.
- If the char is an upper case letter, return the lowercase value.

e) Create a menu function that does the following:

- Implements a looping switch menu that accepts a letter as input.


- Use the validate function to handle input values.
- If the input is not a letter use the default case of the switch menu
to output “"Pick a direction!".
- a valid input value should call the moveShip function.
- The program should loop if and only if the ship is not sunk or lost.
- if ship has sunk, end the loop and output:
“CRASH!!! You boat begins to sink!”
- if ship is lost, end the loop and output:
“"ARGGGH!!!...you have been lost at sea!!!"
- as long as the ship ‘S’ is moved onto an ocean tile (‘~’) the
program should continue to loop endlessly.
- The menu should output as follows (n is the input):

(n)orth
(s)outh
(e)ast
(w)est

Which way, Captain? n

f) Create a moveShip function that does the following:

- Accepts a character as input which specifies direction.


- Based upon the input value, the ship should do the following:
- If the ship sails into another ocean square (‘~’) update the
‘S’ position in the array so that when the program loops displays
‘S’ at its new location.
- If the ship sails off of the map, it is lost. Represent this by
setting the isLost variable to true.
- If the ship sails onto land, it sinks. Represent this by
setting the isSunk variable to true.

* Possible directions are:

N
W E
S

Introduction to C++ Programming Design and Implementation S. Trowbridge 2017


3

Output Example 1 (Ship is Sunk):

Sailing the Seas


---------------
~ . . . . . . . . .
~ ~ . . . . . . . .
~ ~ ~ . . . . . . .
~ ~ ~ ~ . . . . . .
~ ~ ~ ~ ~ . . . . .
~ ~ ~ ~ ~ S . . . .
~ ~ ~ ~ ~ ~ ~ . . .
~ ~ ~ ~ ~ ~ ~ ~ . .
~ ~ ~ ~ ~ ~ ~ ~ ~ .
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
0 1 2 3 4 5 6 7 8 9

n)orth
s)outh
e)ast
w)est

Which way, Captain? s

~ . . . . . . . . .
~ ~ . . . . . . . .
~ ~ ~ . . . . . . .
~ ~ ~ ~ . . . . . .
~ ~ ~ ~ ~ . . . . .
~ ~ ~ ~ ~ ~ . . . .
~ ~ ~ ~ ~ S ~ . . .
~ ~ ~ ~ ~ ~ ~ ~ . .
~ ~ ~ ~ ~ ~ ~ ~ ~ .
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
0 1 2 3 4 5 6 7 8 9

n)orth
s)outh
e)ast
w)est

Which way, Captain? e

~ . . . . . . . . .
~ ~ . . . . . . . .
~ ~ ~ . . . . . . .
~ ~ ~ ~ . . . . . .
~ ~ ~ ~ ~ . . . . .
~ ~ ~ ~ ~ ~ . . . .
~ ~ ~ ~ ~ ~ S . . .
~ ~ ~ ~ ~ ~ ~ ~ . .
~ ~ ~ ~ ~ ~ ~ ~ ~ .
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
0 1 2 3 4 5 6 7 8 9

n)orth
s)outh
e)ast
w)est

Which way, Captain? n

CRASH!!! You boat begins to sink!

Introduction to C++ Programming Design and Implementation S. Trowbridge 2017


4

Output Example 2 (Ship is Lost):


Sailing the Seas
----------------
0 ~ . . . . . . . . .
1 ~ ~ . . . . . . . .
2 ~ ~ ~ . . . . . . .
3 ~ ~ ~ ~ . . . . . .
4 ~ ~ ~ ~ ~ . . . . .
5 ~ ~ ~ ~ ~ S . . . .
6 ~ ~ ~ ~ ~ ~ ~ . . .
7 ~ ~ ~ ~ ~ ~ ~ ~ . .
8 ~ ~ ~ ~ ~ ~ ~ ~ ~ .
9 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
0 1 2 3 4 5 6 7 8 9

(n)orth
(s)outh
(e)ast
(w)est

Which way, Captain? s

0 ~ . . . . . . . . .
1 ~ ~ . . . . . . . .
2 ~ ~ ~ . . . . . . .
3 ~ ~ ~ ~ . . . . . .
4 ~ ~ ~ ~ ~ . . . . .
5 ~ ~ ~ ~ ~ ~ . . . .
6 ~ ~ ~ ~ ~ S ~ . . .
7 ~ ~ ~ ~ ~ ~ ~ ~ . .
8 ~ ~ ~ ~ ~ ~ ~ ~ ~ .
9 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
0 1 2 3 4 5 6 7 8 9

(n)orth
(s)outh
(e)ast
(w)est

Which way, Captain? s

0 ~ . . . . . . . . .
1 ~ ~ . . . . . . . .
2 ~ ~ ~ . . . . . . .
3 ~ ~ ~ ~ . . . . . .
4 ~ ~ ~ ~ ~ . . . . .
5 ~ ~ ~ ~ ~ ~ . . . .
6 ~ ~ ~ ~ ~ ~ ~ . . .
7 ~ ~ ~ ~ ~ S ~ ~ . .
8 ~ ~ ~ ~ ~ ~ ~ ~ ~ .
9 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
0 1 2 3 4 5 6 7 8 9

(n)orth
(s)outh
(e)ast
(w)est

Introduction to C++ Programming Design and Implementation S. Trowbridge 2017


5

Which way, Captain? s

0 ~ . . . . . . . . .
1 ~ ~ . . . . . . . .
2 ~ ~ ~ . . . . . . .
3 ~ ~ ~ ~ . . . . . .
4 ~ ~ ~ ~ ~ . . . . .
5 ~ ~ ~ ~ ~ ~ . . . .
6 ~ ~ ~ ~ ~ ~ ~ . . .
7 ~ ~ ~ ~ ~ ~ ~ ~ . .
8 ~ ~ ~ ~ ~ S ~ ~ ~ .
9 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
0 1 2 3 4 5 6 7 8 9

(n)orth
(s)outh
(e)ast
(w)est

Which way, Captain? s

0 ~ . . . . . . . . .
1 ~ ~ . . . . . . . .
2 ~ ~ ~ . . . . . . .
3 ~ ~ ~ ~ . . . . . .
4 ~ ~ ~ ~ ~ . . . . .
5 ~ ~ ~ ~ ~ ~ . . . .
6 ~ ~ ~ ~ ~ ~ ~ . . .
7 ~ ~ ~ ~ ~ ~ ~ ~ . .
8 ~ ~ ~ ~ ~ ~ ~ ~ ~ .
9 ~ ~ ~ ~ ~ S ~ ~ ~ ~
0 1 2 3 4 5 6 7 8 9

(n)orth
(s)outh
(e)ast
(w)est

Which way, Captain? s

ARGGGH!!!...you have been lost at sea!!!

Introduction to C++ Programming Design and Implementation S. Trowbridge 2017

Anda mungkin juga menyukai