-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.cpp
More file actions
261 lines (240 loc) · 9.25 KB
/
testing.cpp
File metadata and controls
261 lines (240 loc) · 9.25 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
// George Fafard and Sean Cosgrove
// Lisa Dion
// CS 120
// Project 3: Battleship
#include <string>
#include <iostream>
#include <optional>
#include <memory>
#include <cmath>
#include "battleship.h"
#include "destroyer.h"
#include "board.h"
using namespace std;
/********** Global functions declarations **********/
// 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 destroyers
// Effects: fires until one is destroyed (takes turns, destroyer one goes first). returns the winner.
Destroyer destroyerBattle(Destroyer &pOne, Destroyer &pTwo);
// Tester functions
// test battle
// Requires: nothing
// Modifies: two Battleship objects
// Effects: calls battle function
void testBattle();
// test destroyer battle
// Requires: nothing
// Modifies: two Destroyer objects
// Effects: call destroyerBattle function
void testDestroyerBattle();
// test calc distance
// Requires: nothing
// Modifies: board and Battleship x and y coordinates
// Effects: tests movement around the board
void testCalcDistance();
// test fire, fire is a member of the Battleship class
// Requires: target ship
// Modifies: hitpoints of target ship
// Effects: lowers the HP of target ship if firing ship is not destroyed
void testFire();
// test destroyer fire, fire is overwritten in Destroyer class
// Requires: target ship
// Modifies: hitpoints of target ship
// Effects: lowers the HP of target ship if firing ship is not destroyed
void testDestroyerFire();
// test moveX and moveY (members of board)
// requires: a Battleship, a board, and a boolean fowrard
// modifies: the ships position
// effects: changes the ships position based on its speed
void testMoves();
// MAIN
int main(){
testFire();
testDestroyerFire();
testBattle();
testDestroyerBattle();
testCalcDistance();
testMoves();
return 0;
};
/********** Global function definitions **********/
// testFire function
void testFire(){
cout << "==========================================================================================================" << endl;
Battleship one("One");
Battleship two("Two");
cout << "Testing the Fire function. Expected: Two took 657 damage from One; Two now has 343 hit points." << endl;
cout << "Actual: ";
one.fire(two);
}
// testDestroyerFire function
void testDestroyerFire() {
cout << "==========================================================================================================" << endl;
Destroyer one("One");
Destroyer two("Two", 1100, 50000, 50, 50);
cout << "Testing the Destroyer Fire function. Expected: Two took 1098 damage from One; Two now has 2 hit points." << endl;
cout << "Actual: ";
one.fire(two);
}
// testBattle function
void testBattle(){
cout << "==========================================================================================================" << endl;
Battleship one("One");
Battleship two("Two");
cout << "Testing Battle function. Expected: Two is destroyed by a shot from One." << endl;
cout << "Actual: ";
battle(one, two);
cout << "Testing Battle function. expected: cannot battle" << endl;
battle(one, two);
cout << "Testing Battle function. expected: cannot battle" << endl;
battle(two, one);
Battleship three("Three");
cout << "Testing Battle function. Expected: one is destroyed by a shot from 3" << endl;
battle(one, three);
}
// testDestroyerBattle function
void testDestroyerBattle() {
cout << "==========================================================================================================" << endl;
Destroyer one("One");
Destroyer two("Two");
cout << "Testing Destroyer Battle function. Expected: Two is destroyed by a shot from One." << endl;
cout << "Actual: ";
destroyerBattle(one, two);
cout << "Testing Destroyer Battle function. expected: cannot battle" << endl;
destroyerBattle(one, two);
cout << "Testing Destroyer Battle function. expected: cannot battle" << endl;
destroyerBattle(two, one);
Destroyer three("Three", 5000, 1000000, 100, 100);
cout << "Testing Destroyer Battle function. Expected: One is destroyed by a shot from Three" << endl;
destroyerBattle(one, three);
}
// 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;
}
}
}
// testCalcDistance function
void testCalcDistance(){
cout << "==========================================================================================================" << endl;
Board board = Board();
Battleship one("One");
Battleship two("Two");
one.setX(3);
one.setY(4);
two.setX(0);
two.setY(0);
cout << "Testing calcDistance function. One: (3, 4) Two: (0, 0) Expected: 5" << endl;
cout << "actual: " << board.calcDistance(one, two) << endl;
one.setX(0);
one.setY(0);
two.setX(0);
two.setY(0);
cout << "Testing calcDistance function. One: (0, 0) Two: (0, 0) Expected: 0" << endl;
cout << "actual: " << board.calcDistance(one, two) << endl;
one.setX(1);
one.setY(2);
two.setX(1);
two.setY(2);
cout << "Testing calcDistance function. One: (1, 2) Two: (1, 2) Expected: 0" << endl;
cout << "actual: " << board.calcDistance(one, two) << endl;
one.setX(0);
one.setY(0);
two.setX(6);
two.setY(8);
cout << "Testing calcDistance function. One: (0, 0) Two: (6, 8) Expected: 10" << endl;
cout << "actual: " << board.calcDistance(one, two) << endl;
one.setX(-3);
one.setY(-4);
two.setX(0);
two.setY(0);
cout << "Testing calcDistance function. One: (-3, -4) Two: (0, 0) Expected: 5" << endl;
cout << "actual: " << board.calcDistance(one, two) << endl;
one.setX(-3);
one.setY(-4);
two.setX(-8);
two.setY(-6);
cout << "Testing calcDistance function. One: (-3, -4) Two: (-8, -6) Expected: ~5" << endl;
cout << "actual: " << board.calcDistance(one, two) << endl;
one.setX(3);
one.setY(4);
two.setX(8);
two.setY(6);
cout << "Testing calcDistance function. One: (3, 4) Two: (8, 6) Expected: ~5" << endl;
cout << "actual: " << board.calcDistance(one, two) << endl;
one.setXNull();
one.setYNull();
two.setXNull();
two.setYNull();
cout << "Testing calcDistance function. X: null Y: null Expected: 0" << endl;
cout << "actual: " << board.calcDistance(one, two) << endl;
}
// testMoves function
void testMoves(){
cout << "==========================================================================================================" << endl;
Battleship one = Battleship("One");
Board board = Board();
cout << "Setting One's initial position to 0, 0" << endl;
one.setX(0);
one.setY(0);
cout << "Testing move forward X, should become 50. new Position: " << board.moveX(one, true) << endl;
cout << "Testing move backward X, should become 0. new Position: " << board.moveX(one, false) << endl;
cout << "Testing move Y, should become 50. new Position: " << board.moveY(one, true) << endl;
cout << "Testing move down Y, should become 0. new Position: " << board.moveY(one, false) << endl;
cout << "Testing no negative movements." << endl;
cout << "Testing move down Y, should remain 0. new Position: " << board.moveY(one, false) << endl;
cout << "Testing move backward X, should remain 0. new Position: " << board.moveX(one, false) << endl;
one.setX(board.getSizeX());
one.setY(board.getSizeY());
cout << "Testing boundary movements." << endl;
cout << "Moving out of bounds X, should stay 1000. new Position: " << board.moveX(one, true) << endl;
cout << "Moving out of bounds Y, should stay 1000. new Position: " << board.moveY(one, true) << endl;
}