Battleship program problems with classes.

I am writing a battleship program that uses classes. I don't entirely understand classes, so there are a few errors, but I do not know what to do. I'm not exactly sure how to use different variables and functions from different classes, either. Help fixing program would be greatly appreciated.

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

// coordinates (location) of the ship and shots
class location{
public:
  location(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_ */ 


And the .cpp 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
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
138
139
140
#include <iostream>
#include "battleship.h"
#include <ctime>
#include <cstdlib>
using namespace std;

location lo;
ship s;
fleet f;
int fieldSize = 5;

int main()
{
	
}

location::location(void)
{
	x = -1;
	y = 'x';
}

void location::pick(void)
{
	x = rand()%fieldSize+1;
	int temp = rand()%fieldSize;
	switch(temp)
	{
		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)
{
	location temp;
	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;
}

void location::print(void) const
{
	cout << y << x;
}

//friend bool compare

ship::ship(void)
{
	for(int i = 0; i < 5; i++)
		s.sunk == false;
}

bool ship::match(location) const
{
	if (loc == lo.fire.temp.x && loc == lo.fire.temp.y)
		return true;
	else 
		return false;
}

bool ship::isSunk(void) const
{
	if(sunk == true)
		cout << "Sunk. ";
	else if(sunk == false)
		cout << "Up. ";
	return sunk;
}

void ship::sink(void)
{
	sunk == true;
}

void ship::setLocation(location)
{
	
}

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

void fleet::deployFleet(void)
{
    for(unsigned i = 0; i < fleetSize; i++)
    {
        lo.pick() ;
        ships[i].setLocation(loc) ;
    }
}

bool fleet::operational(void) const
{
	for(int i = 0; i < fleetSize; i++)
		if (s.isSunk() == false) 
			return true;
	return false;
}

bool fleet::isHitNSink(location)
{
	if(s.match() == true)
	{
		s.sink();
		return true;
	}
	else
		return false;
}

void fleet::printFleet(void) const
{	
	for(int i = 0; i < fleetSize; i++)
	{
		s.printShip(ships[i]);
	}
	cout << endl;
}

bool fleet::check(location)
{
	for(int i = 0; i < fleetSize; i++)
		if (s.match(s[i], loc))
			return true;
	return false;
}
Topic archived. No new replies allowed.