-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
322 lines (274 loc) · 8.76 KB
/
Copy pathSource.cpp
File metadata and controls
322 lines (274 loc) · 8.76 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
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>
#include "GameBoard.h"
#include "Position.h"
using namespace std;
const int NUMBER_OF_SHIPS = 5;
//Works
void Greet(string &userName)
{
cout << "BATTLESHIP" << endl << endl;
cout << "Created by Berkeley Andrus" << endl;
cout << "Press Enter to Continue." << endl;
cin.get();
cout << "Welcome to Battleship, Admiral. What is your name? ";
getline(cin, userName);
cout << endl;
cout << "Thanks, " << userName << ". I am your onboard computer system, and I will assist you through this war." << endl;
} //Works
//Works
bool IsNotValid(string &userInput)
{
if (userInput[0] >= 'a' && userInput[0] <= 'j')
{
userInput[0] += 'A' - 'a';
}
if (userInput.length() > 3 || userInput.length() < 2)
{
return true;
}
if (userInput[0] < 'A' || userInput[0] > 'J')
{
return true;
}
if (userInput.length() == 3 && userInput[1] == '1' && userInput[2] == '0')
{
return false;
}
if (userInput.length() == 2 && isdigit(userInput[1]) && userInput[1] != '0')
{
return false;
}
return true;
} //
//Works
void AddUserShips(GameBoard &user)
{
string userInput;
string junk;
int endOption = 0;
vector<Position> endOptions;
cout << "Our first task is to assemble our fleet! You have five ships to place on the 10 by 10 grid shown below." << endl;
cout << user.DisplayBoard("user") << endl << endl;
cout << "Press Enter to Continue." << endl;
cin.get();
cout << "For each ship, enter a letter (A-J) followed by a number (1-10). This will tell me where you want to start ";
cout << "each ship." << endl << "I will then give you the options of where the other end should be based on the ship's";
cout << " size and the available spots." << endl;
cout << "If you want to start over, just type 'R' and we'll delete all the ships and start again." << endl;
for (int i = 0; i < NUMBER_OF_SHIPS; i++)
{
cout << endl << "Enter the first end of your ship: ";
getline(cin, userInput);
//If "R", delete all ships and restart.
if (userInput == "R" || userInput == "r")
{
user.Reset();
cout << endl << "All ships have been removed. See, look! This is the ocean right now:" << endl;
cout << user.DisplayBoard("user");
i = -1;
continue;
}
//If not Letter:Number, ask for another one.
if (IsNotValid(userInput))
{
cout << endl << "Sorry, I didn't understand that. Please try again." << endl;
i--;
continue;
}
//If space is taken, ask for another one.
Position newestPos = Position(userInput);
if (user.IsShip(newestPos))
{
cout << "You already have a ship there!" << endl;
i--;
continue;
}
//Check which other ends are available.
endOptions = user.FindOptions(newestPos, i);
if (endOptions.size() == 0)
{
cout << "I'm sorry. If you start your ship there, it won't have anywhere to go!" << endl;
i--;
continue;
}
cout << "Okay, your ship can go from " << newestPos.ToString() << " to the following locations:" << endl;
for (int i = 0; i < endOptions.size(); i++)
{
cout << i + 1 << ": " << endOptions[i].ToString() << endl;
}
cout << "Please select one (1-" << endOptions.size() << "):";
cin >> endOption;
while (cin.fail() || endOption < 1 || endOption > endOptions.size())
{
cin.clear();
cin.ignore(UINT16_MAX, '\n');
cout << "Hold up, that wasn't an option. Try again: " << endl;
cin >> endOption;
}
cout << user.AddShip(newestPos, endOptions[endOption - 1]);
cin.get();
}
}
//Works
void AddOpponentShips(GameBoard &opponent)
{
cout << "Alright, it looks like your opponent is assembling for war." << endl;
cout << "This may take as long as .04 seconds, so please be patient." << endl;
cout << "Press Enter to Continue." << endl;
cin.get();
char randomY;
int randomX;
int endOption = 0;
vector<Position> endOptions;
for (int i = 0; i < NUMBER_OF_SHIPS; i++)
{
randomY = rand() % 9 + 'A';
randomX = rand() % 9 + 1;
//If space is taken, ask for another one.
Position newestPos = Position(randomX, randomY);
if (opponent.IsShip(newestPos))
{
i--;
continue;
}
//Check which other ends are available.
endOptions = opponent.FindOptions(newestPos, i);
if (endOptions.size() == 0)
{
i--;
continue;
}
endOption = rand() % endOptions.size();
opponent.AddShip(newestPos, endOptions[endOption]);
}
//cout << "Opponent's ships: " << endl;
//cout << opponent.DisplayBoard("user");
}
void HumanTurn(GameBoard &user, GameBoard&opponent, string userName, int &userFleet, int &opponentFleet)
{
string userGuess;
int result = 0;
cout << "Here's the enemy waters: " << endl;
cout << opponent.DisplayBoard("opp");
cout << "Where would you like to fire, " << userName << "? (Enter the letter first): ";
getline(cin, userGuess);
while (IsNotValid(userGuess))
{
cout << endl << "Sorry, I didn't understand that. Please try again." << endl;
cout << endl << "Where would you like to fire? ";
getline(cin, userGuess);
}
result = opponent.Guess(Position(userGuess));
switch (result)
{
case INVALID_GUESS:
cout << endl << "Looks like you've already fired there, Admiral. Try again." << endl;
return HumanTurn(user, opponent, userName, userFleet, opponentFleet);
case MISS:
cout << endl << endl << userName << " guessed " << userGuess << " and missed! Better luck next time." << endl;
cout << "Here's what we've gathered so far:" << endl;
cout << opponent.DisplayBoard("opp");
break;
case HIT:
cout << endl << endl << userName << " guessed " << userGuess << " and hit something! Nice work!" << endl;
cout << "Here's what we've gathered so far:" << endl;
cout << opponent.DisplayBoard("opp");
break;
case SUNK:
cout << endl << endl << "YOU'VE SUNK THE OPPONENT'S BATTLESHIP! Or, well, one of its ships. I never learned ";
cout << "the names of all of them..." << endl;
opponentFleet--;
if (opponentFleet == 0)
{
return;
}
cout << "Here's what we've gathered so far:" << endl;
cout << opponent.DisplayBoard("opp");
break;
default:
cout << "Error. BIIIIG Error." << endl;
break;
}
}
void AutoTurn(GameBoard &user, GameBoard&opponent, string userName, int &userFleet, int &opponentFleet)
{
int result = opponent.MakeAutoGuess(user);
switch (result)
{
case HIT:
cout << "Your opponent has struck you, Admiral! It got a hit at " << user.GetLastGuess() << "." << endl;
cout << "Here's your board: " << endl;
cout << user.DisplayBoard("user");
cout << "Your turn is next. Let's get back at those scoundrels!" << endl;
return;
case MISS:
cout << "HA! Your opponent missed you by a mile. Or an inch. I don't really understand numbers." << endl;
cout << "It shot at " << user.GetLastGuess() << " but you're safe." << endl;
cout << "Here's your board: " << endl;
cout << user.DisplayBoard("user");
cout << "Your turn is next. Let's show these guys who's boss." << endl;
return;
case SUNK:
cout << "Bad news, Admiral. It looks like you've lost a ship. You were struck at " << user.GetLastGuess() << "." << endl;
userFleet--;
if (userFleet)
{
cout << "But you're not done yet! Here's the remainder of your fleet." << endl << user.DisplayBoard("user");
cout << "Let's hit them where it hurts!" << endl;
}
return;
default:
cout << "Um, something went wrong. I don't know what happened." << endl;
return;
}
}
void Play(GameBoard &user, GameBoard&opponent, string userName)
{
int userFleet = 5;
int opponentFleet = 5;
string userGuess;
string opponentGuess;
cout << "Well, since you are the human one, I guess you can go first." << endl;
while (userFleet && opponentFleet)
{
HumanTurn(user, opponent, userName, userFleet, opponentFleet);
cout << "Press enter to let the robot guess." << endl;
cin.get();
AutoTurn(user, opponent, userName, userFleet, opponentFleet);
cout << "Press enter to continue." << endl;
cin.get();
}
cout << "Well, that's the game!" << endl;
if (userFleet)
{
cout << "Congratulations, Admiral " << userName << "! You won! Here were both boards: " << endl;
cout << user.DisplayBoard("user") << opponent.DisplayBoard("user");
}
else
{
cout << "I'm sorry, Admiral. You have lost the game." << endl;
cout << user.DisplayBoard("user") << opponent.DisplayBoard("user");
}
return;
}
int main()
{
srand(time(NULL));
string userName;
GameBoard user = GameBoard();
GameBoard opponent = GameBoard();
Greet(userName);
AddUserShips(user);
AddOpponentShips(opponent);
Play(user, opponent, userName);
cout << "Press enter to continue.";
cin.get();
return 0;
}
/*
1. Populate opponent's GameBoard
*/