So I have most of the code written for battleship but now I need to actually implement the code in the main file and dont know where to start. Any Advice?
//Header File (battleship.h)
#include <cstdlib>
#include <ctime>
#ifndef BATTLESHIP_H_
#define BATTLESHIP_H_
//
// data structures definitions
//
constint fleetSize = 6; // number of battleships
constint fieldSize = 6; // the field (ocean) is fieldSize * fieldSize
// coordinates (Location) of the ship and shots
struct Location {
int x; // 1 through fieldSize
char y; // 'a' through fieldSize
};
// contains ship's coordinates (Location) and whether is was sunk
struct Ship {
Location loc;
bool sunk;
};
//
// initialization functions
//
void initialize(Ship[]); // places every Ship in a Location where x-coordinate is -1
// and y-coordinate is '*' (a star) to signify
// that the Ship is not deployed
Location pick(); // generates a random Location
bool match(const Ship, Location); // returns true if this Location matches
// the Location of the Ship
// returns false otherwise
int check(const Ship[], Location); // returns the index of element of the array
// that matches the Location
// returns -1 if none do
// uses match()
void deploy(Ship[]); // places an array of battleships in
// random Locations in the ocean
//
// display functions
//
void printShip(const Ship); // prints the Location and status (sunk or not)
// of a single ship
void printFleet(const Ship[]); // prints the Locations of all the ships and
// whether they are sunk
//
// battle functions
//
bool operational(const Ship[]); // returns true if at least one ship in the array
// is not sunk
Location fire(); // asks the user to input the coordinates of the next
// shot
// note that check() is also used in the battle
void sink(Ship&); // sets "sunk" member variable of the ship to true
#endif BATTLESHIP_H_
//battleship.cpp file
#include"battleship.h";
#include <iostream>;
using std::cout; using std::cin; using std::endl;
Location pick() {
Location loc;
loc.x = (rand() % 6) + 1;
switch ((rand() % 6) + 1) {
case 1:loc.y = 'a'; break;
case 2:loc.y = 'b'; break;
case 3:loc.y = 'c'; break;
case 4:loc.y = 'd'; break;
case 5:loc.y = 'e'; break;
case 6:loc.y = 'f'; break;
}
return loc;
}
Location fire() {
Location fireLoc;
cout << "Input x: ";
cin >> fireLoc.x;
cout << "Input y: ";
cin >> fireLoc.y;
return fireLoc;
}
void printShip(const Ship s) {
cout << "Ship is at " << s.loc.x << ",";
cout << s.loc.y << endl;
if (s.sunk) {
cout << "Ship is destroyed" << endl;
}
else {
cout << "Ship is still up" << endl;
}
}
bool match(const Ship myShip, Location myLoc) {
return (myShip.loc.x == myLoc.x && myShip.loc.y == myLoc.y) ? true : false;
}
void sink(Ship &myShip) {
myShip.sunk = true;
}
void initialize(Ship ships[]) {
for (int i = 0; i < fleetSize; i++) {
ships[i].loc.x = -1;
ships[i].loc.y = '*';
ships[i].sunk = false;
}
}
void printFleet(const Ship ships[]) {
for (int i = 0; i < fleetSize; i++) {
printShip(ships[i]);
}
}
void deploy(Ship ships[]) {
int shipIndex = 0;
Location randomLoc;
while (shipIndex < fleetSize) {
Location randomLoc = pick();
if (check(ships, randomLoc) == -1) {
ships[shipIndex].loc.x = randomLoc.x;
ships[shipIndex].loc.y = randomLoc.y;
ships[shipIndex].sunk = false;
shipIndex++;
}
}
}
int check(const Ship ships[], Location loc) {
for (int i = 0; i < fleetSize; i++) {
if (match(ships[i], loc)) {
return i;
}
}
return -1;
}
bool operational(const Ship ships[]) {
for (int i = 0; i < fleetSize; i++) {
if (!ships[i].sunk) {
returntrue;
}
}
returnfalse;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//game.cpp file
#include "battleship.h"
#include <iostream>
using std::cout; using std::cin; using std::endl;
int main() {
cout << "Welcome to Battleship! There are 6 ships on the board and it is your job to sink them. Good Luck!" << endl << endl;
cout << "The field is a 6x6 square, you will input a number (1-6) and a letter (a-f), Each ship takes up one space and once hit is sunk." << endl;
cout << "Please input your first guess: " << endl;
}
I'm just curious, as I'm trying to learn C++ myself, so I have a couple of questions:
a) shouldn't the #ifndef in the battleship.h file also have an #endif ?
b) I'm curious why "Location pick()" and "Location fire()" do not have return types defined, such as "sometype Location pick()", for example. I think this is me not quite understanding it fully though. I see those functions are defined in the header, and there is the structure Location too, but I was just figuring there should be a return type.
Anyway, if anyone can elucidate then great, but I'll continue my studies of C++ meanwhile.
Technically its not really battleship but what else would you call it? lol.
So if i start with a main loop to run until it is over should it be something like
1 2 3 4
if (fleetsize !=0){
//continue through program}
else
cout<<"You have sunk all the ships!"<<endl;
So, the header was written by the professor and the battleship.cpp was made in our lab with the student teacher so all of those work fine and are written correctly.