The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
For this project we're going to play a game, Battleship (Links to an external site.)). If you want you can even play a game, or 1000, online (Links to an external site.). I'll wait for you to come back and keep reading.
Welcome back. So we're not exactly going to be playing like that. This will be a 1 sided game where you are trying to sink someone else's ships. You won't have any. The focus of the project is on a 2D array and so we'll be focusing mostly on that aspect. You'll be reading information from files and playing the game in that way.
Details
For this game, we'll be using a 10x10 array of characters. The board will be where the ships are placed. The top left corner of the array is 0,0. The lower right corner is 9,9. To play the game, we'll be given an input file that is a series of commands. The first command will be the name of the board file that contains the ship information. The rest of the file will be a series of try commands or display commands. I'll put a sample of the command file at the end since it can get long.
The board will be a set of 5 lines. The lines will indicate a ship, it's place on the board where it starts and the direction the ship is pointing, left, right, up or down. Errors can occur in the board file. There are 5 errors that can and will occur. They are as follows:
If any of these errors occur, the game ceases and an error is displayed. The messages and the errors will be listed below and in the samples.
Assuming we get past the loading phase, then we will start processing the tries that occur in the file. The try simply is where you want to drop a bomb. If you hit a ship, then an X is stored and if you miss, then an O is stored. Once all the ships are sunk, then the game is over. The game is also over if the input runs out before all the ships are sunk. When the game is over you will say the game is over and display the board (as if an implied display command were given).
The input to the battleship function will be the name of a command file. The command file will contain the board file to load and then a series of commands.
Once the input is over, assuming no errors in loading and during play, the game should be over and the board is displayed to show the hits and misses. If the input ends before the game is over, then we still display Game Over and the board with as much of the hits and misses as we had. If there are errors during loading, then we don't show the Game Over message nor the board.
Requirements
Input
This is a sample board file that contains the correct ships and directions:
D A 3 l B H 2 r C D 3 u A F 3 r S B 1 d
The ships are as follows:
The spaces for the rows are given as letters and are from A-J. The A row corresponds to the top row, index 0. The J row corresponds to the bottom row, index 9. The columns are given 1-10. The 1 column is the first column on the left or index 0. The 10 column is the last column on the right and is the 9 index.
Here's a sample of the input file for the commands for the game:
Board: board10.txt try G 7 try H 5 try E 7 try G 9 try D 7 try I 5 try E 4 try D 10 try A 2 try A 6 try G 2 try C 8 try J 4 try C 7 try E 10 try D 4 try F 3 try J 5 try F 5 try A 8 try B 2 try J 6 try I 2 try D 8 try I 8 try B 5 try B 9 try I 10 try G 1 try B 10 try D 6 try I 6 try B 3 try J 10 try B 6 try J 3 try C 2 try C 4 try A 4 try A 1 try J 9 try J 8 try I 9 try D 1 try E 9 try H 2 try F 9 try A 9 try C 1 try F 8 try G 10 try A 3 try C 5 try H 3 try I 4 try D 9 try G 4 try J 7 try F 10 try E 8 try A 5 try F 2 try H 1 try G 6 try E 3 try C 9 try F 4 try H 4 try F 7 try H 9 try H 10 try B 7 try I 7 try E 6 try D 3 try F 6 try G 3 try C 3 try C 6 try C 10 try F 1 try E 1 try D 2 try J 1 try G 8 try D 5 try B 1 try H 6 try H 8 try G 5 try E 5 try I 3 try B 8 try H 7 try J 2 try B 4 try A 7 try A 10 try I 1 try E 2
If the board in the sample above is board10.txt and the commands above are given, this is the output:
Command: try #1: G 7
Miss
Command: try #2: H 5
Hit
Command: try #3: E 7
Miss
Command: try #4: G 9
Miss
Command: try #5: D 7
Miss
Command: try #6: I 5
Miss
Command: try #7: E 4
Miss
Command: try #8: D 10
Miss
Command: try #9: A 2
Hit
Command: try #10: A 6
Miss
Command: try #11: G 2
Miss
Command: try #12: C 8
Miss
Command: try #13: J 4
Miss
Command: try #14: C 7
Miss
Command: try #15: E 10
Miss
Command: try #16: D 4
Miss
Command: try #17: F 3
Hit
Command: try #18: J 5
Miss
Command: try #19: F 5
Hit
Command: try #20: A 8
Miss
Command: try #21: B 2
Miss
Command: try #22: J 6
Miss
Command: try #23: I 2
Miss
Command: try #24: D 8
Miss
Command: try #25: I 8
Miss
Command: try #26: B 5
Miss
Command: try #27: B 9
Miss
Command: try #28: I 10
Miss
Command: try #29: G 1
Miss
Command: try #30: B 10
Miss
Command: try #31: D 6
Miss
Command: try #32: I 6
Miss
Command: try #33: B 3
Hit
Command: try #34: J 10
Miss
Command: try #35: B 6
Miss
Command: try #36: J 3
Miss
Command: try #37: C 2
Miss
Command: try #38: C 4
Miss
Command: try #39: A 4
Miss
Command: try #40: A 1
Miss
Command: try #41: J 9
Miss
Command: try #42: J 8
Miss
Command: try #43: I 9
Miss
Command: try #44: D 1
Hit
Command: try #45: E 9
Miss
Command: try #46: H 2
Hit
Command: try #47: F 9
Miss
Command: try #48: A 9
Miss
Command: try #49: C 1
Hit
Command: try #50: F 8
Miss
Command: try #51: G 10
Miss
Command: try #52: A 3
Hit
You sunk my destroyer
Command: try #53: C 5
Miss
Command: try #54: H 3
Hit
Command: try #55: I 4
Miss
Command: try #56: D 9
Miss
Command: try #57: G 4
Miss
Command: try #58: J 7
Miss
Command: try #59: F 10
Miss
Command: try #60: E 8
Miss
Command: try #61: A 5
Miss
Command: try #62: F 2
Miss
Command: try #63: H 1
Miss
Command: try #64: G 6
Miss
Command: try #65: E 3
Miss
Command: try #66: C 9
Miss
Command: try #67: F 4
Hit
Command: try #68: H 4
Hit
You sunk my battleship
Command: try #69: F 7
Hit
Command: try #70: H 9
Miss
Command: try #71: H 10
Miss
Command: try #72: B 7
Miss
Command: try #73: I 7
Miss
Command: try #74: E 6
Miss
Command: try #75: D 3
Hit
Command: try #76: F 6
Hit
You sunk my aircraft carrier
Command: try #77: G 3
Miss
Command: try #78: C 3
Hit
You sunk my cruiser
Command: try #79: C 6
Miss
Command: try #80: C 10
Miss
Command: try #81: F 1
Miss
Command: try #82: E 1
Miss
Command: try #83: D 2
Miss
Command: try #84: J 1
Miss
Command: try #85: G 8
Miss
Command: try #86: D 5
Miss
Command: try #87: B 1
Hit
You sunk my submarine
Game Over
1 2 3 4 5 6 7 8 9 10
A O X X O O O . O O .
B X O X . O O O . O O
C X O X O O O O O O O
D X O X O O O O O O O
E O . O O . O O O O O
F O O X X X X X O O O
G O O O O . O O O O O
H O X X X X . . . O O
I . O . O O O O O O O
J O . O O O O O O O O
Samples
samples.zip (Links to an external site.)
Errors
Way at the top of this file there are 5 errors described:
So there's really a 6th one too. During the playing of the game if a try is off the board, then you simply indicate that it's off the board and you keep going. So let's list that one too.
(I know it says 1, but it's really #6.)
Okay so if we have 6 errors, below is a list of the messages that will come out for each try of error.
-----------