-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShip.cpp
More file actions
94 lines (83 loc) · 1.26 KB
/
Copy pathShip.cpp
File metadata and controls
94 lines (83 loc) · 1.26 KB
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
#include "Ship.h"
Ship::Ship()
{
}
Ship::Ship(Position start, Position end)
{
int startX = start.GetX();
char startY = start.GetYAsChar();
int endX = end.GetX();
char endY = end.GetYAsChar();
if (startX == endX)
{
if (startY < endY)
{
while (startY <= endY)
{
location.push_back(Position(startX, startY));
startY++;
}
}
else
{
while (endY <= startY)
{
location.push_back(Position(endX, endY));
endY++;
}
}
}
else
{
if (startX < endX)
{
while (startX <= endX)
{
location.push_back(Position(startX, startY));
startX++;
}
}
else
{
while (endX <= startX)
{
location.push_back(Position(endX, endY));
endX++;
}
}
}
length = location.size();
}
Ship::~Ship()
{
}
bool Ship::Hit()
{
hits++;
if (hits == length)
{
sunk = true;
}
return sunk;
}
bool Ship::IsOnShip(Position position)
{
int x = position.GetX();
int y = position.GetY();
for (int i = 0; i < location.size(); i++)
{
if (location[i].GetX() == x && location[i].GetY() == y)
{
return true;
}
}
return false;
}
int Ship::GetSize()
{
return location.size();
}
vector<Position> Ship::GetShip()
{
return location;
}