c++ battleship game program. need Assigment 2, game. cpp. the battleship. h. is
ID: 3687434 • Letter: C
Question
c++ battleship game program. need Assigment 2, game. cpp.
the battleship. h. is :
#ifndef BATTLESHIP_H_ #define BATTLESHIP_H_ const int FLEET_SIZE=5; const int FIELD_SIZE=5; struct location{ int x; char y; }; struct ship{ location loc; bool sunk; }; void initialize(ship[]); location pick(); bool match(ship, location); int check(const ship[], location); void deploy(ship[]); void printShip(ship); void printFleet(const ship[]); bool operational(const ship[]); location fire(); void sink(ship&); #endif
the battleship.cpp is:
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "battleship.h"
using std::cout; using std::cin; using std::endl;
bool match(ship myShip, location mySpot){
// returns true if this location matches
// the location of the ship
// returns false otherwise
if (myShip.loc.x == mySpot.x && myShip.loc.y == mySpot.y)
return true;
else
return false;
}
int check(const ship myFleet[], location mySpot){
// returns the index of element of the array
// that matches the location
// returns -1 if none do
for (int i = 0; i < FLEET_SIZE; ++i)
if (match(myFleet[i], mySpot)) {
return i;
}
return -1;
}
void initialize(ship myFleet[]) {
// places all ships in -1 X location to signify
// that the ship is not deployed
for (int i=0; i < FLEET_SIZE; ++i) {
myFleet[i].loc.x = -1;
myFleet[i].loc.y = 'X';
}
}
location pick() {
// generates a random location
location mySpot;
srand(int(time(0)));
mySpot.x = rand() % 5 + 1;
int y = rand() % 5 + 1;
switch like emoticon {
case 1:
mySpot.y = 'a';
break;
case 2:
mySpot.y = 'b';
break;
case 3:
mySpot.y = 'c';
break;
case 4:
mySpot.y = 'd';
break;
case 5:
mySpot.y = 'e';
break;
}
return mySpot;
}
void deploy(ship myFleet[]) {
int i = 0;
while (i < FLEET_SIZE) {
location mySpot = pick();
int tmp = check(myFleet, mySpot);
if (tmp == -1) {
myFleet[i].loc = mySpot;
myFleet[i].sunk = false;
++i;
}
}
}
void printShip(ship myShip) {
cout << "The ship is at: " << myShip.loc.x << myShip.loc.y << endl;
cout << "Status of ship: ";
if (myShip.sunk == false)
cout << "The ship is still alive!" << endl;
else
cout << "The ship was sunk!" << endl;
}
void printFleet(ship const myFleet[]) {
for (int i = 0; i < FLEET_SIZE; ++i) {
printShip(myFleet[i]);
}
}
bool operational(ship const myFleet[]) {
for (int i = 0; i < FLEET_SIZE; ++i) {
if (myFleet[i].loc.x != -1 && myFleet[i].loc.y != -1)
return true;
}
return false;
}
location fire() {
location mySpot;
cout << "Where will you take your next shot? (Coordinates 1-5 a-e separated by a space): ";
cin >> mySpot.x >> mySpot.y;
return mySpot;
}
void sink(ship& myShip) {
myShip.sunk = true;
}
Explanation / Answer
Given below is the completed code for the question. Modified the batttleship.cpp code to meet the specifications in the question. Specifically these functions - operational(), printShip() and initialize() were modified. The game.cpp is implemented and output shown below. For the purpose of debugging, the printFleet() is done in each iteration. That line can be commented out. Hope the answer helped. If it did, please don't forget to rate the answer. Thank you very much.
battleship.cpp
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "battleship.h"
using std::cout; using std::cin; using std::endl;
bool match(ship myShip, location mySpot){
// returns true if this location matches
// the location of the ship
// returns false otherwise
if (myShip.loc.x == mySpot.x && myShip.loc.y == mySpot.y)
return true;
else
return false;
}
int check(const ship myFleet[], location mySpot){
// returns the index of element of the array
// that matches the location
// returns -1 if none do
for (int i = 0; i < FLEET_SIZE; ++i)
if (match(myFleet[i], mySpot)) {
return i;
}
return -1;
}
void initialize(ship myFleet[]) {
// places all ships in -1 * location to signify
// that the ship is not deployed
for (int i=0; i < FLEET_SIZE; ++i) {
myFleet[i].loc.x = -1;
myFleet[i].loc.y = '*';
}
}
location pick() {
// generates a random location
location mySpot;
mySpot.x = rand() % FIELD_SIZE + 1;
int y = rand() % FIELD_SIZE + 1;
switch(y)
{
case 1:
mySpot.y = 'a';
break;
case 2:
mySpot.y = 'b';
break;
case 3:
mySpot.y = 'c';
break;
case 4:
mySpot.y = 'd';
break;
case 5:
mySpot.y = 'e';
break;
}
return mySpot;
}
void deploy(ship myFleet[]) {
int i = 0;
while (i < FLEET_SIZE) {
location mySpot = pick();
int tmp = check(myFleet, mySpot);
if (tmp == -1) {
myFleet[i].loc = mySpot;
myFleet[i].sunk = false;
++i;
}
}
}
void printShip(ship myShip) {
cout << myShip.loc.y << myShip.loc.x << " ";
if(myShip.sunk)
cout << "sunk" ;
else
cout << "up";
}
void printFleet(ship const myFleet[]) {
printShip(myFleet[0]);
for (int i = 1; i < FLEET_SIZE; ++i) {
cout << ", ";
printShip(myFleet[i]);
}
cout << endl;
}
bool operational(ship const myFleet[]) {
for (int i = 0; i < FLEET_SIZE; ++i) {
if (!myFleet[i].sunk)
return true;
}
return false;
}
location fire() {
location mySpot;
cout << "Where will you take your next shot? (Coordinates a-e 1-5 e.g. a3 or d5): ";
cin >> mySpot.y >> mySpot.x;
return mySpot;
}
void sink(ship& myShip) {
myShip.sunk = true;
}
game.cpp
#include <ctime>
#include <cstdlib>
#include <iostream>
#include "battleship.h"
using namespace std;
int main()
{
ship myFleet[FLEET_SIZE];
string ans;
srand(int(time(0)));
initialize(myFleet);
deploy(myFleet);
cout << "Do you want to print the Fleet? y/n: ";
cin >> ans;
if(ans == "y" || ans == "Y")
printFleet(myFleet);
while(operational(myFleet))
{
location myLocation = fire();
int index = check(myFleet, myLocation);
if(index != -1)
{
sink(myFleet[index]);
cout << "It's a HIT !" << endl;
}
else
{
cout << "It's a MISS!" << endl;
}
printFleet(myFleet); //for debugging only, comment it out
}
cout << "Game over !" << endl;
}
output
Do you want to print the Fleet? y/n: n
Where will you take your next shot? (Coordinates a-e 1-5 separated by a space e.g. a3 or d5): b4
It's a MISS!
d1 up, e1 up, d5 up, a2 up, c5 up
Where will you take your next shot? (Coordinates a-e 1-5 separated by a space e.g. a3 or d5): a5
It's a MISS!
d1 up, e1 up, d5 up, a2 up, c5 up
Where will you take your next shot? (Coordinates a-e 1-5 separated by a space e.g. a3 or d5): e1
It's a HIT !
d1 up, e1 sunk, d5 up, a2 up, c5 up
Where will you take your next shot? (Coordinates a-e 1-5 separated by a space e.g. a3 or d5): c3
It's a MISS!
d1 up, e1 sunk, d5 up, a2 up, c5 up
Where will you take your next shot? (Coordinates a-e 1-5 separated by a space e.g. a3 or d5): a2
It's a HIT !
d1 up, e1 sunk, d5 up, a2 sunk, c5 up
Where will you take your next shot? (Coordinates a-e 1-5 separated by a space e.g. a3 or d5): d1
It's a HIT !
d1 sunk, e1 sunk, d5 up, a2 sunk, c5 up
Where will you take your next shot? (Coordinates a-e 1-5 separated by a space e.g. a3 or d5): c3
It's a MISS!
d1 sunk, e1 sunk, d5 up, a2 sunk, c5 up
Where will you take your next shot? (Coordinates a-e 1-5 separated by a space e.g. a3 or d5): c5
It's a HIT !
d1 sunk, e1 sunk, d5 up, a2 sunk, c5 sunk
Where will you take your next shot? (Coordinates a-e 1-5 separated by a space e.g. a3 or d5): d5
It's a HIT !
d1 sunk, e1 sunk, d5 sunk, a2 sunk, c5 sunk
Game over !
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.