Question: #include BattleBoat.h #include using namespace std; BattleBoat::BattleBoat(unsigned int length) : length { min(MAX_LENGTH, length) } { } unsigned int BattleBoat::getLength() const { return length; }




#include "BattleBoat.h" #include
BattleBoat::BattleBoat(unsigned int length) : length { min(MAX_LENGTH, length) } { }
unsigned int BattleBoat::getLength() const { return length; }
unsigned int BattleBoat::getStartX() const { return startX; }
unsigned int BattleBoat::getStartY() const { return startY; }
bool BattleBoat::isVertical() const { return vertical; }
ostream& BattleBoat::printSegment(ostream& out, unsigned int x, unsigned int y) const { if (length == 1) { // Single cell boat out
out "; } else if (vertical) { if (y == startY) { // Top end cap out
if (hits[0]) { out
out
if (hits[length - 1]) { out
out
if (hits[y - startY]) { out
out
if (hits[0]) { out
out
if (hits[length - 1]) { out
out "; } else { // Middle horizontal segment out
if (hits[x - startX]) { out
out
return out; }
void BattleBoat::setStartX(unsigned int startX) { this->startX = startX; }
void BattleBoat::setStartY(unsigned int startY) { this->startY = startY; }
void BattleBoat::setVertical(bool vertical) { this->vertical = vertical; }
bool BattleBoat::intersects(const BattleBoat& other) const { // TODO: Implement this function return false; }
bool BattleBoat::isHit(unsigned int x, unsigned int y) const { // TODO: Implement this function return false; }
void BattleBoat::hit(unsigned int x, unsigned int y) { // TODO: Implement this function }
bool BattleBoat::isSunk() const { // TODO: Implement this function return false; }
#include "BattleBoatBoard.h" #include
BattleBoatBoard::BattleBoatBoard(unsigned int* boatLengths, unsigned int boatCount) { // TODO: Write the constructor here. }
const BattleBoat* BattleBoatBoard::getBoat(unsigned int x, unsigned int y) const { // TODO: Return a pointer to a boat if one exists at the coordinates (x, y). return nullptr; }
BattleBoat * BattleBoatBoard::getBoat(unsigned int x, unsigned int y) { // TODO: Return a pointer to a boat if one exists at the coordinates (x, y). return nullptr; }
#include "BattleBoatsGame.h" #include "BattleBoat.h" #include
const bool BattleBoatsGame::isRevealed(unsigned int x, unsigned int y) const { // TODO: implement this function return false; }
bool BattleBoatsGame::shot(unsigned int x, unsigned int y) { // TODO: implement this function return false; }
unsigned int BattleBoatsGame::getBoatsSunk() const { // TODO: implement this function return 0; }
Overview In this section, you will implement the following functions, to be submitted as source code via Canvas: In the BattleBoat class: bool isHit(unsigned int x, unsigned int y) const; void hit(unsigned int x, unsigned int y); bool isSunk() const; In the BattleBoatsGame class: bool shot(unsigned int x, unsigned int y); const bool isRevealed(unsigned int x, unsigned int y) const; unsigned int getBoats Sunk() const; In addition, you will develop a user interface for playing a game of Battleboats that must be checked off in person by the instructor or an SI. Backend functions The actual game progresses as the human player takes shots at the computer's board. To implement this game mechanic, you will need to implement two more functions for BattleBoat: bool isHit(unsigned int x, unsigned int y) const; void hit(unsigned int x, unsigned int y); isHit() should take the x-and y-coordinates of a location on the game board and return true if the boat has been hit at that location, or false otherwise. You will need to convert these global coordinates to the index of a segment in the boat (if the boat covers the coordinates specified). If the boat does not cover the coordinates specified, return false. hit() should take the x-and y-coordinates of a location on the game board and mark that the boat has been hit at that location (if the boat covers the coordinates specified). As with isHit(), you will need to convert the global coordinates to the index of a segment in the boat. Once you have written those two BattleBoat functions, you can start working on the BattleBoatsGame class. This class already has a constructor, some "helper functions," and a couple variables defined, including a reference to an instance of BattleBoatBoard that you should use for the game. First, you need to write the definition of a function called shot() in the BattleBoatsGame class. bool shot(unsigned int x, unsigned int y); This function should attempt to hit a boat at the coordinates (x, y) and return a value as follows: If there is no boat at the location, or the user has already attacked that location, or the location is out of bounds, shot() should return false. If there is a boat at the location and the location has not been previously attacked, shot() should return true. You should also increment the turnCount variable maintained by BattleBoatsGame. In addition, shot() should somehow mark the square that was targeted as "revealed" (you may want to use a 2D array to keep track of this). However you keep track of this, implement isRevealed() to return true if the specified coordinates have been revealed, or false otherwise: const bool isRevealed (unsigned int x, unsigned int y) const; If xory is out of bounds, this function should just return false. Once you have implemented shots and hits, you will need to implement functions to keep track of which boats have been sunk. In the BattleBoat class, you should implement the issunk() function that has been declared: bool isSunk() const; This function should return true if every segment in the boat has been hit (each square on the board that the ship occupies), or false otherwise (if any square that it occupies has not been hit). Once you have done that, you should be able to use BattleBoat::isSunk() to implement a function called getBoatsSunk() in the BattleBoatsGame class that counts the number of boats that are sunk: unsigned int getBoats Sunk() const; User interface To finish part 2, you will need to extend your user interface from part 1 to allow the user to play the game. As with part 1, in addition to submitting your code to Canvas, you must get the user interface checked off in person by either the instructor or an SI. Normally, once the board is created, you would hide it from the player so they can't cheat. However, for the sake of testing and grading), you need to offer the option of printing the "cheat" board. This feature is called "debug mode." After setting up the board using your interface in part 1, the game should ask the user if they want to see the board. If they respond "Y", then you should print out the board as you did in part 1. If the user does not indicate that they want to see the cheat board, do not print it. Once you've established whether to play in debug mode, you enter the main game loop. Each turn, the user will be prompted for a location x,y to attack on the board. You can assume that the user will enter (x,y) coordinates in the format: xy (that is, without parentheses or a comma. If there is no boat at the location, or if the user has already attacked that location or the location is out of bounds, print "miss". If there is a boat, print "hit". If the boat sinks, print "sunk". Do not print "hit" again if the user has already "hit" this location. Example: Suppose just one boat is placed with the coordinates (0,0), (0, 1), (0, 2). Turn 1: The user selects (1, 0) and miss is printed Turn 2: The user selects (0,0) and hit is printed Turn 3: The user selects (0,0) again; miss is printed Turn 4: The user selects (0, 1) and hit is printed Turn 5: The user selects (-1,0), which is out of bounds. miss is printed. Turn 6: The user selects (0, 2) and sunk is printed. The game ends because the last boat has sunk. The total number of turns is 6. Before each turn, a partially visible representation of the board should be printed to the screen. For your convenience, a stream insertion operator (
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
