-
Notifications
You must be signed in to change notification settings - Fork 2
/
game.cpp
464 lines (401 loc) · 11.6 KB
/
game.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "game.h"
#include "id.h"
/*
static void OnKeyPressed(Ifel* el, int key)
{
switch(key)
{
case 'y':
break;
case 'n':
break;
}
}
*/
static void OnMouseClick(Ifel* i)
{
//fprintf(stderr, "OnMouseClick in %i\n", i->id);
switch(i->id)
{
case ID_BTN_OPTIONS:
break;
case ID_MSGBOX_ROLL:
case ID_BTN_ROLL:
break;
}
}
CGame::CGame(CGameUI& ui) : m_ui(ui),
gameOver(true),
players(0),
doublesCount(0),
currentPlayer(0)
{
}
bool CGame::IsCurrentPlayerInJail()
{
return player[currentPlayer].inJail;
}
void CGame::NewGame()
{
fprintf(stderr, "New game\n");
// reset ownership of all properties
for (int i=0; i < NUM_PROPERTIES; i++)
owner[i] = -1;
AddPlayer(ID_IMG_SHOE, "Joel");
m_ui.SetTokenActive(ID_IMG_SHOE);
AddPlayer(ID_IMG_DOG, "Merav");
m_ui.SetTokenActive(ID_IMG_SHOE);
srand(time(NULL));
currentPlayer = rand() % players;
gameOver = false;
fprintf(stderr, "player %s starts\n", GetCurrentPlayer()->name);
StartTurn();
}
Player* CGame::GetCurrentPlayer()
{
return &player[currentPlayer];
}
void CGame::Over()
{
gameOver = true;
m_ui.DisableStatusArea();
}
void CGame::PlayerOutOfBusiness(int id)
{
player[id].active = 0;
players--;
if (players == 1)
Over();
}
unsigned int CGame::ActivePlayers()
{
return players;
}
int CGame::HasAssets(int playerID)
{
// do you have anything to mortgage?
// do you have any houses you can sell?
return 0;
}
void CGame::PayMoney(int from, int to, int amount)
{
if (from != PLAYER_BANK)
{
//fprintf(stderr, "current: %i amount: %i\n", player[from].money, amount);
player[from].money -= amount;
// check to see if he is out of money
while (player[from].money < 0 && HasAssets(from))
{
//fprintf(stderr, "Out of money!\n");
m_ui.LiquidateAssets();
}
if (player[from].money < 0)
PlayerOutOfBusiness(from);
}
if (to != PLAYER_BANK)
player[to].money += amount;
}
void CGame::FinishTurn()
{
// reset the doubles counter
doublesCount = 0;
// go to the next active player's turn
currentPlayer = (currentPlayer + 1) % players;
while (!player[currentPlayer].active)
currentPlayer = (currentPlayer + 1) % players;
}
void CGame::SetPlayerName(unsigned int id, const char* name)
{
if (player[id].name)
{
free(player[id].name);
player[id].name = 0;
}
//player[id].name = (char*)malloc(strlen(name) + 1);
//strcpy(player[id].name, name);
player[id].name = strdup(name);
//fprintf(stderr, "set player %i name to %s\n", id, player[id].name);
}
bool CGame::AddPlayer(int token, const char* name)
{
if (players >= MAX_PLAYERS)
{
//fprintf(stderr, "Maximum players %i is reached\n", MAX_PLAYERS);
return false;
}
//fprintf(stderr, "Adding player %s\n", name);
player[players].token = token;
player[players].active = 1;
player[players].inJail = 0;
player[players].location = 0;
player[players].money = STARTING_CASH;
SetPlayerName(players, name);
player[players].index = players;
players++;
return true;
}
CGame::~CGame()
{
for (int i=0; i < MAX_PLAYERS; i++)
{
if (player[i].name)
{
free(player[i].name);
player[i].name = 0;
}
}
}
void CGame::DoublesCountInc()
{
doublesCount++;
}
void CGame::DoublesCountReset()
{
doublesCount = 0;
}
unsigned int CGame::DoublesCount()
{
return doublesCount;
}
void CGame::SetCurrentPlayerSquare(int square)
{
player[currentPlayer].location = square;
}
int CGame::GetCurrentPlayerSquare()
{
return player[currentPlayer].location;
}
void CGame::MovePlayer(int square)
{
int startSquare = GetCurrentPlayerSquare();
if (square == ACCORDING_TO_DICE)
{
// first see if the player is in jail
if (IsCurrentPlayerInJail())
{
player[currentPlayer].turnsLeftInJail--;
//printf("%s is in jail - %i rolls left\n", player[currentPlayer].name, player[currentPlayer].turnsLeftInJail);
doublesCount = 0; // can't accumulate doubles in jail
// if you got doubles, you're out!
if (dice[0] == dice[1])
{
//printf("%s is out of jail (rolled doubles)\n", player[currentPlayer].name);
player[currentPlayer].inJail = 0;
player[currentPlayer].turnsLeftInJail = 0;
doublesCount++;
SetCurrentPlayerSquare(SQUARE_JUST_VISITING);
startSquare = GetPlayerSquare(currentPlayer);
// from here, join the normal flow as if we had been in 'just visiting'
}
else
{
if (player[currentPlayer].turnsLeftInJail == 0)
{
// check if the player has a get out of jail free card
// ask if the player wants to use a get out of jail free card
// no? then he has to pay
//printf("%s has to pay to get out of jail\n", player[currentPlayer].name);
PayMoney(currentPlayer, PLAYER_BANK, 50);
player[currentPlayer].inJail = 0;
player[currentPlayer].turnsLeftInJail = 0;
SetPlayerSquare(currentPlayer, SQUARE_JUST_VISITING);
startSquare = GetPlayerSquare(currentPlayer);
//printf("%s is out of jail (paid $50)\n", player[currentPlayer].name);
}
}
}
else
{
// check for doubles
if (dice[0] == dice[1])
DoublesCountInc(); //doublesCount++;
else
DoublesCountReset();// player[currentPlayer].doublesCount = 0;
}
// did you roll too many doubles in a row?
if (DoublesCount() == 3)
{
//printf("Too many doubles - going to jail\n");
SetCurrentPlayerSquare(SQUARE_IN_JAIL);
}
else
{
SetCurrentPlayerSquare((startSquare + dice[0] + dice[1]) % NUM_PROPERTIES);
}
}
else
{
// jump to a specific square, like 'jail', or something from a 'chance' or 'community chest'
SetPlayerSquare(currentPlayer, square);
}
int endSquare = GetPlayerSquare(currentPlayer);
//printf("%s landed on %s\n", player[currentPlayer].name, board[endSquare].name);
// did you pass go? (and not go to jail)
if ((endSquare != SQUARE_IN_JAIL) && (endSquare < startSquare))
{
//printf("Passed GO - got $200\n");
PayMoney(PLAYER_BANK, currentPlayer, 200);
}
// check the special squares first
int handled = 0;
switch (endSquare)
{
case SQUARE_GO:
//printf("Got $200\n");
PayMoney(PLAYER_BANK, currentPlayer, 200);
handled = 1;
break;
case SQUARE_INCOME_TAX:
//printf("Pay income tax\n");
// give option to calculate 10% of assets - JKN
PayMoney(currentPlayer, PLAYER_BANK, 200);
handled = 1;
break;
case SQUARE_JUST_VISITING:
case SQUARE_FREE_PARKING:
//printf("Nothing happens - this time\n");
handled = 1;
break;
case SQUARE_LUXURY_TAX:
//printf("Pay luxury tax\n");
PayMoney(currentPlayer, PLAYER_BANK, 75);
handled = 1;
break;
case SQUARE_GO_TO_JAIL:
//printf("That's it - off to jail\n");
SetPlayerSquare(currentPlayer, SQUARE_IN_JAIL);
case SQUARE_IN_JAIL:
//printf("%s is in jail\n", player[currentPlayer].name);
player[currentPlayer].inJail = 1;
player[currentPlayer].turnsLeftInJail = 3;
handled = 1;
break;
// community chest
case 2:
case 17:
case 33:
//printf("Pick a CC card\n");
handled = 1;
break;
case 7:
case 22:
case 36:
//printf("Pick a Chance card\n");
handled = 1;
break;
}
if (!handled)
{
// otherwise, it is a property. Is it unowned?
if (board.Property(endSquare).m_owner == -1)
{
// if you have enough money, do you want to buy the property?
if (player[currentPlayer].money < board.Property(endSquare).m_value)
{
//printf("You don't have enough money!\n");
//goto nomoney;
}
if (m_ui.BuyProperty(board.Property(endSquare)))
{
player[currentPlayer].money -= board.Property(endSquare).m_value;
board.Property(endSquare).m_owner = currentPlayer;
//printf("%s bought %s\n", player[currentPlayer].name, board[endSquare].name);
// check to see if the player owns a set
}
else
{
//nomoney:
// no money, or no will
//printf("auctions not implemented yet\n");
// Auction(endSquare);
}
}
else
{
// property is already owned
int rent;
int owner = board.Property(endSquare).m_owner;
if (owner == currentPlayer)
{
//printf("owned by you!\n");
}
else
{
//printf("owned by %s\n", player[owner].name);
if (board.Property(endSquare).m_mortgaged)
{
//printf("mortgaged\n");
}
else
{
if (IS_RAILROAD(endSquare))
{
int rrCount = 0;
// count how many railroads are owned
if (board.Property(SQUARE_READING_RR).m_owner == owner)
rrCount++;
if (board.Property(SQUARE_PENNSYLVANIA_RR).m_owner == owner)
rrCount++;
if (board.Property(SQUARE_BO_RR).m_owner == owner)
rrCount++;
if (board.Property(SQUARE_SHORT_LINE_RR).m_owner == owner)
rrCount++;
// double rent for each additional railroad
int houses = board.Property(endSquare).m_numHouses;
rent = board.Property(endSquare).m_rent[houses] << (rrCount-1);
}
else if ( endSquare == SQUARE_ELECTRIC_CO
|| endSquare == SQUARE_WATER_WORKS)
{
int owner = board.Property(endSquare).m_owner;
// one utility is x4, both utilities is x10
if (board.Property(SQUARE_ELECTRIC_CO).m_owner == owner
&& board.Property(SQUARE_WATER_WORKS).m_owner == owner)
rent = (dice[0] + dice[1]) * 10;
else
rent = (dice[0] + dice[1]) * 4;
}
else
{
int houses = board.Property(endSquare).m_numHouses;
rent = board.Property(endSquare).m_rent[houses];
}
//printf("%s owes %i in rent\n", player[currentPlayer].name, rent);
PayMoney(currentPlayer, board.Property(endSquare).m_owner, rent);
}
}
}
}
// if you got doubles and you are not in jail, roll again
if (dice[0] == dice[1] && !player[currentPlayer].inJail)
{
m_ui.Doubles(OnMouseClick);
}
else
{
FinishTurn();
}
}
void CGame::RollDice(void)
{
dice[0] = (rand() % 6) + 1;
dice[1] = (rand() % 6) + 1;
//fprintf(stderr, "dice: %i %i\n", dice[0], dice[1]);
}
void CGame::SetPlayerSquare(int id, int square)
{
player[id].location = square;
m_ui.SetPlayerSquare(player[id].token, board.Property(square).m_loc);
}
int CGame::GetPlayerSquare(int id)
{
return player[id].location;
}
int CGame::StartTurn()
{
m_ui.SetCurrentPlayerStatus(player[currentPlayer]);
return 0;
}