Week #3 OO Design Our battleships game needs several different kinds of ship in
ID: 3764956 • Letter: W
Question
Week #3 OO Design Our battleships game needs several different kinds of ship in the grid Create a base class called Ship Create child classes for each of the ship types in the game (https://en.wikipedia.org/wiki/Battleship_(game)#Description) that inherit from Ship Ships should have the following properties • A private array of positions o A Position is composed of an X and Y coordinate – you should create a struct to encapsulate this • A read only length o The constructor for each inherited type should set this to the correct length for the ship type • A read only color to be drawn in o The constructor for each inherited type should set this to a different ConsoleColor for drawing • A flag called Sunk o This should default to false for all ships • A property called IsBattleship o This property should be overridden by each child type. Only the BattleShip should return true. • A method called Reset o This method should reset the members to their empty defaults • A method called Place(Position start, Direction direction) o Direction should be an enumeration of either Horizontal or Vertical o This will complete the Position array with the set of coordinates that the ship is covering e.g Place(new Position(1, 1), Direction.Horizontal) on a patrol boat will fill the array with the points (1, 1) and (2, 1) You should choose the correct types and access modifiers for each type. Create a test program that can run code such as the following: AircraftCarrier ac = new AircraftCarrier(); Console.WriteLine(ac.IsBattleShip); ac.Place(1, 1); ac.Reset(); You should write additional code to test all of the methods and ship types. Given that the positions array is private how can you test that the values are correct?
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
const int BOARD_WIDTH = 15;
const int BOARD_HEIGHT = 10;
const int SHIP_TYPES = 5;
const char isWATER = 247; // Code for ASCII Character
const char isHIT = 'X';
const char isSHIP = 'S';
const char isMISS = '0';
struct POINT {
int X;
int Y;
};
struct SHIP {
string name;
int length;
POINT onGrid[5];
bool hitFlag[5];
}ship[SHIP_TYPES];
struct PLAYER {
char grid[BOARD_WIDTH][BOARD_HEIGHT];
}player[3];
enum DIRECTION {HORIZONTAL,VERTICAL};
struct PLACESHIPS {
DIRECTION direction;
SHIP shipType;
};
bool gameRunning = false;
void LoadShips();
void ResetBoard();
void DrawBoard(int);
PLACESHIPS UserInputShipPlacement();
bool UserInputAttack(int&,int&,int);
bool GameOverCheck(int);
int main()
{
LoadShips();
ResetBoard();
for (int aplyr=1; aplyr<3; ++aplyr)
{
for (int thisShip=0; thisShip<SHIP_TYPES; ++thisShip)
{
system("cls");
DrawBoard(aplyr);
cout << " ";
cout << "INSTRUCTIONS (Player " << aplyr << ") ";
cout << "You are about to place your ships. Format should be: ";
cout << "Facing (0:Horizontal,1:Vertical), X (top-row) coords, Y (left-side) coords ";
cout << "Example: 0 7 2 This would place a ship beginning at X:7 Y:2 going horizontal ";
cout << "Ship to place: " << ship[thisShip].name << " which has a length of " << ship[thisShip].length << " ";
cout << "Where do you want it placed? ";
PLACESHIPS aShip;
aShip.shipType.onGrid[0].X = -1;
while (aShip.shipType.onGrid[0].X == -1)
{
aShip = UserInputShipPlacement();
}
aShip.shipType.length = ship[thisShip].length;
aShip.shipType.name = ship[thisShip].name;
player[aplyr].grid[aShip.shipType.onGrid[0].X][aShip.shipType.onGrid[0].Y] = isSHIP;
for (int i=1; i<aShip.shipType.length; ++i)
{
if (aShip.direction == HORIZONTAL){
aShip.shipType.onGrid[i].X = aShip.shipType.onGrid[i-1].X+1;
aShip.shipType.onGrid[i].Y = aShip.shipType.onGrid[i-1].Y; }
if (aShip.direction == VERTICAL){
aShip.shipType.onGrid[i].Y = aShip.shipType.onGrid[i-1].Y+1;
aShip.shipType.onGrid[i].X = aShip.shipType.onGrid[i-1].X; }
player[aplyr].grid[aShip.shipType.onGrid[i].X][aShip.shipType.onGrid[i].Y] = isSHIP;
}
//Loop back through each ship type
}
//Loop back through each player
}
gameRunning = true;
int thisPlayer = 1;
do {
int enemyPlayer;
if (thisPlayer == 1) enemyPlayer = 2;
if (thisPlayer == 2) enemyPlayer = 1;
system("cls");
DrawBoard(enemyPlayer);
bool goodInput = false;
int x,y;
while (goodInput == false) {
goodInput = UserInputAttack(x,y,thisPlayer);
}
if (player[enemyPlayer].grid[x][y] == isSHIP) player[enemyPlayer].grid[x][y] = isHIT;
if (player[enemyPlayer].grid[x][y] == isWATER) player[enemyPlayer].grid[x][y] = isMISS;
int aWin = GameOverCheck(enemyPlayer);
if (aWin != 0) {
gameRunning = false;
break;
}
thisPlayer = (thisPlayer == 1) ? 2 : 1;
} while (gameRunning);
system("cls");
cout << " CONGRATULATIONS!!! PLAYER " << thisPlayer << " HAS WON THE GAME! ";
system("pause");
return 0;
}
bool GameOverCheck(int enemyPLAYER)
{
bool winner = true;
for (int w=0; w<BOARD_WIDTH; ++w){
for (int h=0; h<BOARD_HEIGHT; ++h){
if (player[enemyPLAYER].grid[w][h] = isSHIP)
{
winner = false;
return winner;
}
}}
return winner;
}
bool UserInputAttack(int& x, int& y, int theplayer)
{
cout << " PLAYER " << theplayer << ", ENTER COORDINATES TO ATTACK: ";
bool goodInput = false;
cin >> x >> y;
if (x<0 || x>=BOARD_WIDTH) return goodInput;
if (y<0 || y>=BOARD_HEIGHT) return goodInput;
goodInput = true;
return goodInput;
}
PLACESHIPS UserInputShipPlacement()
{
int d, x, y;
PLACESHIPS tmp;
tmp.shipType.onGrid[0].X = -1;
cin >> d >> x >> y;
if (d!=0 && d!=1) return tmp;
if (x<0 || x>=BOARD_WIDTH) return tmp;
if (y<0 || y>=BOARD_HEIGHT) return tmp;
tmp.direction = (DIRECTION)d;
tmp.shipType.onGrid[0].X = x;
tmp.shipType.onGrid[0].Y = y;
return tmp;
}
void LoadShips()
{
ship[0].name = "Cruiser"; ship[0].length = 2;
ship[1].name = "Frigate"; ship[1].length = 3;
ship[2].name = "Submarine"; ship[2].length = 3;
ship[3].name = "Escort"; ship[3].length = 4;
ship[4].name = "Battleship"; ship[4].length = 5;
}
void ResetBoard()
{
for (int plyr=1; plyr<3; ++plyr)
{
for (int w=0; w<BOARD_WIDTH; ++w){
for (int h=0; h<BOARD_HEIGHT; ++h){
player[plyr].grid[w][h] = isWATER;
}}
}
}
void DrawBoard(int thisPlayer)
{
cout << "PLAYER " << thisPlayer << "'s GAME BOARD ";
cout << "---------------------- ";
cout << " ";
for (int w=0; w<BOARD_WIDTH; ++w) {
if (w < 10)
//Numbers only 1 character long, add two spaces after
cout << w << " ";
else if (w >= 10)
//Numbers 2 characters long, add only 1 space after
cout << w << " ";
}
cout << " ";
for (int h=0; h<BOARD_HEIGHT; ++h){
for (int w=0; w<BOARD_WIDTH; ++w){
if (w==0) cout << h << " ";
if (w<10 && w==0) cout << " ";
if (gameRunning == false) cout << player[thisPlayer].grid[w][h] << " ";
if (gameRunning == true && player[thisPlayer].grid[w][h] != isSHIP)
{cout << player[thisPlayer].grid[w][h] << " ";}
else if (gameRunning == true && player[thisPlayer].grid[w][h] == isSHIP)
{cout << isWATER << " ";}
if (w == BOARD_WIDTH-1) cout << " ";
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.