First Battleship Program

Hey guys I just wanted to share my first battleship program. It is pretty basic but I am new to c++. If you guys have any recommendations on things to fix or clean up to make it better that would be awesome! Thanks!

Header File:

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
#ifndef BATTLESHIP_H_
#define BATTLESHIP_H_

//
// data structures definitions
//

const int FLEET_SIZE=5; // number of battleships
const int FIELD_SIZE=5;  // the field (ocean) is FIELD_SIZExFIELD_SIZE

// coordinates (location) of the ship and shots
struct location{
  int x;  // 1 through FIELD_SIZE
  char y; // 'a' through FIELD_SIZE
};

// contains ship's coordinates (location) and whether is was sunk
struct ship{
  location loc;
  bool sunk;
};

//
// initialization functions
//
void initialize(ship[]); // places all ships in -1 X location to signify
                        // that the ship is not deployed
location pick(void); // generates a random location
bool match(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 the element
                                   // of the ship[] array that matches
                                   // location. Returns -1 if none match
                                   // uses match()
void deploy(ship[]); // places an array of battleships in
                     // random locations in the ocean

//
// display functions
//
void printShip(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(void);       // 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_ */ 


Main Function:

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include "BATTLESHIP.h"
#include <ctime>
#include <cstdlib>
using namespace std;

int main(){

	srand(time(NULL));
	ship s[FLEET_SIZE]; // enemy ship structure with 5 elements
	initialize(s); // run initialize function with structure s
	deploy(s); // run deploy function with structure s
	char shipLoc; // user input for ship status display
	ship user; // users fire guess
	cout << "WELCOME TO BATTLESHIP\n"
		<< "_____________________\n\n"
		<< " -Sink enemy ships-\n\n\n";
	cout << "You have the option to view the status of the ships or go straight to the game.\n\n";

	while(operational(s)){ // run this while there is at least one standing ship
		cout << "Would you like to see the location and status of the ships? <y or n>  ";
		cin >> shipLoc;
		if (shipLoc == 'y' || shipLoc == 'Y'){
			printFleet(s); // prints status and location of enemy ships
			user.loc=fire(); // users guess stored from fire function
			int c = check(s, user.loc); // holds the index of the array or -1
			if (c!=-1){ 
				sink(s[c]); // changes status of sink in individual element
				cout << "HIT! Ship sunk.\n\n";
			}
			else // if c = -1 then report a miss
				cout << "Sorry you missed, try again.\n\n";

		}
		else if (shipLoc == 'n' || shipLoc == 'N'){ // same as above, without printing ships
			user.loc=fire();
			int c = check(s, user.loc);
			if (c!=-1){
				sink(s[c]);
				cout << "HIT! Ship sunk.\n\n";
			}
			else
				cout << "Sorry you missed, try again.\n\n";
		}
		else 
			cout << "\nYou entered an invalid option. Please enter <y or n>\n" << endl;
	}
	cout << "\nCongratulations! You have sunk all enemy ships.\n\n";
}

void initialize(ship s[]){ // sets all ships in -1 to show it is not deployed
	for(int i=0; i<FLEET_SIZE; i++)
		s[i].loc.x = -1;		
}

void deploy(ship s[]){
	int i=0; // Number of ships so far

	while (i<FLEET_SIZE){
		location temp = pick(); // sets random location in temp variable
		int e = check(s, temp); // -1 if there is a match, otherwise holds the element of the ship
		if (e==-1){
			s[i].loc = temp; // gives random location to the ship
			s[i].sunk=false; // makes the ship up 
			i++;
		}
	}

}

location pick(void){ // picks a random location
	location loc;
	loc.x=rand()%FIELD_SIZE+1;
	loc.y=rand()%FIELD_SIZE+1;
	switch(loc.y){
	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;
	}
	return loc;
}

int check(const ship s[], location loc)
{
	for(int i=0; i<FLEET_SIZE; i++)
		if (match(s[i], loc)) // if true, return the index, if not return -1
			return i;
	return -1;
}

bool match(ship s, location l)
{
	if ((s.loc.x == l.x) && (s.loc.y == l.y)) // if both member variables match, return true
		return true;
	else 
		return false;  
}

bool operational(const ship s[]){
	for(int i=0; i<FLEET_SIZE; i++)
		if (s[i].sunk == false) // if there is at least one operational ship, return true
			return true;
	return false;

}

void printShip(ship s){
	cout << s.loc.y << s.loc.x << ' ';
	if(s.sunk == true)
		cout << "sunk  ";
	else if (s.sunk == false)
		cout << "up  ";
}

void printFleet(const ship s[]){
	for(int i=0; i<FLEET_SIZE; i++){
		printShip(s[i]);
	}
	cout << endl;
}

location fire(void){
	location temp; // temp variable for users guess
	cout << "\nWhere would you like to fire a shot?\n" 
		<< "Y-Coordinate <a-e>: ";
	cin >> temp.y;
	cout << "X-Coordinate <1-5>: ";
	cin >> temp.x;
	cout << endl;
	return temp; // returns temps locations
}

void sink(ship& s){
	s.sunk=true; // sinks the ship
}
Topic archived. No new replies allowed.