Battleship - singleplayer

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
 //Header File (battleship.h)
#include <cstdlib>
#include <ctime>


#ifndef BATTLESHIP_H_
#define BATTLESHIP_H_

//
// data structures definitions
//

const int fleetSize = 6; // number of battleships
const int 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_


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//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) {
			return true;
		}
	}
	return false;


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;
	

	



}


You might want to start with a main loop that runs until the game is over.
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.
It does have an #endif.

Location is defined:

1
2
3
4
struct Location {
	int x;  // 1 through fieldSize
	char y; // 'a' through fieldSize
};


Location is the return type. You can return a struct.

Anyway, it's not really battleship, is it. The ships all only take up 1 location. They should take from 2 to 5 locations (or something like that).
Last edited on
Oh I see. Yeah, I get it. That makes sense. Thanks!
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.
Yeah. You could use a while loop like this.
1
2
3
4
5
While(fleetSize!=0)
{
    //do stuff
}
//end game 
Please mark as solved if your question was answered and you understood
closed account (367kGNh0)
Hello Urawrath, may I ask what IDE you are using for the production of your game?
Rascake it's a console game.. you can use any IDE to make a game like that ;p
I think what you're looking for is a game engine.
closed account (367kGNh0)
Ah I see!
Last edited on
Topic archived. No new replies allowed.