-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
400 lines (373 loc) · 14.7 KB
/
main.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
class EnitityObject
{
//class to use for initializing each entity as
public:
EnitityObject();
EnitityObject(int row , int col){rowPosition = row;columnPosition = col;}
int GetrowPosition() { return rowPosition; }
void SetrowPosition(int val) { rowPosition = val; }
int GetcolumnPosition() { return columnPosition; }
void SetcolumnPosition(int val) { columnPosition = val; }
int GetoldRowPosition() { return oldRowPosition; }
void SetoldRowPosition(int val) { oldRowPosition = val; }
int GetoldColumnPosition() { return oldColumnPosition; }
void SetoldColumnPosition(int val) { oldColumnPosition = val; }
protected:
private:
int rowPosition;
int columnPosition;
int oldRowPosition;
int oldColumnPosition;
};
#include <iostream>
#include <fstream>
#include <list>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <vector>
using namespace std;
const char pirateShipSymbol = 'P', iceSymbol = 'I', exitSymbol = 'X', sharkSymbol = 'S', britSymbol = 'B';
const int ROWS = 15, COLUMNS = 30;
EnitityObject* pirateShip;
list<EnitityObject*> sharkList;
list<EnitityObject*> britList;
void briefing();
void readMap(char ocean[ROWS][COLUMNS]);
void printMap(const char ocean[ROWS][COLUMNS]);
bool checkCollisions(char ocean[ROWS][COLUMNS]);
void pirateMove(char ocean[ROWS][COLUMNS]);
bool isCorrectMoveKey(char c);
void updateMapPosition(char ocean[ROWS][COLUMNS], EnitityObject *enemy);
void findbestPathToTravel(char ocean[ROWS][COLUMNS],int &bestPathColumn, int &bestPathRow, bool checkForIce);
int distanceBetween(int x1, int y1, int x2,int y2);
void moveEnemy(char ocean[ROWS][COLUMNS], EnitityObject *entity, bool checkForIce);
void updateEnemyPosition(char ocean[ROWS][COLUMNS], list<EnitityObject*> enemy, bool checkIceCollision);
void updateEnemyMapPosition(char ocean[ROWS][COLUMNS], list<EnitityObject*> enemy);
void removeSharksCollision(char ocean[ROWS][COLUMNS],list<EnitityObject*> removeSharks);
void mapRandomizer( char randOcean[ROWS][COLUMNS]);
int main(){
//main function where all the looping of the game is done
//mapRandomizer();
char ocean[ROWS][COLUMNS];
briefing();
//readMap(ocean);
mapRandomizer(ocean);
printMap(ocean);
bool endOfGame = false;
while(!endOfGame){
pirateMove(ocean);
endOfGame = checkCollisions(ocean);
if(!endOfGame){
updateEnemyPosition(ocean,sharkList,true);
updateEnemyPosition(ocean,britList,false);
endOfGame = checkCollisions(ocean);
if(!endOfGame){
updateMapPosition(ocean, pirateShip);
updateEnemyMapPosition(ocean, sharkList);
updateEnemyMapPosition(ocean, britList);
printMap(ocean);
}
}
}
return 0;
}
void randomSymbolsOnMap( vector<int> &row, vector<int> &column, char ocean[ROWS][COLUMNS],int numElements, char symbol){
srand(static_cast <unsigned int> (time(0)));
for(int i = 0; i < numElements; i++){
int randomIndexRow = rand() % row.size();
ocean[row[randomIndexRow]][column[randomIndexRow]] = symbol;
row.erase(row.begin() + randomIndexRow);
column.erase(column.begin() + randomIndexRow);
}
}
void mapRandomizer(char randOcean[ROWS][COLUMNS]){
int sharkNum = 2;
int britnum = 1;
int iceNum = 12;
vector<int> openRows;
vector<int> openColums;
vector<int> borderRows;
vector<int> borderColumns;
srand(static_cast <unsigned int> (time(0)));
for(int i = 0; i < ROWS; i++){
for(int j = 0; j < COLUMNS; j++){
if(i == 0 || i == (ROWS-1) || j == 0 || j == (COLUMNS-1)){
randOcean[i][j] = iceSymbol;
borderColumns.push_back(j);
borderRows.push_back(i);
}else{
randOcean[i][j] = ' ';
openColums.push_back(j);
openRows.push_back(i);
}
}
}
for(int i = 0; i < britnum; i++){
int randomIndexRow = rand() % openRows.size();
randOcean[openRows[randomIndexRow]][openColums[randomIndexRow]] = britSymbol;
britList.push_back(new EnitityObject(openRows[randomIndexRow],openColums[randomIndexRow]));
openRows.erase(openRows.begin() + randomIndexRow);
openColums.erase(openColums.begin() + randomIndexRow);
}
for(int i = 0; i < sharkNum; i++){
int randomIndexRow = rand() % openRows.size();
randOcean[openRows[randomIndexRow]][openColums[randomIndexRow]] = sharkSymbol;
sharkList.push_back(new EnitityObject(openRows[randomIndexRow],openColums[randomIndexRow]));
openRows.erase(openRows.begin() + randomIndexRow);
openColums.erase(openColums.begin() + randomIndexRow);
}
for(int i = 0; i < iceNum; i++){
int randomIndexRow = rand() % openRows.size();
randOcean[openRows[randomIndexRow]][openColums[randomIndexRow]] = iceSymbol;
openRows.erase(openRows.begin() + randomIndexRow);
openColums.erase(openColums.begin() + randomIndexRow);
}
int randomIndexRow = rand() % openRows.size();
randOcean[openRows[randomIndexRow]][openColums[randomIndexRow]] = pirateShipSymbol;
pirateShip = new EnitityObject(openRows[randomIndexRow],openColums[randomIndexRow]);
openRows.erase(openRows.begin() + randomIndexRow);
openColums.erase(openColums.begin() + randomIndexRow);
randomIndexRow = rand() % borderRows.size();
randOcean[borderRows[randomIndexRow]][borderColumns[randomIndexRow]] = exitSymbol;
borderRows.erase(borderRows.begin() + randomIndexRow);
borderColumns.erase(borderColumns.begin() + randomIndexRow);
for(int i = 0; i < openRows.size(); i++){
randOcean[openRows[i]][openColums[i]] = ' ';
}
}
void briefing(){
//lay out the rules
cout << "blah blah blah rules" << endl;
}
void readMap(char ocean[ROWS][COLUMNS]){
//read map into char array and set the symbols
ifstream inFile("map.txt");
if(!inFile){
cout << "error" << endl;
}else
for(int i = 0; i < ROWS; i++){
for(int j = 0; j < COLUMNS; j++){
char current;
inFile.get(current);
while(isspace(current)){
inFile.get(current);
}
unsigned char c;
switch(current){
case '5':
c = pirateShipSymbol;
pirateShip = new EnitityObject(i,j);
break;
case '4':
c = exitSymbol;
break;
case '3':
c = britSymbol;
britList.push_back(new EnitityObject(i,j));
break;
case '2':
c = sharkSymbol;
sharkList.push_back(new EnitityObject(i,j));
break;
case '1':
c = iceSymbol;
break;
case '0':
c = ' ';
break;
}
ocean[i][j] = c;
}
}
}
void printMap(const char ocean[ROWS][COLUMNS]){
//print map to screen
for(int i = 0; i < ROWS; i++){
for(int j = 0; j < COLUMNS; j++){
cout << ocean[i][j];
}
cout << endl;
}
}
int distanceBetween(int x1, int y1, int x2,int y2){
//calculate the distance between two points
int xDistance = abs(x2-x1);
int yDistance = abs(y2-y1);
return xDistance + yDistance;
}
void updateMapPosition(char ocean[ROWS][COLUMNS], EnitityObject *entity){
//update map
ocean[entity->GetrowPosition()][entity->GetcolumnPosition()] = ocean[entity->GetoldRowPosition()][entity->GetoldColumnPosition()];
ocean[entity->GetoldRowPosition()][entity->GetoldColumnPosition()] = ' ';
}
void findbestPathToTravel(char ocean[ROWS][COLUMNS],int &bestPathColumn, int &bestPathRow, bool checkForIce){
//the logic to determine what way the enemy should travel to get to the player in the fastest route
// the bool check for ice is to allow me to check for ice if need be but if its false the enemy
//just goes through the ice
int const RCCI = 2, RCCO = 9;
int rowColumnAdditions[RCCO][RCCI] = {{0,0},{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
int smallestDistance = 99999;
int tempDistance = 0;
int returningRow = bestPathRow;
int returningColumn = bestPathColumn;
int tempRow = bestPathRow;
int tempColumn = bestPathColumn;
for(int i = 1; i < RCCO; i++){
tempRow = bestPathRow + rowColumnAdditions[i][0];
tempColumn = bestPathColumn + rowColumnAdditions[i][1];
tempDistance = distanceBetween(tempColumn, tempRow, pirateShip->GetcolumnPosition(), pirateShip->GetrowPosition());
if(tempRow != 0 && tempRow != ROWS-1 && tempColumn != 0 && tempColumn != COLUMNS-1){
bool isIce = checkForIce ? (ocean[tempRow][tempColumn] == iceSymbol) : false;
if(tempDistance < smallestDistance && !isIce){
returningColumn = tempColumn;
returningRow = tempRow;
smallestDistance = tempDistance;
}
}
}
bestPathRow = returningRow;
bestPathColumn = returningColumn;
}
void moveEnemy(char ocean[ROWS][COLUMNS], EnitityObject *entity, bool checkForIce){
//move the enemy and set its new position
int columnPosition = entity->GetcolumnPosition();
int rowPosition = entity->GetrowPosition();
int newColumnPosition = columnPosition;
int newRowPosition = rowPosition;
findbestPathToTravel(ocean, newColumnPosition, newRowPosition, checkForIce);
entity->SetoldColumnPosition(columnPosition);
entity->SetoldRowPosition(rowPosition);
entity->SetrowPosition(newRowPosition);
entity->SetcolumnPosition(newColumnPosition);
}
void pirateMove(char ocean[ROWS][COLUMNS]){
//move the pirates position, by checking inputted check then updateing accordingly
int newColumn = pirateShip->GetcolumnPosition(), newRow = pirateShip->GetrowPosition();
char movementDiretion = 'h';
while(!isCorrectMoveKey(movementDiretion)){
cout << "Select direction to move: ";
cin >> movementDiretion;
}
movementDiretion = tolower(movementDiretion);
switch (movementDiretion){
case 'q':
newColumn--;
newRow--;
break;
case 'w':
newRow--;
break;
case 'e':
newColumn++;
newRow--;
break;
case 'a':
newColumn--;
break;
case 'd':
newColumn++;
break;
case 'z':
newColumn--;
newRow++;
break;
case 'x':
newRow++;
break;
case 'c':
newColumn++;
newRow++;
break;
}
pirateShip->SetoldColumnPosition(pirateShip->GetcolumnPosition());
pirateShip->SetoldRowPosition(pirateShip->GetrowPosition());
pirateShip->SetcolumnPosition(newColumn);
pirateShip->SetrowPosition(newRow);
}
bool isCorrectMoveKey(char c){
//check to see if inputted char is allowed
char allowed[8] = {'q','w','e','a','d','z','x','c'};
for(int i = 0; i < 8; i++){
if(tolower(c) == allowed[i]){
return true;
}
}
return false;
}
void updateEnemyPosition(char ocean[ROWS][COLUMNS], list<EnitityObject*> enemy, bool checkIceCollision){
//loop over the list to update the position (before render)
list <EnitityObject*>::iterator iEnemy;
for (iEnemy = enemy.begin(); iEnemy != enemy.end(); ++iEnemy)
{
EnitityObject *dog = dynamic_cast<EnitityObject*>(*iEnemy);
moveEnemy(ocean, dog , checkIceCollision);
}
}
void updateEnemyMapPosition(char ocean[ROWS][COLUMNS], list<EnitityObject*> enemy){
//update the position on the map of the enemys
//need to loop over the list so each element can be updated
list <EnitityObject*>::iterator iEnemy;
for (iEnemy = enemy.begin(); iEnemy != enemy.end(); ++iEnemy)
{
EnitityObject *dog = dynamic_cast<EnitityObject*>(*iEnemy);
updateMapPosition(ocean, dog);
}
}
bool checkCollisions(char ocean[ROWS][COLUMNS]){
//cehck collisions with all objetcs on the screen to determine what to do
int shipRowPosition = pirateShip->GetrowPosition();
int shipColumnPosition = pirateShip->GetcolumnPosition();
if(ocean[shipRowPosition][shipColumnPosition] == iceSymbol){
cout << "\n\n\n\n\n\n";
cout << "You have hit ice!" << endl;
return true;
}
if(ocean[shipRowPosition][shipColumnPosition] == exitSymbol){
cout << "\n\n\n\n\n\n";
cout << "You win!!!!!!!!!!!!!!!" << endl;
return true;
}
list <EnitityObject*>::iterator iShark;
list <EnitityObject*>::iterator iBrits;
list<EnitityObject*> tempSharks;
//loop over brits check for any collisions
for (iBrits = britList.begin(); iBrits != britList.end(); ++iBrits)
{
EnitityObject *brit = dynamic_cast<EnitityObject*>(*iBrits);
if(distanceBetween(brit->GetrowPosition(),brit->GetcolumnPosition(), shipRowPosition,shipColumnPosition) <= 2){
cout << "\n\n\n\n\n\n";
cout << "You have been killed by the brits!" << endl;
return true;
}
}
//loop over sharks check for any collisions
for (iShark = sharkList.begin(); iShark != sharkList.end(); ++iShark)
{
EnitityObject *shark = dynamic_cast<EnitityObject*>(*iShark);
if(shark->GetcolumnPosition() == shipColumnPosition && shark->GetrowPosition() == shipRowPosition){
cout << "\n\n\n\n\n\n";
cout << "You have been eaten by a shark!" << endl;
return true;
}
for (iBrits = britList.begin(); iBrits != britList.end(); ++iBrits){
EnitityObject *brit = dynamic_cast<EnitityObject*>(*iBrits);
if(shark->GetcolumnPosition() == brit->GetcolumnPosition() && shark->GetrowPosition() == brit->GetrowPosition()){
tempSharks.push_back(shark);
}
}
}
removeSharksCollision(ocean, tempSharks);
return false;
}
void removeSharksCollision(char ocean[ROWS][COLUMNS], list<EnitityObject*> removeSharks){
//remove sharks from shark list in case of collision
list <EnitityObject*>::iterator itempShark;
for (itempShark = removeSharks.begin(); itempShark != removeSharks.end(); ++itempShark)
{
EnitityObject *shark = dynamic_cast<EnitityObject*>(*itempShark);
sharkList.remove(shark);
if(ocean[shark->GetoldRowPosition()][shark->GetoldColumnPosition()] != britSymbol)
ocean[shark->GetoldRowPosition()][shark->GetoldColumnPosition()] = ' ';
}
}