-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShip.cpp
196 lines (156 loc) · 5.4 KB
/
Ship.cpp
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "Tile.h"
#include "Ship.h"
#include "Battleship.h"
#include "Cruiser.h"
#include "Destroyer.h"
#include "Board.h"
#include "Player.h"
#include "Game.h"
#include <iostream>
using namespace std;
//Constructor
Ship::Ship(int s){
shipSize = s;
}
//Συνάρτηση που επιστρέφει true αν τοποθετήθηκε πλοίο
bool Ship::placeShip(Tile startCell, Orientation direction, Tile board[][N]){
int i;
int x = startCell.getCoordinatesX();
int y = startCell.getCoordinatesY();
if(direction == horizontal){
for(i=y; i<=y+shipSize - 1; i++){
board[x][i].setShip();
}
}
else if(direction == vertical){
for(i=x; i<=x+shipSize - 1; i++){
board[i][y].setShip();
}
}
return true;
}
//Συνάρτηση που εάν το πλοίο ξεπερνάει τα όρια του πίνακα θα κάνει throw OversizeException και θα επιστρέψει true
bool Ship::isOutOfBoard(Tile board[][N], Tile startCell, Orientation direction, bool exceptionMessages){
int x = startCell.getCoordinatesX();
int y = startCell.getCoordinatesY();
bool isInvalid = false;
try{
if(direction == horizontal){
if(y+shipSize-1>=N){
isInvalid=true;
}
}
else if(direction == vertical){
if(x+shipSize-1>=M){
isInvalid=true;
}
}
if(isInvalid == true){
throw "OversizeException occurred! ";
}
}
catch(const char *e){
if(exceptionMessages == true){
cout << e << "The ship cant get out of the gameboard borders." << endl;
}
}
return isInvalid;
}
//Συνάρτηση που εάν η θέση που τοποθετηθεί το πλοίο υπάρχει άλλο θα κάνει throw OverlapTilesException και θα επιστρέψει true
bool Ship::isAnotherShip(Tile board[][N], Tile startCell, Orientation direction, bool exceptionMessages){
int i;
int x = startCell.getCoordinatesX();
int y = startCell.getCoordinatesY();
bool isInvalid=false;
try{
if(direction == horizontal){
for(i=y; i<=y+shipSize - 1; i++){
if(board[x][i].getTileType() == Tile::Type::ship){
isInvalid = true;
}
}
}
else if(direction == vertical){
for(i=x; i<=x+shipSize - 1; i++){
if(board[i][y].getTileType() == Tile::Type::ship){
isInvalid = true;
}
}
}
if(isInvalid == true){
throw "OverlapTilesException occurred! ";
}
}
catch(const char *e){
if(exceptionMessages == true){
cout << e << "The ship cannot be placed in a tile where another one is already placed." << endl;
}
}
return isInvalid;
}
//Συνάρτηση που ελέγχει αν στα γειτονικά κελιά υπάρχει ήδη πλοίο, αν ναι επιστρέφει true αλλιώς επιστρέφει false
bool Ship::isNextToAnotherShip(Tile board[][N], Tile startCell, Orientation direction, bool exceptionMessages){
int i;
int x = startCell.getCoordinatesX();
int y = startCell.getCoordinatesY();
bool isInvalid=false;
try{
if(direction == horizontal){
if(y>0){
if(board[x][y-1].getTileType() == Tile::Type::ship){
isInvalid = true;
}
}
if(y+shipSize<N){
if(board[x][y+shipSize].getTileType() == Tile::Type::ship){
isInvalid = true;
}
}
for(i=y; i<y+shipSize; i++){
if(x>0){
if(board[x-1][i].getTileType() == Tile::Type::ship){
isInvalid = true;
}
}
if(x<M-1){
if(board[x+1][i].getTileType() == Tile::Type::ship){
isInvalid = true;
}
}
}
}
else if(direction == vertical){
if(x>0){
if(board[x-1][y].getTileType() == Tile::Type::ship){
isInvalid = true;
}
}
if(x+shipSize < M){
if(board[x+1][y].getTileType() == Tile::Type::ship){
isInvalid = true;
}
}
for(i=x; i<x+shipSize; i++){
if(y>0){
if(board[i][y-1].getTileType() == Tile::Type::ship){
isInvalid = true;
}
}
if(y<N-1){
if(board[i][y+1].getTileType() == Tile::Type::ship){
isInvalid = true;
}
}
}
}
if(isInvalid == true){
throw "AdjacentTilesException occurred! ";
}
}
catch(const char *e){
if(exceptionMessages == true){
cout << e << "There must be at least one tile empty between ships." << endl;
}
}
return isInvalid;
}