C++代写:CS27355 Battleship

根据提供的游戏框架,代写完成经典游戏Battleship,通过测试即可。

Background

For this project we’re going to play a game, Battleship. If you want you can even play a game, or 1000, online. 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:

  1. A ship that is trying to be placed and the starting position is off the board.
  2. A ship that is trying to be placed and goes off the board during placement.
  3. A ship that overlaps with a ship that is already placed on the board.
  4. A ship in the file that is not one of the 5 ships we place.
  5. A direction that is not one of the 4 directions we have.

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

  1. You must use a 10x10 array of characters (or integers or other built-in type) for the board. You may have a 3D array if you wish. It must be at least a 2D Array.
  2. You may not use the STL. You may use string, ifstream, ofstream, and the other items we have discussed.
  3. You may not use classes to implement this.
  4. You must implement each command as a function, e.g. a try function, a load function, a display function.
  5. You may not use structs or other aggregate user defined data types, e.g. classes, structs, tuples, etc.
  6. You must declare the following function void battleship(string commands, string output); in a file named battleship.h
  7. You may not use global variables, global constants are fine.
  8. You must follow the Code Style Guidelines in Canvas.

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:

  1. A is an aircraft carrier and is 5 spaces long.
  2. B is a battleship and is 4 spaces long.
  3. C is a carrier and is 3 spaces long.
  4. S is a submarine and is 3 spaces long.
  5. D is a destroyer and is 2 spaces long.

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.

Errors

Way at the top of this file there are 5 errors described:

  1. A ship that is trying to be placed and the starting position is off the board.
  2. A ship that is trying to be placed and goes off the board during placement.
  3. A ship that overlaps with a ship that is already placed on the board.
  4. A ship in the file that is not one of the 5 ships we place.
  5. A direction that is not one of the 4 directions we have.

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.

  1. A try that is not on the board.
    (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.

  1. Bad row/column: K, 15 and on the next line: Error: contained invalid ship. (Assuming the input board file contained a ship located at K, 15)
  2. Bard row/column: K, 10 and on the next line: Error: contained invalid ship. (K, 10 is where the ship went off the board.)
  3. Board is taken at 2, 5 and on the next line: Error: contained invalid ship. (2, 5 is where the two ships overlapped.)
  4. Bad ship: W and on the next line: Error: contained invalid ship. (W is the ship in the file that was not one of the 5 ships we have.)
  5. Bad direction: z and on the next line: Error: contained invalid ship. (z is the direction in the file that was not one of the 4 directions we have.)
  6. (Assuming that a command of try O 19) Bad try: O 19

Grading

To grade the project, zip up your battleship.h and battleship.cpp and turn them in to Web-CAT. You will have 10 submissions for this project.