-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhuman.cpp
More file actions
571 lines (442 loc) · 13.5 KB
/
human.cpp
File metadata and controls
571 lines (442 loc) · 13.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
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/* human.cpp -- wxWidgets port of local_human and remote_human */
#include "hearts.h"
#include "main.h"
#include "resource.h"
#include "debug.h"
#include <cstdio>
#include <cstdlib>
static wxRect rectCard;
extern CMainWindow *pMainWnd;
bool local_human::bTimerOn;
wxString local_human::m_StatusText;
/* human constructor -- abstract class */
human::human(int n, int pos) : player(n, pos)
{
}
/* remote_human constructor */
remote_human::remote_human(int n, int pos) : human(n, pos),
bQuit(false)
{
}
/*
* remote_human::SelectCardToPlay()
*
* Under normal circumstances, all that is required is that mode be set
* to PLAYING. If the remote human has quit and the computer player has
* not yet taken over, this routine just picks the first legal card it
* can find and plays it.
*/
void remote_human::SelectCardToPlay(handinfotype &h, bool bCheating)
{
if (!bQuit)
{
SetMode(PLAYING);
return;
}
bool bFirst = (h.playerled == id); // am I leading?
card *cardled = h.cardplayed[h.playerled];
int nSuitLed = (cardled == NULL ? EMPTY : cardled->Suit());
SLOT sLast[MAXSUIT]; // will contain some card of each suit
SLOT s = EMPTY;
for (int i = 0; i < MAXSUIT; i++)
sLast[i] = EMPTY;
// fill sLast array, and look for two of clubs while we are at it
for (int i = 0; i < MAXSLOT; i++)
{
if (cd[i].IsValid())
{
sLast[cd[i].Suit()] = i;
if (cd[i].ID() == TWOCLUBS)
s = i;
}
}
if (s == EMPTY) // if two of clubs not found
{
if (sLast[CLUBS] != EMPTY)
s = sLast[CLUBS];
else if (sLast[DIAMONDS] != EMPTY)
s = sLast[DIAMONDS];
else if (sLast[SPADES] != EMPTY)
s = sLast[SPADES];
else
s = sLast[HEARTS];
if (!bFirst && (sLast[nSuitLed] != EMPTY))
s = sLast[nSuitLed];
}
SetMode(WAITING);
cd[s].Play(); // mark card as played
h.cardplayed[id] = &(cd[s]); // update handinfo
// inform other players (local-only mode, no DDE)
::move.playerid = id;
::move.cardid = cd[s].ID();
::move.playerled = h.playerled;
::move.turn = h.turn;
// In the MFC version, ddeServer->PostAdvise(hszMove) was called here.
// In the wxWidgets local-only port, we skip the DDE notification.
// inform gamemeister
wxCommandEvent evt(wxEVT_MENU, IDM_REF);
pMainWnd->GetEventHandler()->QueueEvent(evt.Clone());
}
/*
* remote_human::SelectCardsToPass()
*
* Under normal circumstances, all that is required is that mode be set to
* SELECTING. If the remote human has quit and the computer player has not
* yet taken over, just select the first three cards found.
*/
void remote_human::SelectCardsToPass()
{
if (!bQuit)
{
SetMode(SELECTING);
return;
}
cd[0].Select(true);
cd[1].Select(true);
cd[2].Select(true);
for (int i = 3; i < MAXSLOT; i++)
cd[i].Select(false);
wxClientDC dc(pMainWnd);
MarkSelectedCards(dc);
SetMode(DONE_SELECTING);
// In the MFC version, ddeServer->PostAdvise(hszPass) was called here.
// In the wxWidgets local-only port, we skip the DDE notification.
}
/*
* local_human::local_human()
*
* This is the constructor that initializes the local human player.
* It also creates the stretch bitmap that covers a card plus its popped
* height extension.
*/
local_human::local_human(int n) : human(n, 0)
{
m_StatusText = GetStringResource(IDS_INTRO);
wxClientDC dc(pMainWnd);
UpdateStatus();
bTimerOn = false;
m_bmStretchCard = wxBitmap(card::dxCrd, card::dyCrd + POPSPACING);
if (!m_bmStretchCard.IsOk())
{
pMainWnd->FatalError(IDS_MEMORY);
return;
}
}
/* local_human destructor */
local_human::~local_human()
{
m_bmStretchCard = wxNullBitmap;
}
/*
* local_human::Draw()
*
* This virtual function draws selected cards in the popped up position.
* ALL is not used for slot in this variant.
*/
void local_human::Draw(wxDC &dc, bool bCheating, SLOT slot)
{
DisplayName(dc);
SLOT start = (slot == ALL ? 0 : slot);
SLOT stop = (slot == ALL ? MAXSLOT : slot+1);
SLOT playedslot = EMPTY; // must draw cards in play last for EGA
for (SLOT s = start; s < stop; s++)
{
if (cd[s].IsPlayed())
playedslot = s;
else
cd[s].PopDraw(dc); // pop up selected cards
}
if (playedslot != EMPTY)
cd[playedslot].Draw(dc);
}
/*
* local_human::PopCard()
*
* handles mouse button selection of card to pass
*/
void local_human::PopCard(wxBrush &brush, int x, int y)
{
SLOT s = XYToCard(x, y);
if (s == EMPTY)
return;
// count selected cards
int c = 0;
for (int i = 0; i < MAXSLOT; i++)
if (cd[i].IsSelected())
c++;
if (cd[s].IsSelected() && (c == 3))
{
wxCommandEvent evt(wxEVT_MENU, IDM_HIDEBUTTON);
pMainWnd->GetEventHandler()->QueueEvent(evt.Clone());
}
else if (!cd[s].IsSelected())
{
if (c == 3) // only allow three selections
return;
else if (c == 2)
{
wxCommandEvent evt(wxEVT_MENU, IDM_SHOWBUTTON);
pMainWnd->GetEventHandler()->QueueEvent(evt.Clone());
}
}
// toggle selection
bool bSelected = cd[s].IsSelected();
cd[s].Select(!bSelected);
(void)brush;
// Update the backbuffer for just the affected hand cover rect so
// the pop-up/pop-down visual happens immediately, and the new state
// persists through subsequent resizes (OnPaint's RenderScene will
// keep rendering it the same way). Writing to a wxClientDC at
// logical coords would miss when the window has been scaled, and
// those pixels wouldn't survive the next OnPaint since the
// backbuffer wouldn't reflect them.
wxRect refreshRect;
GetCoverRect(refreshRect);
if (pMainWnd->m_backbuffer.IsOk())
{
wxMemoryDC bbdc;
bbdc.SelectObject(pMainWnd->m_backbuffer);
bbdc.SetBrush(CMainWindow::m_BgndBrush);
bbdc.SetPen(*wxTRANSPARENT_PEN);
bbdc.DrawRectangle(refreshRect);
Draw(bbdc, false, ALL);
MarkSelectedCards(bbdc);
bbdc.SelectObject(wxNullBitmap);
}
// Invalidate just the cover rect (not the whole window) so OnPaint
// blits only that slice of the backbuffer to the scaled target.
// eraseBackground=false to avoid wxGTK3 painting the area green
// before OnPaint has a chance to fire.
pMainWnd->RefreshLogicalRect(refreshRect, false);
pMainWnd->Update();
}
/*
* local_human::PlayCard()
*
* handles mouse button selection of card to play
* and ensures move is legal.
*
* PlayCard starts a timer that calls StartTimer().
* Think of it as one long function with a timer delay half way through.
*/
bool local_human::PlayCard(int x, int y, handinfotype &h, bool bCheating,
bool bFlash)
{
SLOT s = XYToCard(x, y);
if (s == EMPTY)
return false;
card *cardled = h.cardplayed[h.playerled];
bool bFirstTrick = (cardled != NULL && cardled->ID() == TWOCLUBS);
/* check if selected card is valid */
if (h.playerled == id) // if local human is leading...
{
if (cd[s].ID() != TWOCLUBS)
{
for (int i = 0; i < MAXSLOT; i++) // is there a two of clubs?
{
if ((i != s) && (cd[i].ID() == TWOCLUBS))
{
UpdateStatus(IDS_LEAD2C);
if (bFlash)
StartTimer(cd[s]);
return false;
}
}
}
if ((cd[s].Suit() == HEARTS) && (!h.bHeartsBroken)) // if hearts led
{
for (int i = 0; i < MAXSLOT; i++) // are there any non-hearts?
{
if ((!cd[i].IsEmpty()) && (cd[i].Suit() != HEARTS))
{
UpdateStatus(IDS_LEADHEARTS);
if (bFlash)
StartTimer(cd[s]);
return false;
}
}
}
}
// if not following suit
else if (cardled != NULL && (cd[s].Suit() != cardled->Suit()))
{
// make sure we're following suit if possible
for (int i = 0; i < MAXSLOT; i++)
{
if ((!cd[i].IsEmpty()) && (cd[i].Suit()==cardled->Suit()))
{
wxString s1, s2, sSpace, sDot, string;
s1 = GetStringResource(IDS_BADMOVE);
s2 = GetStringResource(IDS_SUIT0+cardled->Suit());
sSpace = wxT(" ");
sDot = wxT(".");
string = s1 + sSpace + s2 + sDot;
if (bFlash)
{
UpdateStatus(string);
StartTimer(cd[s]);
}
return false;
}
}
// make sure we're not trying to break the First Blood rule
if (bFirstTrick && pMainWnd->IsFirstBloodEnforced())
{
bool bPointCard =
(cd[s].Suit() == HEARTS || cd[s].ID() == BLACKLADY);
bool bOthersAvailable = false;
for (int i = 0; i < MAXSLOT; i++)
if ((!cd[i].IsEmpty()) && (cd[i].Suit() != HEARTS))
if (cd[i].ID() != BLACKLADY)
bOthersAvailable = true;
if (bPointCard && bOthersAvailable)
{
UpdateStatus(IDS_BADBLOOD);
if (bFlash)
StartTimer(cd[s]);
return false;
}
}
}
SetMode(WAITING);
cd[s].Play();
h.cardplayed[id] = &(cd[s]);
::move.playerid = id;
::move.cardid = cd[s].ID();
::move.playerled = h.playerled;
::move.turn = h.turn;
// In the MFC version, DDE was used here to notify other players.
// In the wxWidgets local-only port, we skip DDE calls.
pMainWnd->OnRef();
return true;
}
void local_human::StartTimer(card &c)
{
wxClientDC dc(pMainWnd);
c.Draw(dc, HILITE); // flash
c.GetRect(rectCard);
pMainWnd->StartBadMoveTimer(rectCard);
bTimerOn = true;
}
/*
* local_human::XYToCard()
*
* returns a card slot number (or EMPTY) given a mouse location
*/
int local_human::XYToCard(int x, int y)
{
// check that we are in the right general area on the screen
if (y < (loc.y - POPSPACING))
return EMPTY;
if (y > (loc.y + card::dyCrd))
return EMPTY;
if (x < loc.x)
return EMPTY;
if (x > (loc.x + (12 * HORZSPACING) + card::dxCrd))
return EMPTY;
// Take first stab at card selected.
SLOT s = (x - loc.x) / HORZSPACING;
if (s > 12)
s = 12;
// If the click is ABOVE the top of the normal card location,
// check to see if this is a selected card.
if (y < loc.y)
{
// If the card is selected, then we have it. If not, it could
// be overhanging other cards.
if (!cd[s].IsSelected())
{
for (;;)
{
if (s == 0)
return EMPTY;
s--;
// if this card doesn't extend as far as x, give up
if ((loc.x + (s * HORZSPACING) + card::dxCrd) < x)
return EMPTY;
// if this card is selected, we've got it
if (cd[s].IsSelected())
break;
}
}
}
// a similar check is used to make sure we pick a card not yet played
if (!cd[s].IsInHand())
{
for (;;)
{
if (s == 0)
return EMPTY;
s--;
// if this card doesn't extend as far as x, give up
if ((loc.x + (s * HORZSPACING) + card::dxCrd) < x)
return EMPTY;
// if this card is in hand, we've got it
if (cd[s].IsInHand())
break;
}
}
return s;
}
/* local_human::SelectCardsToPass() */
void local_human::SelectCardsToPass()
{
SetMode(SELECTING);
}
/* local_human::SelectCardToPlay */
void local_human::SelectCardToPlay(handinfotype &h, bool bCheating)
{
SetMode(PLAYING);
UpdateStatus(IDS_GO);
}
/*
* local_human::UpdateStatus
*
* The status bar can be updated either by manually filling m_StatusText
* or by passing a string resource id.
*/
void local_human::UpdateStatus(void)
{
pMainWnd->SetStatusText(m_StatusText);
}
void local_human::UpdateStatus(int stringid)
{
status = stringid;
m_StatusText = GetStringResource(stringid);
UpdateStatus();
}
void local_human::UpdateStatus(const wxString &string)
{
m_StatusText = string;
UpdateStatus();
}
/*
* local_human::ReceiveSelectedCards
*
* The parameter c[] is an array of three cards being passed from another
* player.
*/
void local_human::ReceiveSelectedCards(int c[])
{
for (int i = 0, j = 0; j < 3; i++)
{
if (cd[i].IsSelected())
cd[i].SetID(c[j++]);
wxASSERT(i < MAXSLOT);
}
SetMode(ACCEPTING);
UpdateStatus(IDS_ACCEPT);
}
/*
* local_human::WaitMessage()
*
* Makes and shows the "Waiting for %s to move..." message
*/
void local_human::WaitMessage(const wxString &name)
{
wxString s1 = GetStringResource(IDS_WAIT1);
wxString s2 = name;
wxString s3 = GetStringResource(IDS_WAIT2);
wxString buf = s1 + s2 + s3;
UpdateStatus(buf);
}