-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathship.h
122 lines (105 loc) · 3.16 KB
/
ship.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
112
113
114
115
116
117
118
119
120
121
122
#ifndef ship_h
#define ship_h
#include <string>
#include <stdio.h>
using namespace std;
class Ship
{
public:
//Variable that is used later in determining whether or not the ship is alive
int health[5];
//A blank array that is initialized in order to allow the direction of the ship's placement to be chosen
//the chunk array contains the positions of the coordinates that each ship is bound to at some point
string chunk[5];
//Variable to be used in the for-loops of the position creation based upon the orientation
int length = 0;
Ship(int shipNumber, string position, int l, string orientation)
{
//Sets the length and initial chunk variable (first position) equal to the respective parameters
length = l;
chunk[0] = position;
//Condition of if the orientation of the ship is to be up
if(orientation.compare("up") == 0)
{
//Increments the ship upwards
char number = position.at(1);
for(int i = 1; i < length; i++)
{
number--;
chunk[i] = string(1,position.at(0)) + number;
}
}
//Condition of if the orientation of the ship is to be down
if(orientation.compare("down") == 0)
{
//Increments the ship downwards
char number = position.at(1);
for(int i = 1; i < length; i++)
{
number++;
chunk[i] = string(1,position.at(0)) + number;
}
}
//Condition of if the orientation of the ship is to be right
if(orientation.compare("right") == 0)
{
//Increments the ship to the right
char number = position.at(0);
for(int i = 1; i < length; i++)
{
number++;
chunk[i] = number + string(1,position.at(1));
}
}
//Condition of if the orientation of the ship is to be left
if(orientation.compare("left") == 0)
{
//Increments the ship to the left
char number = position.at(0);
for(int i = 1; i < length; i++)
{
number--;
chunk[i] = number + string(1,position.at(1));
}
}
//Fills the health array with values equal to the corresponding ship nuber
for(int i = 0; i < length; i++)
{
health[i] = shipNumber;
}
}
//Method that attempts to shoot a ship at a given coordinate
bool shoot(string coord)
{
//Searches through the all of the ship coordinates of the class
for(int i = 0; i < length; i++)
{
//Compares the coordinates that are being shot with those of where the ship is located
if(coord.compare(chunk[i]) && health[i] != 0)
{
//Health of ship at that point is set to 0 (essentially a dead variable)
health[i] = 0;
return true;
}
}
return false;
}
//Statement to see if the ship is still alive
bool isAlive()
{
//Sets a count variable
int count = 0;
for(int i = 0; i < length; i++)
{
//Compounds the count variable based on the values of the health index at that point
count += health[i];
}
//After all of the compounding, if the count variable is equal to 0, the ship is confirmed to not be alive
if(count == 0)
return false;
//It's alive
else
return true;
}
};
#endif