-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain2.cpp
More file actions
624 lines (458 loc) · 16.8 KB
/
main2.cpp
File metadata and controls
624 lines (458 loc) · 16.8 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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/***************************************************************************/
/** Microsoft Windows **/
/** Copyright(c) Microsoft Corp., 1991, 1992 **/
/***************************************************************************/
/****************************************************************************
main2.cpp -- wxWidgets port
Aug 92, JimH
May 93, JimH chico port
Additional member functions for CMainWindow are here.
****************************************************************************/
#include "hearts.h"
#include "main.h"
#include "resource.h"
#include "debug.h"
#include <cstdlib>
#include <wx/sound.h>
#include <wx/filename.h>
#include <wx/stdpaths.h>
int score[MAXPLAYER];
/* nHandsPlayed is defined in dlg.cpp; declared in dlg.h via extern "C" */
/****************************************************************************
CMainWindow::Shuffle -- distributes cards to players
****************************************************************************/
void CMainWindow::Shuffle()
{
static int offset[MAXPLAYER] = { 1, 3, 2, 0 }; // passdir order
// fill temp array with consecutive values
int temp[52]; // array of card values
for (int i = 0; i < 52; i++)
temp[i] = i;
// Sort cards
int nLeft = 52;
for (int i = 0; i < 52; i++)
{
int j = rand() % nLeft;
int id = i / 13;
int pos = Id2Pos(id); // convert id to position
p[pos]->SetID(i % 13, temp[j]);
p[pos]->Select(i % 13, false);
temp[j] = temp[--nLeft];
}
// display PASS button
if (passdir != NOPASS)
{
wxString text = GetStringResource(IDS_PASSLEFT + passdir);
PassBtnSetLabel(text);
PassBtnEnable(false);
PassBtnShow();
}
// set card locs and ask players to select cards to pass
for (int i = 0; i < MAXPLAYER; i++)
{
p[i]->ResetLoc();
if (passdir != NOPASS)
p[i]->SelectCardsToPass();
}
// Paint main window. This is done manually instead of just
// invalidating the rectangle so that the cards are drawn in
// order as if they are dealt, instead of a player at a time.
//
// Draw directly into the logical backbuffer (not a wxClientDC on
// the window). The window is scaled; wxClientDC writes at
// unscaled logical coords and ends up in the wrong place when
// the user has already resized -- and more importantly, those
// pixels are outside the backbuffer, so the very next resize (or
// any OnPaint) blits an empty green backbuffer over the top and
// the cards disappear. This was the "window goes dark on resize
// during pass-selection" bug: Shuffle painted cards onto the
// window via wxClientDC, but the backbuffer never saw them.
wxMemoryDC dc;
dc.SelectObject(m_backbuffer);
dc.SetBrush(m_BgndBrush);
dc.SetPen(*wxTRANSPARENT_PEN);
dc.DrawRectangle(0, 0, WINWIDTH, WINHEIGHT);
for (SLOT s = 0; s < MAXSLOT; s++)
for (int i = 0; i < MAXPLAYER; i++)
p[i]->Draw(dc, bCheating, s);
for (int i = 0; i < MAXPLAYER; i++)
{
if (passdir == NOPASS)
p[i]->NotifyNewRound();
else
{
p[i]->MarkSelectedCards(dc);
wxString sSelect = GetStringResource(IDS_SELECT);
wxString sName;
wxString sSpace = wxT(" ");
wxString sDot = wxT(".");
wxString sString;
int passto = (i + offset[passdir]) % 4;
sName = p[passto]->GetName();
sString = sSelect + sSpace + sName + sDot;
p[i]->UpdateStatus(sString);
}
}
dc.SelectObject(wxNullBitmap);
DoSort();
// Invalidate the whole window so OnPaint blits the freshly-drawn
// backbuffer to the (possibly scaled) client area.
Refresh(false);
Update();
}
/****************************************************************************
CMainWindow::HandlePassing()
This function first checks to make sure each player is DONE_SELECTING,
and then transfers the cards from hand to hand.
This function is called by the gamemeister when he presses the pass
button, or when notification arrives that a remote human has selected
cards to pass.
It returns false if cards were not passed (because a remote human was
still selecting) and true if cards were successfully passed.
****************************************************************************/
bool CMainWindow::HandlePassing()
{
int passto[MAXPLAYER];
int temp[MAXPLAYER][3];
static int offset[MAXPLAYER] = { 1, 3, 2, 0 };
for (int pos = 0; pos < MAXPLAYER; pos++)
if (p[pos]->GetMode() != DONE_SELECTING)
return false;
for (int pos = 0; pos < MAXPLAYER; pos++)
{
passto[pos] = ((pos + offset[passdir]) % 4);
p[pos]->ReturnSelectedCards(temp[pos]);
}
for (int pos = 0; pos < MAXPLAYER; pos++)
p[passto[pos]]->ReceiveSelectedCards(temp[pos]);
for (int pos = 0; pos < MAXPLAYER; pos++)
if (bCheating || (pos == 0))
p[pos]->Sort();
tricksleft = MAXSLOT;
passdir++;
if (passdir > NOPASS)
passdir = LEFT_DIR;
for (int pos = 0; pos < MAXPLAYER; pos++)
p[pos]->NotifyNewRound(); // notify players cards are passed
wxString s = GetStringResource(IDS_OK);
PassBtnSetLabel(s);
PassBtnEnable(true);
for (int pos = 0; pos < MAXPLAYER; pos++)
{
wxRect rect;
if (pos == 0 || bCheating)
p[pos]->GetCoverRect(rect);
else
p[pos]->GetMarkingRect(rect);
RefreshLogicalRect(rect, true);
}
Update();
return true;
}
/****************************************************************************
CMainWindow::FirstMove
resets cardswon[] and tells owner of two of clubs to start hand
****************************************************************************/
void CMainWindow::FirstMove()
{
for (int pos = 0; pos < MAXPLAYER; pos++)
{
p[pos]->SetMode(WAITING);
p[pos]->ResetCardsWon();
}
for (int pos = 0; pos < MAXPLAYER; pos++)
{
for (SLOT s = 0; s < MAXSLOT; s++)
{
if (p[pos]->GetID(s) == TWOCLUBS)
{
int id = Pos2Id(pos);
ResetHandInfo(id);
handinfo.bHeartsBroken = false;
handinfo.bQSPlayed = false;
handinfo.bShootingRisk = true;
handinfo.nMoonShooter = EMPTY;
handinfo.bHumanShooter = false;
p[pos]->SelectCardToPlay(handinfo, bCheating);
if (pos != 0)
((local_human *)p[0])->WaitMessage(p[pos]->GetName());
return;
}
}
}
}
/****************************************************************************
CMainWindow::EndHand
The Ref calls this routine at the end of each hand. It is logically
a single routine, but is broken up so that there is a delay before the
cards are zipped off the screen.
EndHand() calculates who won the hand (trick) and starts a timer.
****************************************************************************/
void CMainWindow::EndHand()
{
/* determine suit led */
int playerled = handinfo.playerled;
card *cardled = handinfo.cardplayed[playerled];
int suitled = cardled->Suit();
int value = cardled->Value2();
trickwinner = playerled; // by default
// Let players update tables, etc.
for (int i = 0; i < 4; i++)
p[i]->NotifyEndHand(handinfo);
// check if anyone else played a higher card of the same suit
for (int i = playerled; i < (playerled + 4); i++)
{
int j = i % 4;
card *c = handinfo.cardplayed[j];
if (c->Suit() == suitled)
{
int v = c->Value2();
if (v > value)
{
value = v;
trickwinner = j;
}
}
}
TRACE0("\n");
// Update moonshoot portion of handinfo
if (handinfo.bShootingRisk)
{
bool bPoints = false; // point cards this hand?
for (int i = 0; i < 4; i++)
{
card *c = handinfo.cardplayed[i];
if ((c->Suit() == HEARTS) || (c->ID() == BLACKLADY))
bPoints = true;
}
if (bPoints)
{
if (handinfo.nMoonShooter == EMPTY)
{
handinfo.nMoonShooter = trickwinner; // first points this round
handinfo.bHumanShooter = p[trickwinner]->IsHuman();
}
else if (handinfo.nMoonShooter != trickwinner) // new point earner
{
handinfo.bShootingRisk = false;
}
}
}
// Start a timer so there is a delay between when the last card of
// the trick is played, and when the cards are whisked off toward
// the trick winner (dispatched.) If the timer fails, just call
// DispatchCards() directly.
if (m_dispatchTimer.StartOnce(1000))
bTimerOn = true;
else
{
bTimerOn = false;
DispatchCards();
}
}
/****************************************************************************
CMainWindow::DispatchCards
Called by the dispatch timer after the trick display delay. Glides
cards towards the trick winner, then checks if there are more tricks
left or if the hand is over and scoring is needed.
****************************************************************************/
void CMainWindow::DispatchCards()
{
m_dispatchTimer.Stop();
bTimerOn = false;
int poswinner = Id2Pos(trickwinner);
// Determine who led so cards can be removed in reverse order.
int playerled = handinfo.playerled;
card *cardled = handinfo.cardplayed[playerled];
// build up background bitmap for Glide()
for (int i = (playerled + 3); i >= playerled; i--)
{
wxClientDC dc(this);
wxBitmap bgBmp(card::dxCrd, card::dyCrd);
{
wxMemoryDC memdc;
memdc.SelectObject(bgBmp);
memdc.SetBrush(m_BgndBrush);
memdc.SetPen(*wxTRANSPARENT_PEN);
memdc.DrawRectangle(0, 0, card::dxCrd, card::dyCrd);
card *c = handinfo.cardplayed[i % 4];
// If cards overlap, there is some extra work to do because the cards
// still in player 0's or 2's hands may overlap cards that have been
// played, so they have to get blted in first.
for (int pos = 0; pos < MAXPLAYER; pos += 2)
{
int mode = ((pos == 0 || bCheating) ? FACEUP : FACEDOWN);
for (SLOT s = 0; s < MAXSLOT; s++)
{
card *c2 = p[pos]->Card(s);
int x = c2->GetX() - c->GetX();
int y = c2->GetY() - c->GetY();
if (!c2->IsPlayed())
c2->Draw(memdc, x, y, mode, false);
}
}
// Everyone needs to check for overlap of played cards.
for (int j = playerled; j < i; j++)
{
card *c2 = handinfo.cardplayed[j % 4];
int x = c2->GetX() - c->GetX();
int y = c2->GetY() - c->GetY();
c2->Draw(memdc, x, y, FACEUP, false);
}
memdc.SelectObject(wxNullBitmap);
}
card::m_bmBgnd = bgBmp;
card *c = handinfo.cardplayed[i % 4];
p[poswinner]->WinCard(dc, c);
c->Remove();
}
ResetHandInfo(trickwinner);
// If there are more tricks left before we need to reshuffle,
// ask the winner of this trick to start next hand, and we're done.
if (--tricksleft)
{
p[poswinner]->SelectCardToPlay(handinfo, bCheating);
if (poswinner != 0)
((local_human *)p[0])->WaitMessage(p[poswinner]->GetName());
if (::cQdMoves > 0)
{
for (int i = 0; i < ::cQdMoves; i++)
HandleMove(::moveq[i]);
::cQdMoves = 0;
}
return;
}
// Make sure sound buffer is freed up.
HeartsPlaySound(OFF);
// Display hearts (and queen of spades) next to whoever "won" them.
int nMoonShot = EMPTY; // assume nobody shot moon
for (int i = 0; i < MAXPLAYER; i++)
{
bool bMoonShot;
score[i] = p[i]->EvaluateScore(bMoonShot);
if (bMoonShot)
nMoonShot = i; // scores need to be adjusted
wxClientDC dc(this);
p[i]->DisplayHeartsWon(dc);
p[i]->SetMode(SCORING);
}
// adjust scores if someone collected all hearts AND queen of spades
if (nMoonShot != EMPTY)
{
for (int i = 0; i < MAXPLAYER; i++)
{
if (i == nMoonShot)
score[i] -= 26;
else
score[i] += 26;
p[i]->SetScore(score[i]); // adjust player score manually
}
}
// Show score
p[0]->UpdateStatus(IDS_SCORE);
p[0]->SetMode(SCORING);
CScoreDlg scoredlg(this, score, m_myid); // update scores in scoredlg
player *pold = p[0];
scoredlg.ShowModal(); // display scores
// If there has been a request to shut down while the score dialog
// is displayed, m_FatalErrno will be non-zero.
if (m_FatalErrno != 0)
{
p[0]->SetMode(PLAYING); // something other than SCORING...
FatalError(m_FatalErrno); // so FatalError will accept it.
return;
}
// It's possible for another player to have quit the game while
// the score dialog was showing, so check that we're still
// alive and well.
if (p[0] != pold)
return;
// replace quit remote humans with computer players
for (int i = 1; i < MAXPLAYER; i++)
{
if (p[i]->HasQuit())
{
wxString name = p[i]->GetName();
int scoreLocal = p[i]->GetScore();
delete p[i];
p[i] = new computer(i); // check for failure
wxClientDC dc(this);
p[i]->SetName(name, dc);
p[i]->SetScore(scoreLocal);
}
}
p[0]->SetMode(passdir == NOPASS ? DONE_SELECTING : SELECTING);
if (scoredlg.IsGameOver())
{
GameOver();
return;
}
Shuffle();
// If there is no passing for upcoming round, we must make the changes
// that HandlePassing() would normally do to start the next round.
if (passdir == NOPASS)
{
for (int i = 0; i < MAXPLAYER; i++) // everyone's DONE_SELECTING
p[i]->SetMode(DONE_SELECTING);
passdir = LEFT_DIR; // NEXT hand passes left
tricksleft = MAXSLOT; // reset # of hands
FirstMove(); // start next trick
}
for (int i = 0; i < ::cQdMoves; i++)
HandleMove(::moveq[i]);
::cQdMoves = 0;
for (int i = 0; i < ::cQdPasses; i++)
HandlePass(::passq[i]);
::cQdPasses = 0;
}
/****************************************************************************
CMainWindow::ResetHandInfo
Note that handinfo.bHeartsBroken is not reset here -- it applies to
the entire hand and is set only in FirstMove()
Same with handinfo.bQSPlayed and moonshoot variables.
****************************************************************************/
void CMainWindow::ResetHandInfo(int playernumber)
{
handinfo.playerled = playernumber;
handinfo.turn = playernumber;
for (int i = 0; i < MAXPLAYER; i++)
handinfo.cardplayed[i] = nullptr;
}
/****************************************************************************
CMainWindow::HandleMove
Process a move from a player (local or queued).
****************************************************************************/
void CMainWindow::HandleMove(MOVE &move)
{
int id = move.playerid;
int pos = Id2Pos(id);
int cardid = move.cardid;
SLOT s = p[pos]->GetSlot(cardid);
p[pos]->MarkCardPlayed(s);
handinfo.cardplayed[id] = p[pos]->Card(s);
handinfo.playerled = move.playerled;
handinfo.turn = move.turn;
OnRef();
}
/****************************************************************************
CMainWindow::HandlePass
Process a pass from a player (queued).
****************************************************************************/
void CMainWindow::HandlePass(PASS3 &pass3)
{
int id = pass3.id;
int pos = Id2Pos(id);
for (int i = 0; i < 3; i++)
{
SLOT s = p[pos]->GetSlot(pass3.cardid[i]);
p[pos]->Select(s, true);
}
p[pos]->SetMode(DONE_SELECTING);
// Check if everyone is ready
bool bReady = true;
for (int i = 0; i < MAXPLAYER; i++)
if (p[i]->GetMode() != DONE_SELECTING)
bReady = false;
if (bReady)
HandlePassing();
}