/
PlayEngine.cpp
1716 lines (1488 loc) · 45.5 KB
/
PlayEngine.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
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//----------------------------------------------------------------------------------------
//
// This file and all other Easy Bridge source files are copyright (C) 2002 by Steven Han.
// Use of this file is governed by the GNU General Public License.
// See the files COPYING and COPYRIGHT for details.
//
//----------------------------------------------------------------------------------------
//===========================================================
//
// CPlayEngine
//
// - encapsulates the play engine and its status variables
//
//=========================================================
#include "stdafx.h"
#include "EasyB.h"
#include "EasyBdoc.h"
#include "HandHoldings.h"
#include "BidEngine.h"
#include "Player.h"
#include "PlayerStatusDialog.h"
#include "Card.h"
#include "deck.h"
/*
#include "Play.h"
#include "Cash.h"
#include "Type1Finesse.h"
#include "Type2Finesse.h"
#include "Type3Finesse.h"
#include "Ruff.h"
#include "Sluff.h"
#include "Drop.h"
#include "Discard.h"
*/
#include "PlayEngine.h"
//#include "PlayList.h"
//
//==========================================================================
//==========================================================================
//
// Initialization routines
//
//
CPlayEngine::CPlayEngine()
{
}
CPlayEngine::~CPlayEngine()
{
Clear();
}
//
//-----------------------------------------------------------------------
//
// Initialize()
//
// one-time initialization at program start
//
void CPlayEngine::Initialize(CPlayer* pPlayer, CPlayer* pPartner, CPlayer* pLHOpponent, CPlayer* pRHOpponent, CHandHoldings* pHoldings, CCardLocation* pCardLocation, CGuessedHandHoldings** ppGuessedHands, CBidEngine* pBidder, CPlayerStatusDialog* pStatusDlg)
{
//
m_pPlayer = pPlayer;
m_pPartner = pPartner;
m_pLHOpponent = pLHOpponent;
m_pRHOpponent = pRHOpponent;
m_nPosition = pPlayer->GetPosition();
m_nPartnerPosition = m_pPartner->GetPosition();
m_pPartnersHand = &m_pPartner->GetHand();
//
ASSERT(ISPOSITION(m_nPartnerPosition));
strcpy(szLHO, PositionToString(m_pLHOpponent->GetPosition()));
strcpy(szRHO, PositionToString(m_pRHOpponent->GetPosition()));
//
m_pHand = pHoldings;
m_pCardLocation = pCardLocation;
m_ppGuessedHands = ppGuessedHands;
m_pBidder = pBidder;
m_pStatusDlg = pStatusDlg;
}
//
//-----------------------------------------------------------------------
//
// Clear()
//
void CPlayEngine::Clear()
{
// clear analysis variables
for(int i=0;i<4;i++)
m_ppGuessedHands[i]->Clear(TRUE);
//
m_nPrioritySuit = NONE;
m_nPartnersPrioritySuit = NONE;
for(i=0;i<4;i++)
m_nSuitPriorityList[4] = NONE;
}
//
//-----------------------------------------------------------------------
//
// InitNewHand()
//
// called when the hand is dealt (or play is restarted)
//
void CPlayEngine::InitNewHand()
{
// set general status info
CEasyBDoc* pDoc = pDOC;
m_nContract = pDoc->GetContract();
m_nContractModifier = pDoc->GetContractModifier();
m_nTrumpSuit = pDoc->GetContractSuit();
//
m_bVulnerable = pDOC->IsTeamVulnerable(m_pPlayer->GetTeam());
m_bOpponentsVulnerable = pDoc->IsTeamVulnerable(m_pLHOpponent->GetTeam());
if (m_pRHOpponent->IsDeclarer())
m_bLHDefender = TRUE;
else
m_bLHDefender = FALSE;
if (m_pLHOpponent->IsDeclarer())
m_bRHDefender = TRUE;
else
m_bRHDefender = FALSE;
// clear analysis variables
Clear();
}
//
//-----------------------------------------------------------------------
//
// InitSwappedHand()
//
// called when the hand is swapped in midgame
//
void CPlayEngine::InitSwappedHand()
{
// set general status info
CEasyBDoc* pDoc = pDOC;
m_nContract = pDoc->GetContract();
m_nContractModifier = pDoc->GetContractModifier();
m_nTrumpSuit = pDoc->GetContractSuit();
//
m_bVulnerable = pDOC->IsTeamVulnerable(m_pPlayer->GetTeam());
m_bOpponentsVulnerable = pDoc->IsTeamVulnerable(m_pLHOpponent->GetTeam());
if (m_pRHOpponent->IsDeclarer())
m_bLHDefender = TRUE;
else
m_bLHDefender = FALSE;
if (m_pLHOpponent->IsDeclarer())
m_bRHDefender = TRUE;
else
m_bRHDefender = FALSE;
// clear analysis variables
Clear();
}
//
// RestartPlay()
//
void CPlayEngine::RestartPlay()
{
CPlayerStatusDialog& status = *m_pStatusDlg;
status << "3PLAY! Play restarted.\n====================\n";
Clear();
InitNewHand();
}
//
// GetDummyHand()
//
CHandHoldings& CPlayEngine::GetDummyHand()
{
ASSERT(pDOC->IsDummyExposed());
CHandHoldings& dummyHand = pDOC->GetDummyPlayer()->GetHand();
return dummyHand;
}
//
// GetDummySuit()
//
CSuitHoldings& CPlayEngine::GetDummySuit(int nSuit)
{
ASSERT(pDOC->IsDummyExposed());
ASSERT(ISSUIT(nSuit));
CHandHoldings& dummyHand = pDOC->GetDummyPlayer()->GetHand();
return dummyHand.GetSuit(nSuit);
}
//
//==========================================================================
//==========================================================================
//
// Observation routines
//
//
//
// RecordCardPlay()
//
// record that a card has been played
//
void CPlayEngine::RecordCardPlay(int nPos, CCard* pCard)
{
CPlayerStatusDialog& status = *m_pStatusDlg;
/*
if (pDOC->GetNumCardsPlayedInRound() == 1)
{
status << "3==============================================\n";
status << "3Beginning round " & pDOC->GetPlayRound() + 1 & ".\n";
}
*/
if (nPos == m_nPosition)
status << "4PLAY! We" &
((pDOC->GetNumCardsPlayedInRound() == 1)? " lead" : " play") &
" the " & pCard->GetName() & ".\n";
else
status << "4PLAY! " & PositionToString(nPos) &
((pDOC->GetNumCardsPlayedInRound() == 1)? " leads" : " plays") &
" the " & pCard->GetName() & ".\n";
// record the card played by the specified player
CGuessedHandHoldings* pHand = m_ppGuessedHands[nPos];
CGuessedCard* pGuessedCard = new CGuessedCard(pCard, // card
FALSE, // not outstanding
nPos, // location
1.0); // known with certainty
*pHand << pGuessedCard;
pHand->Sort();
// then re-evaluate holdings in light of the new information
AdjustHoldingsCount(pCard);
AdjustCardCountFromPlay(nPos, pCard);
}
//
// RecordCardUndo()
//
// note that a card has been undone
//
void CPlayEngine::RecordCardUndo(int nPos, CCard* pCard)
{
// remove the card from the observed list (i.e., "forget" that it was played)
VERIFY(ISPLAYER(nPos) && (pCard != NULL));
CPlayerStatusDialog& status = *m_pStatusDlg;
status << "4>> Card taken back.\n";
// remove the card from the guessed list
CGuessedHandHoldings* pHand = m_ppGuessedHands[nPos];
CGuessedCard* pGuessedCard = pHand->RemoveByValue(pCard->GetDeckValue());
delete pGuessedCard;
// re-adjust hand values
AdjustHoldingsCount(pCard);
AdjustCardCountFromUndo(nPos, pCard);
}
//
// RecordTrickUndo()
//
// note that the trick has been undone
// (currently not used)
//
void CPlayEngine::RecordTrickUndo()
{
CPlayerStatusDialog& status = *m_pStatusDlg;
status << "4- - - - - - - -\n";
status << "4>> Trick taken back.\n";
}
//
// RecordRoundComplete()
//
// nPos = winner
// pCard = winning card
//
void CPlayEngine::RecordRoundComplete(int nPos, CCard* pCard)
{
CPlayerStatusDialog& status = *m_pStatusDlg;
status << "4PLAY! " & PositionToString(nPos) &
" wins the round.\n--------\n";
//
// AdjustHoldingsCount(pCard);
// RecordCardsPlayed();
}
//
// RecordHandComplete()
//
void CPlayEngine::RecordHandComplete(int nResult)
{
CPlayerStatusDialog& status = *m_pStatusDlg;
int nTeam = pDOC->GetDeclaringTeam();
if (nResult > 0)
{
status << "3RESULT! " & TeamToString(nTeam) & " made " &
nResult & " overtrick" & ((nResult > 1)? "s" : "") & ".\n";
}
else if (nResult < 0)
{
status << "3RESULT! " & TeamToString(nTeam) & " made the contract.\n";
}
else
{
status << "3RESULT! " & TeamToString(nTeam) & " were set " &
-nResult & " trick" & ((nResult < -1)? "s" : "") & ".\n";
}
}
//
// RecordSpecialEvent()
//
void CPlayEngine::RecordSpecialEvent(int nCode, int nParam1, int nParam2, int nParam3)
{
CPlayerStatusDialog& status = *m_pStatusDlg;
int nPlayer = nParam1;
int nTeam = GetPlayerTeam(nPlayer);
int numTricks = nParam2;
//
switch(nCode)
{
case CEasyBDoc::EVENT_CLAIMED:
// status << "3PLAYCL! " & TeamToString(nTeam) & " has claimed the remaining " &
status << "3PLAYCL! " & PositionToString(nPlayer) & " has claimed the remaining " &
numTricks & " tricks.\n";
break;
case CEasyBDoc::EVENT_CONCEDED:
// status << "3PLAYCN! " & TeamToString(nTeam) & " has conceded the remaining " &
status << "3PLAYCN! " & PositionToString(nPlayer) & " has conceded the remaining " &
numTricks & " tricks.\n";
break;
}
}
//
// RecordCardsPlayed()
//
// - records card played from a full round
// (currently not used)
//
void CPlayEngine::RecordCardsPlayed()
{
// record the cards that have been played
for(int i=0;i<4;i++)
{
// record the card played by this player
CCard* pPlayedCard = pDOC->GetCurrentTrickCard(i);
CGuessedHandHoldings* pHand = m_ppGuessedHands[i];
CGuessedCard* pCard = new CGuessedCard(pPlayedCard, // card
FALSE, // not outstanding
i, // location
1.0); // known with certainty
*pHand << pCard;
pHand->Sort();
}
}
//
// AdjustCardCountFromPlay()
//
// adjust card count and analysis after a card is played
//
void CPlayEngine::AdjustCardCountFromPlay(int nPos, CCard* pCard)
{
// default code
// if (nPos != m_pPlayer->GetPosition())
// {
// note the card that was played
CGuessedHandHoldings* pPlayerHoldings = m_ppGuessedHands[nPos];
CGuessedCard* pGuessedCard = new CGuessedCard(pCard, // card
FALSE, // no longer outstanding
nPos, // location
1.0); // known with certainty
*pPlayerHoldings << pGuessedCard;
// see if the player showed out
CPlayerStatusDialog& status = *m_pStatusDlg;
CCard* pCardLed = pDOC->GetCurrentTrickCardByOrder(0);
if (pCard)
{
int nSuitLed = pCardLed->GetSuit();
ASSERT(nSuitLed != NONE);
CGuessedSuitHoldings& suit = pPlayerHoldings->GetSuit(nSuitLed);
if ((pCard->GetSuit() != nSuitLed) && (!suit.IsSuitShownOut()))
{
status << "4RCP10! " & PositionToString(nPos) &
" shows out of " & STS(nSuitLed) & ".\n";
suit.MarkSuitShownOut();
if (pPlayerHoldings->GetNumSuitsFullyIdentified() > 1)
{
// multiple suits identified
status << "4RCP1A! " & PositionToString(nPos) &
" is now known to have started with ";
int numTotalIdentifiedSuits = pPlayerHoldings->GetNumSuitsFullyIdentified();
int numIdentifiedSuits = 0;
int numTotalIdentifiedCards = 0;
int numTotalOriginalCards = 0;
int nIdentifiedSuits[4];
for(int i=0;i<4;i++)
{
if (pPlayerHoldings->GetSuit(i).AreAllCardsIdentified())
{
CGuessedSuitHoldings& currSuit = pPlayerHoldings->GetSuit(i);
status < ((numIdentifiedSuits > 0)? " and " : " ") &
currSuit.GetNumOriginalCards() & " " &
((suit.GetNumOriginalCards() > 1)? STS(i) : STSS(i));
nIdentifiedSuits[numIdentifiedSuits] = i;
numIdentifiedSuits++;
numTotalIdentifiedCards += currSuit.GetNumDefiniteCards();
numTotalOriginalCards += currSuit.GetNumOriginalCards();
}
}
status < ".\n";
// if the number of cards is known in 3 suits, the orignal
// and current length of the 4th suit is also known
if (numTotalIdentifiedSuits == 3)
{
// first identify the fourth suit
int nFourthSuit = NONE;
for(int i=0;i<4;i++)
{
// test each suit to see if it's in the list of know suits
for(int j=0;j<3;j++)
{
if (nIdentifiedSuits[j] == i)
break;
}
if (j == 3)
nFourthSuit = i;
}
//
VERIFY(nFourthSuit != NONE);
CGuessedSuitHoldings& fourthSuit = pPlayerHoldings->GetSuit(nFourthSuit);
int numOriginalCards = 13 - numTotalOriginalCards;
VERIFY(numOriginalCards >= 0);
fourthSuit.SetNumOriginalCards(numOriginalCards);
int numRemainingCards = numOriginalCards - fourthSuit.GetNumCardsPlayed();
VERIFY(numRemainingCards >= 0);
// mark the # of remaining cards
// revisit this later
// fourthSuit.SetNumLikelyCards(numRemainingCards);
fourthSuit.SetNumRemainingCards(numRemainingCards);
//
status << "4RCP1B! Therefore, " & PositionToString(nPos) &
" started with " & numOriginalCards & " " & STS(nFourthSuit) &
" and has " & numRemainingCards & " left.\n";
}
}
else
{
status < "RCP5A! " & PositionToString(nPos) &
" is now known to have started with " &
suit.GetNumOriginalCards() & " " &
((suit.GetNumOriginalCards() == 1)? STSS(nSuitLed) : STS(nSuitLed)) & ".\n";
}
}
// also, if this is the dummy, and the dummy plays his last card
// in the suit, he has effectively shown out
CPlayer* pDummy = pDOC->GetDummyPlayer();
if ((nPos == pDummy->GetPosition()) && (pDummy->AreCardsExposed()))
{
if ((pCard->GetSuit() == nSuitLed) &&
(pDummy->GetHand().GetNumCardsInSuit(nSuitLed) == 0) &&
(!pPlayerHoldings->IsSuitShownOut(nSuitLed)))
{
status << "3RCP20! Dummy is now out of " & STS(nSuitLed) & ".\n";
pPlayerHoldings->MarkSuitShownOut(nSuitLed);
}
}
}
// }
// special code -- if dummy has just been laid down, mark a suit as shown
// out if dummy is void in the suit
CPlayer* pDummy = pDOC->GetDummyPlayer();
int nRound = pDOC->GetPlayRound();
if ((nRound == 0) && (nPos == pDummy->GetPosition()) && (pDummy->AreCardsExposed()))
{
CHandHoldings& dummy = pDummy->GetHand();
for(int i=0;i<4;i++)
{
if (dummy.GetNumCardsInSuit(i) == 0)
{
status << "3RCP25! Dummy is shown to be void in " & STS(i) & ".\n";
pPlayerHoldings->MarkSuitShownOut(i);
}
}
}
}
//
// AdjustCardCountFromUndo()
//
// adjust card count and analysis after a card play is undone
//
void CPlayEngine::AdjustCardCountFromUndo(int nPos, CCard* pCard)
{
// default code
// if (nPos != m_pPlayer->GetPosition())
{
// find the card in the guessed cards list and delete it
m_ppGuessedHands[nPos]->Delete(pCard);
// later put in code to "forget" analysis based on the last card
}
}
//
// AdjustHoldingsCount()
//
// called to adjust analysis of holdings after a round of play
//
void CPlayEngine::AdjustHoldingsCount(CCard* pCard)
{
// adjust our own holdings
m_pHand->ReevaluateHoldings(pCard);
}
//
//================================================================
//
// Play routines
//
//================================================================
//
//
// PlayCard()
//
// default implementation
//
CCard* CPlayEngine::PlayCard()
{
int nOrdinal = pDOC->GetNumCardsPlayedInRound();
CCard* pCard = NULL;
switch(nOrdinal)
{
case 0:
pCard = PlayFirst();
break;
case 1:
pCard = PlaySecond();
break;
case 2:
pCard = PlayThird();
break;
case 3:
pCard = PlayFourth();
break;
default:
pCard = NULL;
}
// do some sanity checks
ASSERT(pCard->IsValid());
if (!m_pPlayer->IsDeclarer())
{
// playing as defender (or dummy, potentially)
ASSERT(m_pHand->HasCard(pCard));
}
else
{
// declarer -- may be playing for dummy or self
if (pDOC->GetCurrentPlayer() == m_pPartner)
ASSERT(m_pPartnersHand->HasCard(pCard));
else
ASSERT(m_pHand->HasCard(pCard));
}
//
return pCard;
}
//
// PlayFirst()
//
// default implementation
//
CCard* CPlayEngine::PlayFirst()
{
CPlayerStatusDialog& status = *m_pStatusDlg;
status << "5PLAY1! Leading card, using default player logic.\n";
// lead a card
CCard* pLeadCard = GetLeadCard();
ASSERT(m_pHand->HasCard(pLeadCard));
return pLeadCard;
}
//
// PlaySecond()
//
// default implementation, generally should be overridden in derived classes
//
CCard* CPlayEngine::PlaySecond()
{
CPlayerStatusDialog& status = *m_pStatusDlg;
status << "5PLAY2! Playing second, using default player logic.\n";
// get play info
int nDummyPos = pDOC->GetDummyPosition();
CCard* pCardLed = pDOC->GetCurrentTrickCardLed();
int nSuitLed = pCardLed->GetSuit();
int nFaceValue = pCardLed->GetFaceValue();
CCard* pCurrTopCard = pDOC->GetCurrentTrickHighCard();
int nTrumpSuit = pDOC->GetTrumpSuit();
CSuitHoldings& suit = m_pHand->GetSuit(nSuitLed);
// card to play
CCard* pCard = NULL;
// second hand low
int numCardsInSuit = suit.GetNumCards();
if (numCardsInSuit > 0)
{
// default behavior -- just play the low card
pCard = m_pHand->GetSuit(nSuitLed).GetBottomCard();
if (numCardsInSuit > 1)
{
if (*pCard < *pCurrTopCard)
status << "PLY2C1! Play second hand low with the " & pCard->GetFaceName() & ".\n";
else
status << "PLY2C2! As second hand, we play the lowest card we have in the suit, the " & pCard->GetFaceName() & ".\n";
}
else
{
status << "PLY2C4! Play our only " & STSS(nSuitLed) & ", the " & pCard->GetFaceName() & ".\n";
}
}
else
{
// no cards in the suit led
// trump here if possible
if (m_pHand->GetNumTrumps() > 0)
{
//
CSuitHoldings& trumpSuit = m_pHand->GetSuit(nTrumpSuit);
// see if partner would win the suit otherwise
CGuessedSuitHoldings& partnerSuit = m_pPlayer->GetGuessedHand(m_nPartnerPosition)->GetSuit(nSuitLed);
CCardList outstandingCards;
GetOutstandingCards(nSuitLed, outstandingCards);
if (partnerSuit.AreAllCardsIdentified() && partnerSuit.HasCard(outstandingCards[0]->GetFaceValue()))
{
// partner may win the trick, so discard
status << "PLY2K1! We could trump here, but we know partner holds the " & outstandingCards[0]->GetName() &
" and can win the trick, so discard the " & pCard->GetName() & ".\n";
}
else
{
// trump here if possible
// but first see if we're playing ahead of dummy (dummy is to our left),
// and dummy is also void in the suit
CGuessedHandHoldings* pDummyHand = m_pPlayer->GetGuessedHand(nDummyPos);
if ((m_pLHOpponent == pDOC->GetDummyPlayer()) &&
(pDummyHand->GetSuit(nSuitLed).GetNumRemainingCards() == 0) &&
(pDummyHand->GetSuit(nTrumpSuit).GetNumRemainingCards() > 0))
{
// dummy is also void, so trump if we have a trump higher than dummy's
CGuessedCard* pDummyTopTrump = pDummyHand->GetSuit(nTrumpSuit).GetAt(0);
if (trumpSuit.GetNumCardsAbove(pDummyTopTrump->GetFaceValue()) > 0)
{
// go ahead and ruff
pCard = trumpSuit.GetLowestCardAbove(pDummyTopTrump->GetFaceValue());
status << "PLY2M1! Trump here, making sure to thwart dummy's " & pDummyTopTrump->GetFaceName() &
" of trumps by playing the " & pCard->GetFaceName() & ".\n";
}
else
{
// dummy would overtrump
pCard = GetDiscard();
status << "PLY2M2! We'd like to trump here, but Dummy may overruff, so discard the " & pCard->GetName() & ".\n";
}
}
else
{
// safe to trump -- play the lowest trump
pCard = trumpSuit.GetBottomCard();
status << "PLY2P1! Trump with the " & pCard->GetFaceName() & ".\n";
}
}
}
else
{
// discard
pCard = GetDiscard();
status << "PLY2Y! We have no " & SuitToString(nSuitLed) & ", so discard the " & pCard->GetName() & ".\n";
}
}
//
ASSERT(pCard->IsValid());
return pCard;
}
//
// PlayThird()
//
// default implementation
//
CCard* CPlayEngine::PlayThird()
{
CPlayerStatusDialog& status = *m_pStatusDlg;
status << "5PLAY3! Playing third, using default player logic.\n";
// use common code
CCard* pCard = PlayBestCard(3);
//
ASSERT(pCard->IsValid());
return pCard;
}
//
// PlayFourth()
//
// default implementation
//
CCard* CPlayEngine::PlayFourth()
{
CPlayerStatusDialog& status = *m_pStatusDlg;
status << "5PLAY4! Playing fourth, using default player logic.\n";
// use common code
CCard* pCard = PlayBestCard(4);
//
ASSERT(pCard->IsValid());
return pCard;
}
//
// PlayBestCard()
//
// called on the third and fourth hand plays to try to win the trick
//
CCard* CPlayEngine::PlayBestCard(int nPosition)
{
CPlayerStatusDialog& status = *m_pStatusDlg;
// status << "2PLAY3! Playing best card.\n";
// get play info
CCard* pCurrentCard = pDOC->GetCurrentTrickCardLed();
int nSuitLed = pCurrentCard->GetSuit();
int nTopPos;
CCard* pCurrTopCard = pDOC->GetCurrentTrickHighCard(&nTopPos);
CString strTopCardPos = PositionToString(nTopPos);
BOOL bPartnerHigh = FALSE;
int nCurrentRound = pDOC->GetPlayRound();
int nCurrentSeat = pDOC->GetNumCardsPlayedInRound() + 1;
CCard* pPartnersCard = pDOC->GetCurrentTrickCard(m_pPartner->GetPosition());
if (pPartnersCard == pCurrTopCard)
bPartnerHigh = TRUE;
//
int nTrumpSuit = pDOC->GetTrumpSuit();
int numCardsInSuitLed = m_pHand->GetNumCardsInSuit(nSuitLed);
// card to play
CCard* pCard = NULL;
//
// first see if somebody trumped in this hand
//
if ((pDOC->WasTrumpPlayed()) && (nTrumpSuit != nSuitLed))
{
// a trump has been played
// see whether it was played by partner or by an opponent
if (bPartnerHigh)
{
// partner trumped -- leave it alone for now
pCard = GetDiscard();
status << "PLAYB10! We let partner's " & pCurrTopCard->GetName() & " trump ride and discard the " &
pCard->GetName() & ".\n";
}
else
{
// it was an opponent that did the trumping
// see if we can overtrump
CSuitHoldings& trumpSuit = m_pHand->GetSuit(nTrumpSuit);
CCard* pTopTrump = NULL;
if (trumpSuit.GetNumCards() > 0)
pTopTrump = trumpSuit.GetTopCard();
if ((numCardsInSuitLed == 0) && (pTopTrump) && (*pTopTrump > *pCurrTopCard))
{
// get the lowest trump that wil top the current top trump
int numTopCards = trumpSuit.GetNumCardsAbove(pCurrTopCard);
pCard = trumpSuit[numTopCards-1];
status << "PLAYB20! We can overtrump " & strTopCardPos & "'s " & pCurrTopCard->GetName() &
" with the " & pCard->GetFaceName() & ".\n";
}
else
{
// no chance to win, so discard
pCard = GetDiscard();
if ((numCardsInSuitLed == 0) && (trumpSuit.GetNumCards() > 0))
status << "PLAYB22! We can't overtrump " & strTopCardPos & "'s " &
pCurrTopCard->GetFaceName() & ", so discard the " & pCard->GetName() & ".\n";
else
status << "PLAYB23! We can't beat the opponent's " & pCurrTopCard->GetFaceName() &
" of trumps, so discard the " & pCard->GetName() & ".\n";
}
}
}
else
{
// else nobody has played a trump this round, _or_ a trump was led
// see if we can play trumps
if (numCardsInSuitLed > 0)
{
// nope, gotta follow the suit that was led, trumps or otherwise
// if we can beat the current top card, do so with the cheapest card
CSuitHoldings& suit = m_pHand->GetSuit(nSuitLed);
if (*(suit.GetTopCard()) > *pCurrTopCard)
{
// but see if the top card is partner's
if (bPartnerHigh)
{
// see if we should unblock here
if (ISSUIT(nTrumpSuit) && (nCurrentRound == 0) &&
(suit.GetNumHonors() == 1))
{
// first round in an NT contract, with one honor
// in the suit -- unblock
pCard = suit.GetTopCard();
if (suit.GetNumCards() > 1)
status << "PLAYB30! Drop the " & pCard->GetFaceName() &
" here to unblock the suit for partner.\n";
}
else
{
// else this is not an unblocking situation
if (nCurrentSeat == 4)
{
// playing in 4th seat, high card is partner, so discard
pCard = GetDiscard();
status << "PLAYB34! Partner's " & pCurrTopCard->GetFaceName() &
" is high, so discard the " & pCard->GetName() & ".\n";
}
else
{
// playing in third position -- decide whether to
// let partner's card ride
// do so if if partner's card beats all outstanding cards
CCard* pTopOutstandingCard = GetHighestOutstandingCard(nSuitLed);
if ((pTopOutstandingCard == NULL) || (*pCurrTopCard > *pTopOutstandingCard))
{
// let partner's card ride
pCard = GetDiscard();
status << "PLAYB36! Partner's " & pCurrTopCard->GetFaceName() &
" is higher than any outstanding card, so discard the " &
pCard->GetName() & ".\n";
}
else
{
// partner's card is not necessarily highest, so top it
pCard = suit.GetTopSequence().GetBottomCard();
status << "PLAYB37! Partner's " & pCurrTopCard->GetFaceName() &
" might not win the round, so top it with the " & pCard->GetFaceName() & ".\n";
}
}
}
}
else
{
// else high card is opponent's, so beat it w/ highest card affordable
// although, if playing in 3rd pos ahead of dummy, just play
// high enuff to beat dummy
if ((nCurrentSeat == 3) && (m_bLHDefender))
{
CSuitHoldings& dummySuit = GetDummySuit(nSuitLed);
int nDummyTopCard = 0;
if (dummySuit.GetNumCards() > 0)
nDummyTopCard = dummySuit[0]->GetFaceValue();
int nTopVal = Max(nDummyTopCard, pCurrTopCard->GetFaceValue());
pCard = suit.GetLowestCardAbove(nTopVal);
// see if we can beat the top card or dummy's top card
if (pCard)
{
if (nTopVal == nDummyTopCard)
{
// dummy has the top card and we can beat it
status << "PLAYB38A! Playing third ahead of dummy, need to beat dummy's " &
CardValToString(nDummyTopCard) & ".\n";
}
else
{
// the top card is declarer's
status << "PLAYB38B! Play high to win with the " & pCard->GetFaceName() & ".\n";
}
}
else
{
// else we can't beat dummy's top card, but play
// high anyway to force out his winner
pCard = suit.GetLowestCardAbove(pCurrTopCard);
status << "PLAYB38C! We top declarer's " & pCurrTopCard->GetFaceName() &
" to force a winner from dummy.\n";
}
}
else if (nCurrentSeat == 3)
{
// else we're playing 3rd, so play the lowest card from the top sequence
pCard = suit.GetTopSequence().GetBottomCard();
status << "PLAYB40! Play high to win with the " & pCard->GetFaceName() & ".\n";
}
else
{
// else we're playing last (4th)
// play the cheapest card that will beat the top card
pCard = suit.GetLowestCardAbove(pCurrTopCard);
status << "PLAYB41! Play the " & pCard->GetFaceName() & " to win the trick.\n";
}
}
}
else
{
// we don't have a card to top the current high card
if (bPartnerHigh)
{
// but partner's card is high, so we're OK
pCard = GetDiscard();
status << "PLAYB47! Partner's " & pCurrTopCard->GetFaceName() &
" can win the trick, so discard the " & pCard->GetName() & ".\n";
}
else
{
// else we're screwed
pCard = GetDiscard();
status << "PLAYB48! We can't beat " & strTopCardPos & "'s " & pCurrTopCard->GetFaceName() &
", so discard the " & pCard->GetName() & ".\n";
}
}
}
else if (ISSUIT(nTrumpSuit) && (nSuitLed != nTrumpSuit) &&
(m_pHand->GetNumCardsInSuit(nTrumpSuit) > 0))
{
// here, we can play a trump, so do so if appropriate