-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
416 lines (397 loc) · 14.5 KB
/
main.cpp
File metadata and controls
416 lines (397 loc) · 14.5 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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// George Fafard and Sean Cosgrove
// Lisa Dion
// CS 120
// Project 3: Battleship
#include <string>
#include <iostream>
#include <optional>
#include <memory>
#include "battleship.h"
#include "destroyer.h"
#include "torpedoes.h"
#include "board.h"
using namespace std;
/********** Global functions declarations **********/
// start game
// Requires: nothing
// Modifies: nothing
// Effects: gets user input for battleship or destroyer battle, calls respective game function
void startGame();
// battleship game
// Requires: nothing
// Modifies: battleship object
// Effects: calls make user battleship and battle functions
void battleshipGame();
// destroyer game
// Requires: nothing
// Modifies: destroyer object
// Effects: calls make user destroyer and destroyer battle function
void destroyerGame();
// make user battleship
// Requires: nothing
// Modifies: creates a new battleship with user input values
// Effects: returns a new battleship
Battleship makeUserBattleship();
// make user destroyer
// Requires: nothing
// Modifies: creates a new destroyer with user input values
// Effects: returns a new destroyer
Destroyer makeUserDestroyer();
// battle
// Requires: two battleships
// Modifies: hitpoints of the battleships
// Effects: fires until one is destroyed (takes turns, battleship one goes first). returns the winner.
Battleship battle(Battleship &pOne, Battleship &pTwo);
// destroyer battle
// Requires: two destroyers
// Modifies: hitpoints of the battleships
// Effects: fires until one is destroyed (takes turns, destroyer one goes first). returns the winner.
Destroyer destroyerBattle(Destroyer &pOne, Destroyer &pTwo);
// MAIN
int main() {
startGame();
return 0;
}
/********** Global function definitions **********/
// startGame function
void startGame() {
// get user input
char choice;
cout << "Battleship or Destroyer battle? (B/D) ";
cin >> choice;
while (tolower(choice) != 'b' && tolower(choice) != 'd') {
cout << "Please enter a 'B' or 'D' ";
cin >> choice;
}
// user wants to fight with destroyers
if (tolower(choice) == 'D') {
destroyerGame(); // call destroyerGame function
// user wants to fight with battleships
} else if (tolower(choice) == 'B') {
battleshipGame(); // call battleshipGame function
} else {
battleshipGame();
}
}
// battleshipGame function
void battleshipGame() {
// initialize variables
Battleship userShip = makeUserBattleship();
Board board = Board();
int userX;
int userY;
int counter = 0;
bool forward;
char choice;
// while loop until game is over
while (userShip.getDestroyed() == false){
// if initial position
if (userShip.getX()) {
// if they want to move on X
choice = 'a';
cout << "Do you want to move on the X axis (y/n)? ";
cin >> choice;
if (choice == 'Y' || choice == 'y') {
// move forward?
choice = 'a';
cout << "Do you want to move forward? (y/n) ";
cin >> choice;
if (choice == 'Y' || choice == 'y') {
forward = true;
cout << "your new X position is: " << board.moveX(userShip, forward) << endl;
} else if (choice == 'n' || choice != 'N') {
forward = false;
cout << "your new X position is: " << board.moveX(userShip, forward) << endl;
} else {
cout << "thats not a y or an n" << endl;
}
} else if (choice == 'n' || choice != 'N') {
cout << "Ok, you did not move." << endl;
} else {
cout << "thats not a y or an n" << endl;
}
} else{
cout << "Enter your starting X position between 0 and " << board.getSizeX() << ": ";
cin >> userX;
while (userX > board.getSizeX() || userX < 0){
cout << "Enter a valid X: ";
cin >> userX;
}
userShip.setX(userX);
}
if (userShip.getY()){
// move on Y
choice = 'a';
cout << "Do you want to move on the Y axis (y/n)? ";
cin >> choice;
if (choice == 'Y' || choice == 'y') {
// move up?
choice = 'a';
cout << "Do you want to move up? (y/n) ";
cin >> choice;
if (choice == 'Y' || choice == 'y') {
forward = true;
cout << "your new Y position is: " << board.moveY(userShip, forward) << endl;
} else if (choice == 'n' || choice != 'N') {
forward = false;
cout << "your new Y position is: " << board.moveY(userShip, forward) << endl;
} else {
cout << "thats not a y or an n" << endl;
}
} else if (choice == 'n' || choice != 'N') {
cout << "Ok, you did not move." << endl;
} else {
cout << "thats not a y or an n" << endl;
}
} else {
cout << "Enter your starting Y position between 0 and " << board.getSizeY() << ": ";
cin >> userY;
while (userY > board.getSizeY() || userY < 0){
cout << "Enter a valid Y: ";
cin >> userY;
}
userShip.setY(userY);
}
cout << "Battle enemy ship? (Y/N) ";
// battle the enemy
cin >> choice;
if (choice == 'Y' || choice == 'y' ){
// count how many ships
++counter;
unique_ptr<Battleship> computerPtr = make_unique<Battleship>("Computer " + to_string(counter));
// computer position
computerPtr->setX(0);
computerPtr->setY(0);
cout << "Your distance from " << computerPtr->getName() << " is " << board.calcDistance(userShip, *computerPtr) << endl;
// call battle function
battle(userShip, *computerPtr);
} else if(choice == 'n' || choice != 'N') {
cout << "you're a wimp" << endl;
} else {
cout << "thats not a y or an n" << endl;
}
}
cout << "You were destroyed. Game Over." << endl;
}
// destroyerGame function
void destroyerGame() {
// initialize variables
Destroyer userShip = makeUserDestroyer();
Board board = Board();
int userX;
int userY;
int counter = 0;
bool forward;
char choice;
// while loop until game is over
while (userShip.getDestroyed() == false){
// if initial position
if (userShip.getX()) {
// if they want to move on X
choice = 'a';
cout << "Do you want to move on the X axis (y/n)? ";
cin >> choice;
if (choice == 'Y' || choice == 'y') {
// move forward?
choice = 'a';
cout << "Do you want to move forward? (y/n) ";
cin >> choice;
if (choice == 'Y' || choice == 'y') {
forward = true;
cout << "your new X position is: " << board.moveX(userShip, forward) << endl;
} else if (choice == 'n' || choice != 'N') {
forward = false;
cout << "your new X position is: " << board.moveX(userShip, forward) << endl;
} else {
cout << "thats not a y or an n" << endl;
}
} else if (choice == 'n' || choice != 'N') {
cout << "Ok, you did not move." << endl;
} else {
cout << "thats not a y or an n" << endl;
}
} else{
cout << "Enter your starting X position between 0 and " << board.getSizeX() << ": ";
cin >> userX;
while (userX > board.getSizeX() || userX < 0){
cout << "Enter a valid X: ";
cin >> userX;
}
userShip.setX(userX);
}
if (userShip.getY()){
// move on Y
choice = 'a';
cout << "Do you want to move on the Y axis (y/n)? ";
cin >> choice;
if (choice == 'Y' || choice == 'y') {
// move up?
choice = 'a';
cout << "Do you want to move up? (y/n) ";
cin >> choice;
if (choice == 'Y' || choice == 'y') {
forward = true;
cout << "your new Y position is: " << board.moveY(userShip, forward) << endl;
} else if (choice == 'n' || choice != 'N') {
forward = false;
cout << "your new Y position is: " << board.moveY(userShip, forward) << endl;
} else {
cout << "thats not a y or an n" << endl;
}
} else if (choice == 'n' || choice != 'N') {
cout << "Ok, you did not move." << endl;
} else {
cout << "thats not a y or an n" << endl;
}
} else {
cout << "Enter your starting Y position between 0 and " << board.getSizeY() << ": ";
cin >> userY;
while (userY > board.getSizeY() || userY < 0){
cout << "Enter a valid Y: ";
cin >> userY;
}
userShip.setY(userY);
}
cout << "Battle enemy ship? (Y/N) ";
// battle the enemy
cin >> choice;
if (choice == 'Y' || choice == 'y' ){
// count how many ships
++counter;
unique_ptr<Destroyer> computerPtr = make_unique<Destroyer>("Computer " + to_string(counter));
// computer position
computerPtr->setX(0);
computerPtr->setY(0);
cout << "Your distance from " << computerPtr->getName() << " is " << board.calcDistance(userShip, *computerPtr) << endl;
// call destroyerBattle function
destroyerBattle(userShip, *computerPtr);
} else if(choice == 'n' || choice != 'N') {
cout << "you're a wimp" << endl;
} else {
cout << "thats not a y or an n" << endl;
}
}
cout << "You were destroyed. Game Over." << endl;
}
// make user battleship function
Battleship makeUserBattleship() {
string name;
int hitPoints;
int firePower;
int accuracy;
int speed;
cout << "Enter the name of your Battleship: ";
cin >> name;
cout << "Enter the Hitpoints of your Battleship (Integer Between 100-30,000): ";
cin >> hitPoints;
if (hitPoints < 100 || hitPoints > 30000) {
cout << "Invalid hit points, set to default (1,000)" << endl;
hitPoints = 1000;
}
cout << "Enter the Fire Power of your Battleship (Between 50,000-1,000,000): ";
cin >> firePower;
if (firePower < 50000 || firePower > 1000000) {
cout << "Invalid fire power, set to default (50,000)" << endl;
firePower = 50000;
}
cout << "Enter the Accuracy of your Battleship (Between 1-100): ";
cin >> accuracy;
if (accuracy < 0 || accuracy > 100) {
cout << "Invalid pAccuracy, set to default (75)" << endl;
accuracy = 75;
}
cout << "Enter the Speed of your Battleship (Between 1-100): ";
cin >> speed;
if (speed < 0 || speed > 100){
cout << "Invalid pSpeed, set to default (50)" << endl;
speed = 50;
}
// make Battleship a unique pointer and return it to battleshipGame function
unique_ptr<Battleship> userShip = make_unique<Battleship>(name, hitPoints, firePower, accuracy, speed);
return *userShip;
}
// make user destroyer function
Destroyer makeUserDestroyer() {
string name;
int hitPoints;
int firePower;
int accuracy;
int speed;
cout << "Enter the name of your Battleship: ";
cin >> name;
cout << "Enter the Hitpoints of your Battleship (Integer Between 100-30,000): ";
cin >> hitPoints;
if (hitPoints < 100 || hitPoints > 30000) {
cout << "Invalid hit points, set to default (1,000)" << endl;
hitPoints = 1000;
}
cout << "Enter the Fire Power of your Battleship (Between 50,000-1,000,000): ";
cin >> firePower;
if (firePower < 50000 || firePower > 1000000) {
cout << "Invalid fire power, set to default (50,000)" << endl;
firePower = 50000;
}
cout << "Enter the Accuracy of your Battleship (Between 1-100): ";
cin >> accuracy;
if (accuracy < 0 || accuracy > 100) {
cout << "Invalid pAccuracy, set to default (75)" << endl;
accuracy = 75;
}
cout << "Enter the Speed of your Battleship (Between 1-100): ";
cin >> speed;
if (speed < 0 || speed > 100){
cout << "Invalid pSpeed, set to default (50)" << endl;
speed = 50;
}
// make Destroyer a unique pointer and return it to destroyerGame function
unique_ptr<Destroyer> userShip = make_unique<Destroyer>(name, hitPoints, firePower, accuracy, speed);
return *userShip;
}
// battle function
Battleship battle(Battleship &pOne, Battleship &pTwo){
if (pOne.getDestroyed() == false && pTwo.getDestroyed() == false) {
while (pOne.getDestroyed() == false && pTwo.getDestroyed() == false) {
pOne.fire(pTwo);
if (pTwo.getDestroyed() == false) {
pTwo.fire(pOne);
}
}
if (pOne.getDestroyed() == false) {
return pOne;
} else {
return pTwo;
}
} else{
if (pOne.getDestroyed()){
cout << "cannot battle, " << pOne.getName() << " is destroyed" << endl;
return pTwo;
} else{
cout << "cannot battle, " << pTwo.getName() << " is destroyed" << endl;
return pOne;
}
}
}
// destroyerBattle function
Destroyer destroyerBattle(Destroyer &pOne, Destroyer &pTwo){
if (pOne.getDestroyed() == false && pTwo.getDestroyed() == false) {
while (pOne.getDestroyed() == false && pTwo.getDestroyed() == false) {
pOne.fire(pTwo);
if (pTwo.getDestroyed() == false) {
pTwo.fire(pOne);
}
}
if (pOne.getDestroyed() == false) {
return pOne;
} else {
return pTwo;
}
} else{
if (pOne.getDestroyed()){
cout << "cannot battle, " << pOne.getName() << " is destroyed" << endl;
return pTwo;
} else{
cout << "cannot battle, " << pTwo.getName() << " is destroyed" << endl;
return pOne;
}
}
}