-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
388 lines (366 loc) · 17.3 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
/*
* Email: [email protected]
* ID: ****6281
* @SamuraiPolix - Samuel Lazareanu
*/
/*
* Main exec file for Ex3.
*/
// FAST START BEGINNING GOOD PLACEMENTS: 29 29 30 20 20 21 33 33 34 24 24 25 10 10 9 41 41 42
#include <iostream>
#include <stdexcept>
#include <vector>
#include "Catan.hpp"
#include "Player.hpp"
#include "Types.hpp"
using std::exception, std::cout, std::endl, std::vector, std::string, std::cin;
using namespace ariel;
#define NUM_OF_PLAYER 3
void dealWithTrade(Player& currPlayer, Catan& catan);
int useDevelopmentCard(Player& currPlayer, Catan& catan);
int main()
{
Player p1("Amit");
Player p2("Yossi");
Player p3("Dana");
Catan catan(p1, p2, p3);
// Starting of the game. Every player places two settlements and two roads.
catan.ChooseStartingPlayer(); // should print the name of the starting player
Board* board = catan.getBoard();
catan.printBoard();
for (int i = 0; i < 2; i++){ // 2 settlements and 2 roads for each player
for (int playerInd = 0; playerInd < NUM_OF_PLAYER; playerInd++){
Player& currPlayer = catan.getCurrentPlayer();
cout << "Player " << currPlayer.getName() << " turn" << endl;
cout << "Choose a vertex to place a settlement" << endl;
int status = -1;
do {
size_t vertexChoice = 0;
cin >> vertexChoice;
while (vertexChoice < 0){
cout << "Invalid vertex choice" << endl;
cin >> vertexChoice;
}
try {
status = currPlayer.placeSettlement(vertexChoice, board);
} catch (const std::invalid_argument& e){
cout << e.what() << endl;
status = -1;
cout << "Please choose another vertex" << endl;
}
} while (status == -1);
cout << "Choose an edge to place a road (enter 2 vertex numbers, seperated by a space!)" << endl;
do {
size_t vertex1, vertex2;
cin >> vertex1;
while (vertex1 < 0){
cout << "Invalid vertex choice" << endl;
cin >> vertex1;
}
cin >> vertex2;
while (vertex2 < 0){
cout << "Invalid vertex choice" << endl;
cin >> vertex2;
}
try {
status = currPlayer.placeRoad(vertex1, vertex2, board);
} catch (const std::invalid_argument& e){
cout << e.what() << endl;
status = -1;
cout << "Please choose another" << endl;
}
} while (status == -1);
currPlayer.endTurn(catan);
catan.printBoard();
}
}
// main game loop
while (true)
{
catan.printBoard();
Player& currPlayer = catan.getCurrentPlayer();
cout << "Player " << currPlayer.getName() << " turn" << endl;
int status1 = -1;
int firstMenuChoice = -1;
while (status1 < 0) {
cout << "Choose your action:" << endl;
cout << "1. Throw cubes" << endl;
cout << "2. Use development card (You can also use a card after throwing cubes)" << endl;
cin >> firstMenuChoice;
if (firstMenuChoice == 1){
currPlayer.rollDice(catan);
int status = -1;
int secondMenuChoice = -1;
while (status < 0 || secondMenuChoice != 6){
catan.printPlayers(); // print updated player resources
cout << currPlayer.getName() << ", choose your action:" << endl;
cout << "1. Build a settlement (1 Wood, 1 Brick, 1 Sheep, 1 Wheat)" << endl;
cout << "2. Build a road (1 Wood, 1 Brick)" << endl;
cout << "3. Buy a development card (1 Ore, 1 Wheat, 1 Sheep)" << endl;
cout << "4. Use a development card" << endl;
cout << "5. Trade" << endl;
cout << "6. End turn" << endl;
cin >> secondMenuChoice;
if (secondMenuChoice == 1){
if (currPlayer.availableResourcesBuildable(BuildableTypes::Settlement) == false){
cout << "You don't have enough resources to build a settlement, choose another action" << endl;
continue;
}
cout << "Choose a vertex to place a settlement" << endl;
status = -1;
size_t vertexChoice = 0;
do {
cin >> vertexChoice;
while (vertexChoice < 0){
cout << "Invalid choice" << endl;
cin >> vertexChoice;
}
try {
status = currPlayer.placeSettlement(vertexChoice, board);
} catch (const std::invalid_argument& e){
cout << e.what() << endl;
status = -1;
} catch (const valid_resources& e){
cout << e.what() << endl;
status = -2;
}
if (status == -1){
cout << "Please choose another" << endl;
}
else if (status == -2){
cout << "Please choose another action" << endl;
// goes over loop again
}
} while (status == -1);
}
else if (secondMenuChoice == 2){
if (currPlayer.availableResourcesBuildable(BuildableTypes::Road) == false){
cout << "You don't have enough resources to build a road, choose another action" << endl;
continue;
}
cout << "Choose an edge to place a road" << endl;
status = -1;
do {
size_t vertex1, vertex2;
cin >> vertex1;
while (vertex1 < 0){
cout << "Invalid vertex choice" << endl;
cin >> vertex1;
}
cin >> vertex2;
while (vertex2 < 0){
cout << "Invalid vertex choice" << endl;
cin >> vertex2;
}
try {
status = currPlayer.placeRoad(vertex1, vertex2, board);
} catch (const std::invalid_argument& e){
cout << e.what() << endl;
status = -1;
} catch (const valid_resources& e){
cout << e.what() << endl;
status = -2;
}
if (status == -1){
cout << "Please choose another" << endl;
}
else if (status == -2){
cout << "Please choose another action" << endl;
// goes over loop again
}
} while (status == -1);
}
else if (secondMenuChoice == 3){
if (currPlayer.availableResourcesDevelopmentCard() == false){
cout << "You don't have enough resources to buy a development card, choose another action" << endl;
continue;
}
status = -1;
size_t cardIndex = INT16_MAX;
try {
cardIndex = currPlayer.buyDevelopmentCard(catan);
} catch (const std::invalid_argument& e){
cout << e.what() << endl;
status = -1;
} catch (const valid_resources& e){
cout << e.what() << endl;
status = -2;
cout << "Please choose another action" << endl;
}
cout << "Do you want to use the development card? (1 for yes, 0 for no)" << endl;
int useCard = -1;
do {
cin >> useCard;
if (useCard == 1){
currPlayer.useDevelopmentCard(cardIndex);
// End turn after using a card
secondMenuChoice = 6;
break;
}
if (useCard != 0 && useCard != 1){
cout << "Invalid choice!" << endl;
}
} while (useCard != 0 && useCard != 1);
}
else if (secondMenuChoice == 4){
status = useDevelopmentCard(currPlayer, catan);
if (status >= 0){
secondMenuChoice = 6; // end turn after using a card
break;
}
}
else if (secondMenuChoice == 5){
dealWithTrade(currPlayer, catan);
}
else if (secondMenuChoice == 6){
break; // go to end of loop to end turn
}
else {
cout << "Invalid choice" << endl;
continue;
}
}
if (secondMenuChoice == 6){
break;
}
}
else if (firstMenuChoice == 2){
status1 = useDevelopmentCard(currPlayer, catan);
if (status1 >= 0){
// go to end of loop to end turn after using card
break;
}
}
else {
cout << "Invalid choice" << endl;
continue;
}
}
currPlayer.endTurn(catan);
// check if someone won
if (catan.printWinner() == 1){
break;
}
}
return 0;
}
void dealWithTrade(Player& currPlayer, Catan& catan){
int chosenPlayer = -1;
do {
cout << currPlayer.getName() << ", choose a player to trade with (enter the number next to the name)" << endl;
catan.printPlayersExceptCurrent();
cin >> chosenPlayer;
// TODO add check for invalid choice (current player or non existing player, usign a function in Catan class)
} while (chosenPlayer < 0 || chosenPlayer > 2);
vector<tuple<ResourceType, size_t>> sendingResources;
vector<tuple<ResourceType, size_t>> receivingResources;
int choiceSend = -1, choiceReceive = -1;
size_t amountSend = 0, amountReceive = 0;
int chooseAnother = -1;
// choose a resource to send
do {
do {
cout << "Choose a resource to send" << endl;
cout << "0. None" << endl;
cout << "1. Ore" << " (" << currPlayer.getResourceAmount(ResourceType::Ore) << " available)" << endl;
cout << "2. Wheat" << " (" << currPlayer.getResourceAmount(ResourceType::Wheat) << " available)" << endl;
cout << "3. Sheep" << " (" << currPlayer.getResourceAmount(ResourceType::Sheep) << " available)" << endl;
cout << "4. Wood" << " (" << currPlayer.getResourceAmount(ResourceType::Wood) << " available)" << endl;
cout << "5. Brick" << " (" << currPlayer.getResourceAmount(ResourceType::Brick) << " available)" << endl;
cin >> choiceSend;
if (choiceSend < 0 || choiceSend > 5){
cout << "Invalid choice. Choose again:" << endl;
}
if (choiceSend != 0 && currPlayer.getResourceAmount((ResourceType)choiceSend) == 0){
cout << "You don't have any of this resource. Choose again:" << endl;
choiceSend = -1;
}
} while (choiceSend < 0 || choiceSend > 5);
// choose amout of resource to send
while (choiceSend != 0 && (amountSend < 1 || amountSend > currPlayer.getResourceAmount((ResourceType)choiceSend))) {
cout << "Choose the amount of selected resource to send" << endl;
cout << "You have " << currPlayer.getResourceAmount((ResourceType)choiceSend)<< " of chosen resource" << endl;
cin >> amountSend;
if (amountSend < 1 || amountSend > currPlayer.getResourceAmount((ResourceType)choiceSend)){
cout << "Invalid amount. Choose again:" << endl;
}
}
sendingResources.push_back(std::make_tuple((ResourceType)choiceSend, amountSend));
chooseAnother = -1;
if (choiceSend != 0){
cout << "Do you want to choose another resource to send? (1 for yes, 0 for no)" << endl;
cin >> chooseAnother;
while (chooseAnother != 0 && chooseAnother != 1){
cout << "Invalid choice. Choose again:" << endl;
cin >> chooseAnother;
}
}
amountSend = 0;
choiceSend = -1;
} while (chooseAnother == 1);
chooseAnother = -1;
// choose a resource to receive
do {
do {
cout << "Choose a resource to receive" << endl;
cout << "0. None" << endl;
cout << "1. Ore" << " (" << catan.getPlayerById(chosenPlayer).getResourceAmount(ResourceType::Ore) << " available)" << endl;
cout << "2. Wheat" << " (" << catan.getPlayerById(chosenPlayer).getResourceAmount(ResourceType::Wheat) << " available)" << endl;
cout << "3. Sheep" << " (" << catan.getPlayerById(chosenPlayer).getResourceAmount(ResourceType::Sheep) << " available)" << endl;
cout << "4. Wood" << " (" << catan.getPlayerById(chosenPlayer).getResourceAmount(ResourceType::Wood) << " available)" << endl;
cout << "5. Brick" << " (" << catan.getPlayerById(chosenPlayer).getResourceAmount(ResourceType::Brick) << " available)" << endl;
cin >> choiceReceive;
if (choiceReceive < 0 || choiceReceive > 5){
cout << "Invalid choice. Choose again:" << endl;
}
if (choiceReceive != 0 && catan.getPlayerById(chosenPlayer).getResourceAmount((ResourceType)choiceReceive) == 0){
cout << "They don't have any of this resource. Choose again:" << endl;
choiceReceive = -1;
}
} while (choiceReceive < 0 || choiceReceive > 5);
// choose amount of resource to receive
while (choiceReceive != 0 && (amountReceive < 1 || amountReceive > catan.getPlayerById(chosenPlayer).getResourceAmount((ResourceType)choiceReceive))) {
cout << "Choose the amount of resources to receive" << endl;
cout << "They have " << catan.getPlayerById(chosenPlayer).getResourceAmount((ResourceType)choiceReceive)<< " of chosen resource" << endl;
cin >> amountReceive;
if (amountReceive < 1 || amountReceive > catan.getPlayerById(chosenPlayer).getResourceAmount((ResourceType)choiceReceive)){
cout << "Invalid amount. Choose again:" << endl;
}
}
receivingResources.push_back(std::make_tuple((ResourceType)choiceReceive, amountReceive));
chooseAnother = -1;
if (choiceReceive != 0){
cout << "Do you want to choose another resource to receive? (1 for yes, 0 for no)" << endl;
cin >> chooseAnother;
while (chooseAnother != 0 && chooseAnother != 1){
cout << "Invalid choice. Choose again:" << endl;
cin >> chooseAnother;
}
}
amountReceive = 0;
choiceReceive = -1;
} while (chooseAnother == 1);
// currPlayer.trade(catan.getPlayerById(chosenPlayer), (ResourceType)choiceSend, (ResourceType)choiceReceive, amountSend, amountReceive);
currPlayer.trade(catan.getPlayerById(chosenPlayer), sendingResources, receivingResources);
}
int useDevelopmentCard(Player& currPlayer, Catan& catan){
cout << endl;
size_t numOfCards = currPlayer.printDevelopmentCards();
if (numOfCards == 0){
cout << "You don't have any development cards, choose another action!" << endl;
return -2;
}
size_t cardChoice = 0;
cout << "Please choose a card to use (write its number and press enter) or -1 to cancel:" << endl;
do {
cin >> cardChoice;
if (cardChoice == -1){
return -1;
}
if (cardChoice >= numOfCards){
cout << "Please choose another " << endl;
}
} while (cardChoice >= numOfCards);
currPlayer.useDevelopmentCard(cardChoice);
return 1;
}