Battleship C++ Class Implementation Description In this assignment (HW2) and in
ID: 3556804 • Letter: B
Question
Battleship C++ Class Implementation
Description
In this assignment (HW2) and in the next assignment (HW3), you will write an object-oriented
implementation of the battleship board game.
Battleship game
The game is played on a board consisting of 100 cells arranged as a 10x10 square. Ten ships are
placed on the board. There are four kinds of ships. Each ship occupies a number of cells that
depends on the size of the ship. The ship types are
1) Aircraft carrier, size=5
2) Battleship, size=4
3) Cruiser, size=3
4) Destroyer, size=2
A ship occupies contiguous cells arranged as a rectangle of width=1 and height=size, or
width=size and height=1, i.e. the orientation of the rectangle can be horizontal or vertical. Ships
cannot overlap and cannot touch each other (either sideways or diagonally).
Assignment
The goal is to implement classes representing ships. These classes will be later used to implement the full battleship board game.
The class Ship is an abstract base class from which four classes are derived:
AircraftCarrier, BattleShip, Cruiser and Destroyer. A detailed specification of
the class Ship is given below. You are given the header file Ship.h (at
http://www.cs.ucdavis.edu/~fgygi/ecs40/homework/hw2). Your task is to write the
implementation file Ship.cpp containing the implementation of all classes. You are not
allowed to modify the file Ship.h (Do not insert your SID in it since we will use the cmp
command to check that the file is the same as the original). Your class implementation file
Ship.cpp should compile without warning using the command
$ g++ -Wall
Explanation / Answer
#include #include using namespace std; const int BOARD_WIDTH = 15; const int BOARD_HEIGHT = 10; const int SHIP_TYPES = 5; const char isWATER = 247; //ASCII Character Code const char isHIT = 'X'; const char isSHIP = 'S'; const char isMISS = '0'; struct POINT { //A location on the grid defined //by X(horizontal) Y(vertical) coordinates int X; int Y; }; struct SHIP { //Ship name string name; //Total points on the grid int length; //Coordinates of those points POINT onGrid[5]; //0-4 max length of biggest ship //Whether or not those points are a "hit" bool hitFlag[5]; }ship[SHIP_TYPES]; struct PLAYER { char grid[BOARD_WIDTH][BOARD_HEIGHT]; }player[3]; //Ignore player 0, just using player's 1 & 2 enum DIRECTION {HORIZONTAL,VERTICAL}; struct PLACESHIPS { DIRECTION direction; SHIP shipType; }; bool gameRunning = false; //Functions void LoadShips(); void ResetBoard(); void DrawBoard(int); PLACESHIPS UserInputShipPlacement(); bool UserInputAttack(int&,int&,int); bool GameOverCheck(int); int main() { LoadShips(); ResetBoard(); //"PLACE SHIPS" phase of game //Loop through each player... for (int aplyr=1; aplyrRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.