In: Computer Science
I am working on a project for my Computer Science course. I am trying to create a Battleship game where a user names two coordinates on a grid and is then told whether this results in a "Hit" or a "Miss". Once the ship has been hit a certain number of times (based on the size of the ship) the ship is sunk. I have written all the code but it now fails to execute when I try to run it. Could someone please help me to get this program working properly? I will paste the code below:
battleship.h:
#ifndef BATTLESHIP_H_
#define BATTLESHIP_H_
// coordinates (location) of the ship and shots
class location {
public:
void locationreset(void); // void constructor, assigns -1 to
X
void pick(void); // picks a random location
void fire(void); // asks the user to input the
// coordinates of the next shot
void print(void) const; // prints location in format "a1"
// returns true if the two locations match
friend bool compare(location, location);
private:
static const int fieldSize = 5; // the field (ocean) is fieldSize X
fieldSize
int x; // 1 through fieldSize
char y; // 'a' through fieldSize
};
// contains ship's coordinates (location) and whether is was
sunk
class ship {
public:
ship(void); // void constructor, sets sunk=false
bool match(location) const; // returns true if this location
matches
// the ship's location
bool isSunk(void) const { return(sunk); }; // checks to see if the
ship is sunk
void sink(void); // sets "sunk" member variable of the ship to
true
void setLocation(location); // deploys the ship at the specified
location
void printShip(void) const; // prints location and status of the
ship
private:
location loc;
bool sunk;
};
// contains the fleet of the deployed ships
class fleet {
public:
void deployFleet(void); // deploys the ships in random
locations
// of the ocean
bool operational(void) const; // returns true if at least
// one ship in the fleet is not sunk
bool isHitNSink(location); // returns true if there was a
deployed
// ship at this location (hit) and sinks it
// otherwise returns false (miss)
void printFleet(void) const; // prints out locations of ships in
fleet
private:
static const int fleetSize = 5; // number of battleships
bool check(location); // returns true if one of the ship's
locations
// matches the ship, returns false
// if none-match
ship ships[fleetSize]; // battleships of the fleet
};
#endif /* BATTLESHIP_H_ */
//////////////////////////////////////////////////////////////////////////////////////////////////////////
battleship.cpp:
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "battleship.h"
using namespace std;
void location::locationreset(void) {
x = -1;
y = 'x';
}
void location::pick(void) {
x = rand() % fieldSize + 1;
y = rand() % fieldSize;
switch (y) {
case 0: y = 'a'; break;
case 1: y = 'b'; break;
case 2: y = 'c'; break;
case 3: y = 'd'; break;
case 4: y = 'e'; break;
}
}
void location::fire(void) {
cout << "input the coordinates of the next shot
"; cin >> y >> x;
}
void location::print() const {
cout << y << x;
}
bool compare(location loc1, location loc2) {
return (loc1.x == loc2.x && loc1.y ==
loc2.y);
}
ship::ship(void) {
sunk = false;
}
bool ship::match(location shiploc) const {
return compare(loc, shiploc);
}
void ship::sink(void) {
sunk = true;
}
void ship::setLocation(location shiploc) { //
deploys the ship at the specified location
loc = shiploc;
}
void ship::printShip(void) const { // prints location and status
of the ship
loc.print();
if (sunk == true)
cout << "sunk";
else if (sunk == false)
cout << "not sunk" <<
endl;
}
void fleet::deployFleet(void) { // deploys the ships in random
locations of the ocean
location loc; loc.locationreset();
for (int i = 0; i < fleetSize; i++) {
loc.pick();
ships[i].setLocation(loc);
}
}
bool fleet::operational() const { // returns true if at least
one ship in the fleet is not sunk
for (int i = 0; i < fleetSize; ++i)
if (ships[i].isSunk() ==
false)
return
true;
return false;
}
bool fleet::isHitNSink(location shiploc) { //
returns true if there was a deployed
for (int i = 0; i < fleetSize; ++i) {
if (ships[i].match(shiploc) == true
&& ships[i].isSunk() == false)
{
ships[i].sink();
return
true;
}
else
return
false;
}
}
void fleet::printFleet(void) const { // prints out locations of
ships in fleet
for (int i = 0; i < fleetSize; ++i)
ships[i].printShip();
}
bool fleet::check(location shiploc) {
for (int i = 0; i < fleetSize; ++i)
if (ships[i].match(shiploc))
{
return
true;
}
else
{
return
false;
}
}
////////////////////////////////////////////////////////////////////////////
main.cpp:
#include "battleship.h"
#include <iostream>
using namespace std;
int main() {
srand(time(NULL));
char answer;
fleet myFleet; //computer's ships
myFleet.deployFleet(); // fleet is deployed at random
locations
cout << "Do ships' positions and status need to
be printed? (y for yes)" << endl;
cin >> answer;
if (answer == 'y')
myFleet.printFleet();
while (myFleet.operational() == true) {
cout << "there are still
ships up";
location userShot;
userShot.locationreset();
cout << "Input location(a-e
1-5): ";
userShot.fire();
if (myFleet.isHitNSink(userShot) ==
true)
cout <<
"hit";
else
cout <<
"miss";
cout << "Do ships' positions
and status need to be printed? (y for yes, q to quit)";
cin >> answer;
if (answer == 'y')
myFleet.printFleet();
else if (answer == 'q')
break;
}
}
If you are compiling below C++ codes in command line then compile it by using below line:
g++ main.cpp battleship.cpp
C++ code:
battleship.h
#ifndef BATTLESHIP_H_
#define BATTLESHIP_H_
// coordinates (location) of the ship and shots
class location
{
public:
void locationreset(void); // void constructor, assigns -1 to X
void pick(void); // picks a random location
void fire(void); // asks the user to input the
// coordinates of the next shot
void print(void) const; // prints location in format "a1"
// returns true if the two locations match
friend bool compare(location, location);
private:
static const int fieldSize = 5; // the field (ocean) is fieldSize X fieldSize
int x; // 1 through fieldSize
char y; // 'a' through fieldSize
};
// contains ship's coordinates (location) and whether is was sunk
class ship
{
public:
ship(void); // void constructor, sets sunk=false
bool match(location) const; // returns true if this location matches
// the ship's location
bool isSunk(void) const { return (sunk); }; // checks to see if the ship is sunk
void sink(void); // sets "sunk" member variable of the ship to true
void setLocation(location); // deploys the ship at the specified location
void printShip(void) const; // prints location and status of the ship
private:
location loc;
bool sunk;
};
// contains the fleet of the deployed ships
class fleet
{
public:
void deployFleet(void); // deploys the ships in random locations
// of the ocean
bool operational(void) const; // returns true if at least
// one ship in the fleet is not sunk
bool isHitNSink(location); // returns true if there was a deployed
// ship at this location (hit) and sinks it
// otherwise returns false (miss)
void printFleet(void) const; // prints out locations of ships in fleet
private:
static const int fleetSize = 5; // number of battleships
bool check(location); // returns true if one of the ship's locations
// matches the ship, returns false
// if none-match
ship ships[fleetSize]; // battleships of the fleet
};
#endif /* BATTLESHIP_H_ */
battleship.cpp
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "battleship.h"
using namespace std;
void location::locationreset(void)
{
x = -1;
y = 'x';
}
void location::pick(void)
{
x = rand() % fieldSize + 1;
y = rand() % fieldSize;
switch (y)
{
case 0:
y = 'a';
break;
case 1:
y = 'b';
break;
case 2:
y = 'c';
break;
case 3:
y = 'd';
break;
case 4:
y = 'e';
break;
}
}
void location::fire(void)
{
cout << "\ninput the coordinates of the next shot: ";
cin >> y >> x;
}
void location::print() const
{
cout << y << x;
}
bool compare(location loc1, location loc2)
{
return (loc1.x == loc2.x && loc1.y == loc2.y);
}
ship::ship(void)
{
sunk = false;
}
bool ship::match(location shiploc) const
{
return compare(loc, shiploc);
}
void ship::sink(void)
{
sunk = true;
}
void ship::setLocation(location shiploc)
{ // deploys the ship at the specified location
loc = shiploc;
}
void ship::printShip(void) const
{ // prints location and status of the ship
loc.print();
if (sunk == true)
cout << " sunk" << endl;
else if (sunk == false)
cout << " not sunk" << endl;
}
void fleet::deployFleet(void)
{ // deploys the ships in random locations of the ocean
location loc;
loc.locationreset();
for (int i = 0; i < fleetSize; i++)
{
loc.pick();
ships[i].setLocation(loc);
}
}
bool fleet::operational() const
{ // returns true if at least one ship in the fleet is not sunk
for (int i = 0; i < fleetSize; ++i)
if (ships[i].isSunk() == false)
return true;
return false;
}
bool fleet::isHitNSink(location shiploc)
{ // returns true if there was a deployed
for (int i = 0; i < fleetSize; ++i)
{
if (ships[i].match(shiploc) == true && ships[i].isSunk() == false)
{
ships[i].sink();
return true;
}
}
return false;
}
void fleet::printFleet(void) const
{ // prints out locations of ships in fleet
for (int i = 0; i < fleetSize; ++i)
ships[i].printShip();
}
bool fleet::check(location shiploc)
{
for (int i = 0; i < fleetSize; ++i)
if (ships[i].match(shiploc))
return true;
return false;
}
main.cpp
#include "battleship.h"
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
srand(time(NULL));
char answer;
fleet myFleet; //computer's ships
myFleet.deployFleet(); // fleet is deployed at random locations
cout << "Do ships' positions and status need to be printed? (y for yes): ";
cin >> answer;
if (answer == 'y')
myFleet.printFleet();
while (myFleet.operational() == true)
{
cout << "\nthere are still ships up";
location userShot;
userShot.locationreset();
cout << "\nInput location(a-e 1-5): ";
userShot.fire();
if (myFleet.isHitNSink(userShot) == true)
cout << "hit";
else
cout << "miss";
cout << "\nDo ships' positions and status need to be printed? (y for yes, q to quit): ";
cin >> answer;
if (answer == 'y')
myFleet.printFleet();
else if (answer == 'q')
break;
}
}
Output: