C++ Help!! Beginner programing please! No global variables! For this project you
ID: 3905703 • Letter: C
Question
C++ Help!! Beginner programing please! No global variables!
For this project you will write a couple of classes that could be used to make a program that plays the game BattleshipLinks to an external site..
The Ship class represents a ship that has:
a name (e.g. "my destroyer", "my submarine", "Boaty McBoatface")
a length (the number of squares it occupies)
a damage (how many of its squares have been hit)
a constructor that takes as parameters (in this order): the Ship's name and the Ship's length, which will be used to initialize the corresponding data members. The damage should be initialized to zero.
getMethods for each data member (getName, getLength, getDamage)
a method called takeHit that increments a Ship's damage by one
The BBoard class represents a 10x10 grid that holds some number of Ships. It should have:
a 10x10 array of bools (for keeping track of what squares have been attacked)
a 10x10 array of Ship-pointers (for keeping track of which Ships occupy which squares)
a variable that keeps track of the number of ships that remain un-sunk
a constructor that initializes each element of the boolArray to false and each element of the Ship-pointer array to NULL (or nullptr if you prefer)
a method called getAttacksArrayElement that takes a row and column (in that order) and returns the element at that position of the bool array
a method called getShipsArrayElement that takes a row and column (in that order) and returns the element at that position of the ships array
a method called getNumShipsRemaining that returns how many ships remain un-sunk
a method called placeShip that takes as parameters (in this order): the address of a Ship object, the row and column of the square of the Ship that is closest to (0, 0), and the Ship's orientation (either 'R' if its squares occupy the same row or 'C' if its squares occupy the same column). I give a couple of examples at the end of the specifications.
This method will set the elements of the array that the Ship occupies to hold the address of the Ship. If a Ship would not fit entirely on the Board, or if it would overlap any previously placed ships, the ship should not be added to the Board and the method should return false. Otherwise, the ship should be placed on the Board, the number of un-sunk ships should be incremented, and the method should return true.
a method called attack that takes as parameters the row and column of the attack (in that order). If the attack hits a Ship, you should:
record the attack in the bool array
if that square has not been hit before, you should call that Ship's takeHit method
if all of a Ship's squares have been hit, you should print "They sank [insert name of ship here]!" and decrement the number of ships that remain un-sunk (you should only do these once for any ship)
return true (even if that square was previously hit)
If the attack is not a hit, you should record the attack in the bool array and return false.
a method called allShipsSunk that returns true if all ships on the Board have been sunk, but returns false otherwise.
The files must be named Ship.hpp, Ship.cpp, BBoard.hpp, and BBoard.cpp.
Example of the placeShip method - if we have the following values for the parameters:
a Ship that has a length of 4
the row and column are 6 and 8 respectively
the orientation is 'C'
Then the ship would occupy the following squares:
If we have the following values for the parameters:
a Ship that has a length of 3
the row and column are 4 and 2 respectively
the orientation is 'R'
Then the ship would occupy the following squares:
Explanation / Answer
// CODE
// Main.c
#include <iostream>
#include"BBoard.h"
using namespace std;
int main() {
BBoard bb; // create a board
// create ships
Ship s1("Cruiser", 4);
Ship s2("Destroyer", 3);
cout<<"Placing 2 Ships in the Board"<<endl;
bb.placeShip(s1, 6, 8, 'C');
bb.placeShip(s2, 4, 2, 'R');
bb.displayBoard(); // display board
cout<<" Number of Unsunk ships : "<<bb.getNumShipsRemaining()<<endl;
// attack a ship
cout<<endl<<"Attacking ships on coordinates (4,2), (4, 3), (4, 4)"<<endl;
bb.attack(4, 2);
bb.attack(4, 3);
bb.attack(4, 4);
cout<<" Number of Unsunk ships : "<<bb.getNumShipsRemaining()<<endl;
}
// Ship.h
#ifndef __SHIP__
#define __SHIP__
#include<iostream>
using namespace std;
// Declaration of class Ship
class Ship {
private:
string name;
int length;
int damage;
public:
Ship(string name, int length);
Ship();
string getName();
int getLength();
int getDamage();
void takeHit();
bool isCompletelyHit();
// assignment Operator overloading
void operator=(const Ship &s) {
this->name = s.name;
this->length = s.length;
this->damage=s.damage;
}
};
#endif // __SHIP__
// Ship.cpp
#include"Ship.h"
using namespace std;
// implementation of Ship
// parameterized constructor
Ship::Ship(string name, int length) {
this->name = name;
this->length = length;
damage = 0;
}
// Default Constructor for ship
Ship::Ship() {
this->name = "No Ship";
this->length = 0;
damage = 0;
}
// returns the name of the ship
string Ship::getName() {
return name;
}
// returns the length of the ship
int Ship::getLength() {
return length;
}
// returns the damage done to the ship
int Ship::getDamage() {
return damage;
}
// takes a hit and increases the damage done
void Ship::takeHit() {
cout<<name<<" got hit ";
this->damage++;
}
// returns true if the damage done is equal to the length of the ship
bool Ship::isCompletelyHit() {
return this->damage == this->length;
}
// BBoard.h
#ifndef __BBOARD__
#define __BBOARD__
#include"Ship.h"
using namespace std;
// declaration of class BBoard
class BBoard {
private:
const static int CSIZE = 10;
const static int RSIZE = 10;
bool attacked[RSIZE][CSIZE]; // boolean array to keep the status of attack on a cell
Ship ***ships; // a pointer array of ships(this is a 2D pointer array to ship class)
int unsunkShips; // number of ships that are unsunk
public:
BBoard();
bool getAttacksArrayElement(int r, int c);
Ship getShipArrayElement(int r, int c);
int getNumShipsRemaining();
bool placeShip(Ship& ship, int r, int c, char orientation);
void attack(int r, int c);
bool allShipsSunk();
void displayBoard();
};
#endif // __BBOARD__
// BBoard.cpp
#include"BBoard.h"
using namespace std;
// Constructor of BBoard class
BBoard::BBoard() {
ships = new Ship**[RSIZE]; // create a 2D array of Ship pointers
for(int i = 0; i < RSIZE; i++)
ships[i] = new Ship*[CSIZE];
// initialize attacked array with false values
// and ships array with null pointers
for(int i = 0; i < RSIZE; i++)
for(int j = 0; j < CSIZE; j++){
attacked[i][j] = false;
Ship s;
ships[i][j] = NULL; // first make all cells null
}
unsunkShips = 0; // set number of unsunk ships to 0
}
bool BBoard::getAttacksArrayElement(int r, int c) {
if(r < RSIZE && r >= 0 && c < CSIZE && c >= 0) // if r and c are valid
return attacked[r][c]; // return the value at rth row and cth col
else
return false; // else return false
}
Ship BBoard::getShipArrayElement(int r, int c) {
if(r < RSIZE && r >= 0 && c < CSIZE && c >= 0)// if r and c are valid
return *ships[r][c]; // return the value at rth row and cth col
else
return Ship(); // else return a default ship
}
int BBoard::getNumShipsRemaining() {
return unsunkShips;
}
bool BBoard::placeShip(Ship& ship, int r, int c, char orientation) {
if(r < RSIZE && r >= 0 && c < CSIZE && c >= 0) { // if r and c are valid
if(orientation == 'R' && c+ship.getLength() <= CSIZE) { // ship is horizontal and length of the ship fits the board
for(int i = c; i < c+ship.getLength();i++ ) // for each cell it occupies
ships[r][i] = &ship; // assign this ship's reference to that cell
unsunkShips++; // increment the count of unsank ships
return true;
}
else if(orientation == 'C' && r+ship.getLength() <= RSIZE) { // ship is vertical and length of the ship fits the board
for(int i = r; i < r+ship.getLength(); i++) // for each cell it occupies
ships[i][c] = &ship;// assign this ship's reference to that cell
unsunkShips++; // increment the count of unsank ships
return true;
} else
return false;
}
return false;
}
void BBoard::attack(int r, int c) {
if(r < RSIZE && r >= 0 && c < CSIZE && c >= 0) { // if r and c are valid
if(!attacked[r][c]) { // and that cell is not previously attacked
attacked[r][c] = true; // make it as a attacked cell
ships[r][c]->takeHit(); // call take hit method on the Ship's object that the cell is pointing to
if(ships[r][c]->isCompletelyHit()) { // if the ship is completely damaged
cout<<"They Hit "<<ships[r][c]->getName()<<endl; // print a message for the user
unsunkShips--; // reduce the number of unsank ships by 1
}
}
}
}
bool BBoard::allShipsSunk() {
return unsunkShips == 0;
}
void BBoard::displayBoard() {
cout<<" Displaying board ";
cout<<" ";
for(int i = 0; i < CSIZE; i++)
cout<<(i)<<" ";
cout<<endl;
Ship s; // empty ship
for(int i = 0; i < RSIZE; i++) {
cout<<i<<" ";
for(int j = 0; j < CSIZE; j++) {
if(ships[i][j] == NULL) {
cout<<" ";
} else {
cout<<"X ";
}
}
cout<<endl;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.