/* Battleship Game (Idan Harel) * ship.cpp: Add or remove ships */#include <string>using namespace std;/* Battle Ship * This class usage for creation of one battle ship. * Constructor get the ship block size (by default, 1 to 5) * Ship_GetSize () usaed to get ship size. If faild to create ship, it will return false * * Ship_SetCoord (direction, x, y) sets a new ship coordinations. * Direction can be v (vertical) or h (horizontal). * x and y is the coordination on board where the ship starts. * My_ShipCoord array 0 is the x, and 1 is the y. (x horizontal, y vertical) * The function will calculate where the ship ends and put it in the array. * * Ship_Hit (x, y) trys to hit the ship. * Functions returns yes if ship got hit, no if not and sink if ship sinked while hit. */class Ship{ private: static const int SHIP_MAX_SIZE = 5; // Maximum 5 Ship block size unsigned short int My_ShipSize; // The ship size short int My_ShipCoord [2][SHIP_MAX_SIZE]; // The ship coordinations public: Ship (unsigned short int ShipSize) // Constructor. Gets ships size { if (ShipSize < SHIP_MAX_SIZE) // If the ship size is less then the maximum size this-> My_ShipSize = ShipSize; // New ship has been created } unsigned short int Ship_GetSize () // Gets Ship size. Returns false if ship isn't created { if (this-> My_ShipSize > 0) return this-> My_ShipSize; else // Ship dosen't exists (created with errors) return false; } bool Ship_SetCoord (char direction [1], unsigned int x, unsigned int y) // Set new ship cordinations. { for (short int i = 0; i < this-> My_ShipSize; i++) { this-> My_ShipCoord [0][i] = x; // My_ShipCoords[0] horizontal this-> My_ShipCoord [1][i] = y; // My_ShipCoords[1] vertical if (direction == "h") x++; // Direction is horizontal else if (direction == "v") y++; // Direction is vertical else return false; } return true; } string Ship_Hit (unsigned int x, unsigned int y) // Trys to hit a ship. Returns return_status { string return_status; return_status = "no"; unsigned short int sink_count = 0; // Count ship hits for (short int i = 0; i < this-> My_ShipSize; i++) { if ((this-> My_ShipCoord [0][i] == x) && (this-> My_ShipCoord [1][i] == y)) // Ship got an hit { this-> My_ShipCoord [0][i] = -1; // -1 Means the ship has been hit this-> My_ShipCoord [1][i] = -1; return_status = "yes"; } if ((this-> My_ShipCoord [0][i] == -1) && (this-> My_ShipCoord [1][i] == -1)) sink_count++; } if ((sink_count == this-> My_ShipSize) && (return_status == "yes")) // If ship has been hitted and sink counter is the ship size return_status = "sink"; return return_status; }};/* Battle Ships * Creates NUMBER_OF_SHIPS ships. * Constractor gets array with ship block size (See class Ship for maximum block size) * And creates objects from type class Ship and put this into MyShips array. * Ships_GetShip (ShipNumber) will return the pointer for object of the ship (class Ship) */class Ships{ private: static const int NUMBER_OF_SHIPS = 5; // Maximum 5 Ships Ship * MyShips [NUMBER_OF_SHIPS]; // Ships vector of my ships public: Ships (int * ShipsArray) // Constructor. Ships array of Maxmimum ships with there sizes { for (short int i=0; i < NUMBER_OF_SHIPS; i++) this-> MyShips[i] = new Ship(ShipsArray[i]); // Creates the new ship } Ship * Ships_GetShip (short int ShipNumber) // Returns pointer for object of class Ship { return this-> MyShips[ShipNumber]; }};// *** ANOTHER FILE WITH INCLUDES ***int main (){ int arr [] = { 2,3,3,2,3 }; Ships test (arr); Ship * MyShip = test.Ships_GetShip (3); // Get ship number 4 cout << MyShip-> Ship_GetSize (); getch (); return 0;}