Question: Need help writing a battle ship program for c++ (it is for a 120 class so it's not very advanced). I have function prototypes with
Need help writing a battle ship program for c++ (it is for a 120 class so it's not very advanced). I have function prototypes with comments of what they are supposed to do and include PLEASE USE THESE PROTOTYPES:
#include
#include
#include
#include
/* Some constants to organize the logic
* of rendering the battleship board. */
enum cell_t {
BLANK, HIT, MISS, SHIP, DMG
};
/* For some geometry. The x and y
* coordinates will refer to row and column,
* respectively in most instances. */
struct Point {
int row = 0;
int col = 0;
};
/* Represents one "cell" of a ship on the board.
* The boolean represents whether it has been
* destroyed or not. */
struct ShipSegment {
Point p; // corresponds to one square on the grid
bool hit = false;
};
/* Representation of a ship by an array of
* "ShipSegment" structs of the correct size. */
struct Ship {
int size;
ShipSegment* segs; // An array of grid squares
};
/* One cell of the gameboard. Only really used to
* render the state of the game for the user's reference. */
struct BoardCell {
cell_t type; // See the enum above, used main for rendering
};
/* An object for the gameboard, hard-coded
* to be 10-by-10. */
struct Board {
BoardCell cells[10][10]; // 2-d array of cells
// these will need to be initialized!
int rows = 10;
int cols = 10;
};
/* Keep track of which ships belong to which player.
* Also has a username for the UI. */
struct Player {
std::string username;
Ship* ships; // The array of ships
int n_ships; // The number of ships
Board atk; // Tracks hits and misses
Board fleet; // Shows player ships and damage
};
/* Clears the console */
void clear();
/* Generic to wait for the user to continue. */
void WaitEnter();
/* Verify if a point is on the board */
bool validCoords(int row, int col);
/* Verify the coordinates in the input format */
bool validCoords(char row, char col);
/* Test coordinate-wise equality between
* two points. */
bool equals(Point p1, Point p2);
/* Allocates the correct number of segments
* for a ship s. Assumes that s has not been
* initialized before, and should only be called
* once per ship! */
void initShip(Ship& s, int size);
/* Allocate space and set all cells to be blank at the start */
void initBoard(Board& b);
/* Translate to a character, based on the type */
char toChar(BoardCell c);
/* Sets all cells of a 10-by-10 board */
void setToBlank(Board& b);
/* Tests if a point p is inside the ship s.
* This assumes that the test is the result
* of a player's turn, and if it is a hit,
* marks the segment of the ship as destroyed.
*
* It then returns whether it was a hit or miss
* to the calling function. */
bool pointHits(Ship s, Point p);
/* Shoot at a player, and update the board with
* the result of the shot.
* Returns whether it was a hit for UI purposes. */
bool fireAt(Player& atk, Player& def, Point p);
/* Output subroutine for the game board. */
void printBoard(Board& b);
/* Ask a user to place their ships on the board.
* Does not let the lay ships across each other. */
void getShipPlacement(Player& pl, int ind, int size);
/* Checks whether a new ship (s1) is in spatial
* conflict with an existing one (s2). */
bool collidesWith(Ship& s1, Ship& s2);
/* Pulling out the logic for the initial
* fleet setup process. */
void askForShips(Player& p);
/* Checks to see if a player has lost all their ships. */
bool fleetDestroyed(Player* p);
/* Shows full "game" -- the player's
* fleet and their attacks */
void showStatus(Player* p);
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
