Having Problems with classes

Hello. I am creating a battleship program and it appears for everything to be working fine, except the shots. For some reason my shots and my locations are not working and I have been working on fixing this a long time. Can somebody please take a look at my code?
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
//Header File:
#ifndef BATTLESHIP_H_
#define BATTLESHIP_H_

// coordinates (location) of the ship and shots
class location{
public:
  location(); // void constructor, assigns -1 to X 
  void pick(); // picks a random location
  void fire(); // asks the user to input coordinates of the next shot
  void print() 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 constructor, sets sunk=false
  bool match(location&) const; // returns true if this location matches
                                    // the ship's location
  bool isSunk() const {return sunk;} // checks to see if the ship is sunk
  void sink();       // sets "sunk" member variable of the ship to true
  void setLocation(const location&); // deploys the ship at the specified location
  void printShip() 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(); // deploys the ships in random locations
                          // of the ocean
  bool operational() const; // returns true if at least
                                // one ship in the fleet is not sunk
  bool isHitNSink(const location &); // returns true if there was a deployed
                        // ship at this location (hit) and sinks it
                        // otherwise returns false (miss)
  void printFleet() const; // prints out locations of ships in fleet

private:
  static const int fleetSize=5; // number of battleships
  int check(const location &);          // returns index of the ship 
                                // that matches location
                                // -1 if none match
  ship ships[fleetSize];        // battleships of the fleet
};

#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
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
//Function Definitions:
#include "battleship.h"
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

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

void location::pick(void){
	srand(time(NULL));
	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 tmp;
	cout<<"Pick a location to shoot"<<endl;
	cout<<"Enter a 'y' <a-e> then 'x' <1-5>"<<endl;
	cin>>tmp.y>>tmp.x;
	cout<<endl;
}

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


bool compare(location spot1, location spot2){
	return (spot1.x==spot2.x && spot1.y==spot2.y);
}


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

bool ship::match(location& shipSpot) const {
	return compare(loc, shipSpot);
}

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


void ship::setLocation(const location& shipSpot){
	loc=shipSpot;
}

void ship::printShip(void) const{
	loc.print();
	if(sunk==true)
		cout<<"Sunk";
	else if(sunk==false)
		cout<<"remains";
	cout<<endl;
}

void fleet::deployFleet(void){
	location spot;
	spot.location::location();
	for(int i=0; i<fleetSize; i++){
		spot.pick();
		ships[i].setLocation(spot);
	}
}

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

bool fleet::isHitNSink(const location& shipSpot){
	location tmp=shipSpot;
	for(int i=0; i<fleetSize; ++i)
		if(ships[i].match(tmp)==true && ships[i].isSunk()==false){
			ships[i].sink();
			return true;
		}
		else
			return false;
}

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

int fleet::check(const location& shipSpot){
	location tmp=shipSpot;
	for(int i=0; i<fleetSize; ++i)
		if(ships[i].match(tmp))
			return true;
		else
			return false;
}


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
//Test File to run the program:
#include "battleship.h"
#include <iostream>

using namespace std;

int main (){
	char answer;	
	fleet myFleet;
	myFleet.deployFleet();
	cout << "Print position of ships?<y or n>" << endl;
	cin >> answer;
	if (answer=='y')
		myFleet.printFleet();
	while (myFleet.operational()==true){
		cout << "there are still ships up";
		location userShot;
		userShot.location();
		cout << "Please select next shot(a-e then 1-5) ";
		userShot.fire();
		if (myFleet.isHitNSink(userShot)==true)
			cout << "Shot!";
		else
			cout << "Missed!";
		cout << "Print position of ships? <y or n>";
		cin >> answer;
		if (answer== 'y')
			myFleet.printFleet();
		else if(answer=='n')
			break;
	}
}
So what's the problem with the shooting, and where in the code do you think the problem is?
I want to guess it's in both my deploy and firing function and I've tried editing both of those and I'm figuring out nothing....
Using the test that I have it has to be in the deploy. I get the same fleet everytime that it deploys but after I print and try to shoot a spot where I know there is a boat it says I missed.
Update: I figured out the deploy but the fire function is messed up.
Last edited on
Topic archived. No new replies allowed.