Question: Submarine Finder C++ Question WRITE CODE IN C++ Design a program that allows you to search for 5 submarines in a given ocean represented as
Submarine Finder C++ Question
WRITE CODE IN C++
Design a program that allows you to search for 5 submarines in a given ocean represented as 10 rows x 10 columns grid board. To play, enter the coordinates of the point in the ocean you wish to search (as row, col integer coordinates of the board). The program will tell you how far away you are from the closest submarine, if you are in relative close proximity.
****************************************************************************************************** You are the captain of the Simon, a submarine hunting ship. Your current mission is to find the five submarines that are lurking in the part of the ocean you are in.
To play, enter the coordinates of the point in the ocean you wish to drop an explosive depth charge. The program will tell you how far away you are from the closest live submarine, if you are in relative close proximity.
For example, here is a submarine (the s) located at a distance of 2 away from the drop point (the d): ..... ..... s.... s....
.d... .2...
..... .....
..... .....
The point where the explosive charge was dropped will be marked with a 2. Submarines do not move around. You can detect a submarine up to a distance of 9. If all submarines are out of range, the point will be marked with '0'.
If your selected drop point is directly on a submarine, you have discovered its location, and it will be marked by #. The marked point will remain there till the end of the game.
When you find a submarine, all points will be updated to locate the next closest submarine.
We will store the row number r and column number c in a Location structure representing the location of a submarine or the location of a depth_charge. We will be storing these locations in two vectors discussed later.
****************************************************************************************************** Create a GameBoard program for the logic of the game (Starter code included).
NOTE1 : You will need to maintain the following data types ocean: a 2D int [ROWS][COLS] array that would hold char symbols:
'.' for clear water 'S' for submarines (in DE_BUG mode)
'#' for sunk submarines 'n' where n is an integer representing the distance between a depth charge location to
the nearest live submarine.
subs_list: a vector to hold Location structures of all subs. drop_list : a vector to hold Location structures of where the user has dropped depth charges. You will need to create a Location structure with two int fields r for the row and c for column position. You will be storing Location structures in the subs_list and drop_list vectors.
Page 1 of 6
CISC 1600/1610 Final Project Prof. Kadri NOTE2: Use a bool constant DE_BUG set to true. When DE_BUG is true, show the location of
a live submarine with 'S'. When DE_BUG is false, do not show the locations of live submarines.
NOTE 3: int and char are primitive data types. As long as an int is a single digit, you can convert it to char as follows: if chr is declared as char and value is declared as int: chr = '0' + value;
This will be useful to place single digits as characters like '4' instead of 4 on the board.
Here is pseudo-code to find the distance from location (r,c) to the nearest submarine:
/* * Input: r, c are the depth charge coordinates. * Output: distance between depth charge and nearest live * submarine. * Note: abs()absolute function is defined in. */
int findShortestDistance(int r, int c) { int dist; // distance to a live submarine int sr, sc; // submarine coordinates
/* For each live submarine in the vector subs_list { calculate the distance between the current location(r,c) and the submarine's location (sr,sc):
dist = abs(r-sr)+ abs(c-sc); keep track of the shortest so far
} return the shortest distance */
}
Page 2 of 6
CISC 1600/1610 Final Project Prof. Kadri Your assignment is to implement the following (suggested) functions and may add more:
0123456789 0.......... 0 1.......... 1 2......S... 2 3S..S...... 3 4.......... 4 5.S........ 5 6......S... 6 7.......... 7 8.......... 8 9.......... 9
0123456789
printBoard() to print the board after each move. The above printout shows a game board after calling addSubs().
addSubs() // fills subs_list with 5 no duplicates submarine locations Each coordinate must be obtained randomly by using the Random class.
Location getDepthChargeLocation() // Asks the user to enter where a drop charge is to be dropped, and returns a valid drop location
bool isValidLocation(Location x) // returns true if location is inside the board and not that of a sunken sub or that of a depth charge.
bool inDropsList(Location x) // returns true if location is in drop_list
bool inSubsList(Location x) // returns true if location is in subs_list. Used to avoid adding two subs with the same location.
updateBoard() // calls findShortestDistance() to change the values of the distance to the nearest alive submarine for each drop_list location. These values will be used by printBoard() when displaying the board.
play() // gets from player a valid depth charge location // if that sinks a sub, display a message and remove it from the subs_list // add the location to the drop_list // updateBoard()
gameOver() // returns true if all 5 subs are sunk
The game loop inside the main() function should be:
while (!gameOver()) { printBoard();
play(); }
printBoard(); cout<<("!!! C O N G R A T U L A T I O N S !!!");
Page 3 of 6
CISC 1600/1610 Examples run (with DE_BUG set to true):
0123456789 0.......... 0 1.......... 1 2......S... 2 3S..S...... 3 4.......... 4 5.S........ 5 6......S... 6 7.......... 7 8.......... 8 9.......... 9
0123456789
Final Project
Prof. Kadri
Enter the r c coordinates (separated by a space) to hit: 2 3
0123456789 0.......... 0 1.......... 1 2...1..S... 2 3S..S...... 3 4.......... 4 5.S........ 5 6......S... 6 7.......... 7 8.......... 8 9.......... 9
0123456789
Enter the r c coordinates (separated by a space) to hit: 1 3
0123456789 0.......... 0 1...2...... 1 2...1..S... 2 3S..S...... 3 4.......... 4 5.S........ 5 6......S... 6 7.......... 7 8.......... 8 9.......... 9
0123456789
Enter the r c coordinates (separated by a space) to hit: 3 3 !! Boom: Sub sunk !!
0123456789 0.......... 0 1...4...... 1 2...3..S... 2
Page 4 of 6
CISC 1600/1610
Final Project
Prof. Kadri
3S..#...... 3 4.......... 4 5.S........ 5 6......S... 6 7.......... 7 8.......... 8 9.......... 9
0123456789
Notice: the '#' marking the sunk sub and how the numbers changed at (1,3) was 2 became 4 and (2,3) was 1 became 3
Enter the r c coordinates (separated by a space) to hit: 2 0
0123456789 0.......... 0 1...4...... 1 21..3..S... 2 3S..#...... 3 4.......... 4 5.S........ 5 6......S... 6 7.......... 7 8.......... 8 9.......... 9
0123456789
Enter the r c coordinates (separated by a space) to hit: 2 6 !! Boom: Sub sunk !!
0123456789 0.......... 0 1...5...... 1 21..4..#... 2 3S..#...... 3 4.......... 4 5.S........ 5 6......S... 6 7.......... 7 8.......... 8 9.......... 9
0123456789
Notice: how the numbers changed at (1,3) was 3 became 5 and (2,3) was 3 became 4 Enter the r c coordinates (separated by a space) to hit: 3 0 !! Boom: Sub sunk !!
0123456789
Page 5 of 6
0.......... 0 1...6...... 1 24..5..#... 2 3#..#...... 3 4.......... 4 5.S........ 5 6......S... 6 7.......... 7 8.......... 8 9.......... 9
0123456789
Notice: how the numbers changed at (1,3) was 5 became 6 and (2,3) was 4 became 5 (2,0) was 1 became 4
Enter the r c coordinates (separated by a space) to hit:
Important
Think through the conditional logic carefully. If you find yourself repeating blocks of code, stop and re-think the logic.
Follow an incremental approach in writing your program. In other words, write a few lines of code, make sure it compiles and executes as expected, then continue with more code.
If the program compiles but does not work as expected, consider using cout statements to display the value of variables in order to identify the errors.
For full credit be sure to:
? Include a descriptive Comment Block.
? Use correct indentation and alignment.
? Use descriptive identifiers for variable names as well as appropriate data types.
? Use blank lines to separate the code into appropriate blocks.
? Include comments to help others understand your program, and help yourself think!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
