-
Notifications
You must be signed in to change notification settings - Fork 1
/
DefenderPlayEngine.cpp
2489 lines (2242 loc) · 78.3 KB
/
DefenderPlayEngine.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.
//
//----------------------------------------------------------------------------------------
/=====================================================================
//
// CDefenderPlayEngine
//
// - encapsulates the defender's play engine and its status variables
//
//=====================================================================
#include "stdafx.h"
#include "EasyB.h"
#include "EasyBdoc.h"
#include "PlayerStatusDialog.h"
#include "Card.h"
#include "HandHoldings.h"
#include "BidEngine.h"
#include "GIB.h"
#include "progopts.h"
#include "DefenderPlayEngine.h"
//
//==========================================================================
//==========================================================================
//
// Initialization routines
//
//
CDefenderPlayEngine::CDefenderPlayEngine()
{
}
CDefenderPlayEngine::~CDefenderPlayEngine()
{
Clear();
}
//
//-----------------------------------------------------------------------
//
// Initialize()
//
// one-time initialization
//
void CDefenderPlayEngine::Initialize(CPlayer* pPlayer, CPlayer* pPartner, CPlayer* pLHOpponent, CPlayer* pRHOpponent, CHandHoldings* pHoldings, CCardLocation* pCardLocation, CGuessedHandHoldings** ppGuessedHands, CBidEngine* pBidder, CPlayerStatusDialog* pStatusDlg)
{
// call base class first
CPlayEngine::Initialize(pPlayer, pPartner, pLHOpponent, pRHOpponent, pHoldings, pCardLocation, ppGuessedHands, pBidder, pStatusDlg);
}
//
//-----------------------------------------------------------------------
//
// Clear()
//
void CDefenderPlayEngine::Clear()
{
// call base class first
CPlayEngine::Clear();
// clear class members
for(int i=0;i<4;i++)
m_nPartnerSuitPreference[i] = 0;
//
m_nSignalledSuit = NONE;
m_nEchoedSuit = NONE;
for(i=0;i<4;i++)
{
m_bSuitEchoed[i] = FALSE;
m_bSuitLed[i] = FALSE;
m_nSuitSignalStatus[i] = 0;
}
}
//
//-----------------------------------------------------------------------
//
// InitNewHand()
//
// called when the hand is dealt (or play is restarted)
//
void CDefenderPlayEngine::InitNewHand()
{
// make sure we're not declarer
VERIFY(!m_pPlayer->IsDeclarer());
// call base class first
CPlayEngine::InitNewHand();
// m_numRequiredTricks = # requried to defeat the contract
m_numRequiredTricks = 13 - (BID_LEVEL(m_nContract) + 6) + 1;
}
//
// RecordCardPlay()
//
// note that a card has been played
//
void CDefenderPlayEngine::RecordCardPlay(int nPos, CCard* pCard)
{
// first call the base class
CPlayEngine::RecordCardPlay(nPos, pCard);
// skip the rest if reviewing a game
if (pDOC->IsReviewingGame())
return;
// then re-evaluate holdings
m_pHand->ReevaluateHoldings(pCard);
// if this suit is being led for the first time, mark it
if (pDOC->GetNumCardsPlayedInRound() == 1)
{
int nSuitLed = pCard->GetSuit();
if (!m_bSuitLed[nSuitLed])
m_bSuitLed[nSuitLed] = TRUE;
}
// see if partner is trying to signal with his discard
CPlayerStatusDialog& status = *m_pStatusDlg;
int nRoundLead = pDOC->GetRoundLead();
CCard* pCardLed = pDOC->GetCurrentTrickCardByOrder(0);
CCard* pTopCard = pDOC->GetCurrentTrickHighCard(0);
int nSuitLed = pCardLed->GetSuit();
int nTrumpSuit = pDOC->GetTrumpSuit();
int nCurrPlayer = pCard->GetOwner();
int nPlayerPos = GetPlayerPosition();
int nPartnerPos = GetPartnerPosition();
int nRound = pDOC->GetNumTricksPlayed();
// we only check for hi/lo signals on rounds 1 & 2
// if we led this round and partner just discarded, check for a signal
if ( (nRoundLead == nPlayerPos) && (nCurrPlayer == nPartnerPos) &&
(pCard != pTopCard) && (pCard->GetSuit() == pCardLed->GetSuit()) )
{
// is this a signal?
if (m_nPartnerSuitPreference[nSuitLed] == 0)
{
// check if partner played high (above a six)
// this is the first indication of a suit preference
if (pCard->GetFaceValue() > 6)
{
status << "2PLYSIG0! Partner discarded high with the " & pCard->GetFaceName() &
", so consider it a suit preference signal.\n";
m_nPartnerSuitPreference[nSuitLed] = 1;
}
else
{
status << "2PLYSIG1! Partner discarded low with the " & pCard->GetFaceName() &
", so consider it a signal that he dislikes the suit.\n";
m_nPartnerSuitPreference[nSuitLed] = -1;
}
}
else if ((nRound >= 1) && ((m_nPartnerSuitPreference[nSuitLed] == 1) || (m_nPartnerSuitPreference[nSuitLed] == -1)) )
{
// see if we led the previous trick in the same suit, and
// partner discarded high or low
int nPrevRound = nRound - 1;
CCard* nPrevCardLed = pDOC->GetGameTrickCard(nPrevRound, nPlayerPos);
if ( (pDOC->GetGameTrickLead(nPrevRound) == nPlayerPos) &&
(pDOC->GetGameTrickWinner(nPrevRound) != nPartnerPos) &&
(nPrevCardLed->GetSuit() == nSuitLed) )
{
// then check partner's discards from last round
CCard* pPrevCard = pDOC->GetGameTrickCard(nPrevRound, nPartnerPos);
int nPrevCardVal = pPrevCard->GetFaceValue();
if ( (m_nPartnerSuitPreference[nSuitLed] == 1) &&
(pCard->GetFaceValue() < nPrevCardVal) )
{
// high-lo
status << "2PLYSIG6! Partner discarded low with the " & pCard->GetFaceName() &
" after discarding the " & pPrevCard->GetFaceName() &
" last round, thus marking a high-low suit preference signal.\n";
m_nPartnerSuitPreference[nSuitLed] = 2;
}
else if ( (m_nPartnerSuitPreference[nSuitLed] == -1) &&
(pCard->GetFaceValue() > nPrevCardVal) )
{
// lo-high
status << "2PLYSIG8! Partner discarded high with the " & pCard->GetFaceName() &
" after discarding the " & pPrevCard->GetFaceName() &
" last round, thus marking a low-high signal for a poor suit.\n";
m_nPartnerSuitPreference[nSuitLed] = -2;
}
}
}
}
//
// if this is notrump, record partner's opening lead suit
//
if ( (nRound == 0) && !ISSUIT(nTrumpSuit) && (nRoundLead == nPartnerPos) &&
(nCurrPlayer == nPartnerPos) )
{
m_nPartnersPrioritySuit = pCardLed->GetSuit();
}
//
// check for an unusual discard / suit preference signal
// rules:
// ------
// 1. partner must not have led
// 2. partner must have discarded in a side suit (not trumps)
// 3. this must be his first play of a card in this suit,
// 3. the discarded card face value must be greater than a six
// 4. this must be no later than the 4th round
//
int nPartnerSuit = pCard->GetSuit();
if ( (nRoundLead != nPartnerPos) && (nCurrPlayer == nPartnerPos) &&
(nPartnerSuit != pCardLed->GetSuit()) &&
(m_ppGuessedHands[m_nPartnerPosition]->GetSuit(nPartnerSuit).GetNumCardsPlayed() == 0) &&
(nPartnerSuit != nTrumpSuit) && (pCard->GetFaceValue() > 6) && (nRound <= 6))
{
// this is s suit preference signal
status << "2PLYSIG20! Partner discarded high in the " & STSS(pCard->GetSuit()) &
" suit, indicating a preference for that suit.\n";
m_nPartnerSuitPreference[pCard->GetSuit()] = 1;
}
}
//
// RecordCardUndo()
//
// note that a card has been undone
//
void CDefenderPlayEngine::RecordCardUndo(int nPos, CCard* pCard)
{
// call the base class
CPlayEngine::RecordCardUndo(nPos, pCard);
}
//
// RecordTrickUndo()
//
// note that the current trick has been undone
// (currently not used)
//
void CDefenderPlayEngine::RecordTrickUndo()
{
// call base class
CPlayEngine::RecordTrickUndo();
}
//
// RecordRoundComplete()
//
void CDefenderPlayEngine::RecordRoundComplete(int nPos, CCard* pCard)
{
// call the base class
CPlayEngine::RecordRoundComplete(nPos, pCard);
}
//
// RestartPlay()
//
void CDefenderPlayEngine::RestartPlay()
{
// call base class
CPlayEngine::RestartPlay();
}
/////////////////////////////////////////////////////////////////////////////
//
//
// UItilities
//
//
//
/////////////////////////////////////////////////////////////////////////////
//
// IsPartnerVoidInSuit()
//
BOOL CDefenderPlayEngine::IsPartnerVoidInSuit(int nSuit)
{
CGuessedHandHoldings* pPartnersHand = m_pPlayer->GetGuessedHand(GetPartnerPosition());
// see if partner has shown out
return pPartnersHand->IsSuitShownOut(nSuit);
}
//
// GetNumClaimableTricks()
//
int CDefenderPlayEngine::GetNumClaimableTricks()
{
int numWinners = 0;
// special handling for suit contracts
int nTrumpSuit = pDOC->GetTrumpSuit();
if (ISSUIT(nTrumpSuit))
{
// first check trumps
CSuitHoldings& trumpSuit = m_pHand->GetSuit(nTrumpSuit);
numWinners = trumpSuit.GetNumWinners();
// add the winners in the other suits only if we hold all winners in trumps
// and at least as many as the outstanding trumps
int numOutstandingTrumps = GetNumOutstandingCards(nTrumpSuit);
if ( (numWinners >= trumpSuit.GetNumCards()) & (numWinners >= numOutstandingTrumps) )
{
for(int i=0;i<4;i++)
{
if (i != nTrumpSuit)
numWinners += m_pHand->GetSuit(i).GetNumWinners();
}
}
}
else
{
// NT contract is straightforward
numWinners = m_pHand->GetNumWinners();
}
//
return numWinners;
}
/////////////////////////////////////////////////////////////////////////////
//
//
// Analysis routines
//
//
/////////////////////////////////////////////////////////////////////////////
//
// AssessPosition()
//
// called once on each round of play
//
void CDefenderPlayEngine::AssessPosition()
{
// call base class first
CPlayEngine::AssessPosition();
// then perform class-specific operations
m_bUsingGIB = theApp.GetValue(tbEnableGIBForDefender);
}
//
// GetLeadCard()
//
// - returns a card to lead
//
CCard* CDefenderPlayEngine::GetLeadCard()
{
// get basic info
CPlayerStatusDialog& status = *m_pStatusDlg;
int nRound = pDOC->GetPlayRound();
int nTrumpSuit = pDOC->GetTrumpSuit();
int nContractLevel = pDOC->GetContractLevel();
CCard* pLeadCard = NULL;
int nPlayerPos = GetPlayerPosition();
int nPartnerPos = GetPartnerPosition();
int nPartnersSuit = NONE;
int nDeclarer = pDOC->GetDeclarerPosition();
int nDummy = pDOC->GetDummyPosition();
// see if this is the first round
if (nRound == 0)
{
// this is the opening lead
status << "2PLYLEAD! Making opening lead as " & PositionToString(nPlayerPos) &
" against a contract of " & ContractToFullString(pDOC->GetContract()) & ".\n";
// review partner's bidding history
nPartnersSuit = ReviewBiddingHistory();
// special code -- see if we can beat a slam contract in our hand
if (nContractLevel == 7)
{
// do we have an Ace?
if (m_pHand->GetNumAces() >= 1)
{
for(int nSuit=CLUBS;nSuit<=SPADES;nSuit++)
{
if (m_pHand->GetSuit(nSuit).HasAce())
break;
}
pLeadCard = m_pHand->GetSuit(nSuit).GetTopCard();
status << "PLYLDS1! We can " & (ISSUIT(nTrumpSuit)? "most likely" : "") &
"defeat the contract by playing the " & pLeadCard->GetName() & " here, so lead it now.\n";
return pLeadCard;
}
}
else if (nContractLevel == 6)
{
// do we have two Aces?
if (m_pHand->GetNumAces() >= 2)
{
// lead Aces from the shortest suit first
int nSuit, nIndex = 3;
while(nIndex >= 0)
{
nSuit = m_pHand->GetSuitsByLength(nIndex);
if (m_pHand->GetSuit(nSuit).HasAce())
break;
nSuit = m_pHand->GetSuitsByLength(nIndex);
nIndex--;
}
pLeadCard = m_pHand->GetSuit(nSuit).GetTopCard();
status << "PLYLDS2! We can " & (ISSUIT(nTrumpSuit)? "most likely" : "") &
"defeat the contract by playing two Aces, so lead them starting with the shortest suit -- lead the " & pLeadCard->GetName() & " first.\n";
return pLeadCard;
}
// else see if we have an Ace-King combo
int nSuit, nIndex = 3;
while(nIndex >= 0)
{
nSuit = m_pHand->GetSuitsByLength(nIndex);
if (m_pHand->GetSuit(nSuit).HasAce() && m_pHand->GetSuit(nSuit).HasKing())
break;
nSuit = m_pHand->GetSuitsByLength(nIndex);
nIndex--;
}
if (nIndex >= 0)
{
pLeadCard = m_pHand->GetSuit(nSuit).GetTopCard();
status << "PLYLDS3! We " & (ISSUIT(nTrumpSuit)? "can" : "may be able to") &
" defeat the contract by playing two Aces, so lead them starting with the shortest suit -- lead the " & pLeadCard->GetName() & " first.\n";
return pLeadCard;
}
}
// see if we're playing in a suit contract
if (ISSUIT(nTrumpSuit))
{
// leading in the first round against a suit contract
// either lead from a singleton, from partner's suit, from a sequence,
// an Ace, a trump, or simply 4th best
// see if we have a singleton and some trumps
if ((m_pHand->GetNumSingletons() >= 1) && (m_pHand->GetNumTrumps() > 0))
{
// but don't lead the trump suit, or a singleton honor
for(int i=0;i<4;i++)
{
CSuitHoldings& suit = m_pHand->GetSuit(i);
if ((suit.GetNumCards() == 1) && (suit[0]->GetFaceValue() < JACK) && (i != nTrumpSuit))
{
pLeadCard = suit[0];
status << "PLYLD4! Lead the singleton " & pLeadCard->GetName() & ".\n";
ASSERT(m_pHand->HasCard(pLeadCard));
return pLeadCard;
}
}
}
// see if we can lead partner's suit
if (ISSUIT(nPartnersSuit))
{
CSuitHoldings& partnersSuit = m_pHand->GetSuit(nPartnersSuit);
if (partnersSuit.GetNumCards() >= 3)
{
// lead LOW with 3+ cards
pLeadCard = partnersSuit.GetBottomCard();
status << "PLYLD4! Lead the bottom card of partner's " & STSS(nPartnersSuit) &
" suit (the " & pLeadCard->GetFaceName() & ".\n";
ASSERT(m_pHand->HasCard(pLeadCard));
return pLeadCard;
}
else if (partnersSuit.GetNumCards() > 0)
{
pLeadCard = partnersSuit[0];
status << "PLYLD4! Lead the top card of partner's " & STSS(nPartnersSuit) &
" suit (the " & pLeadCard->GetFaceName() & ".\n";
ASSERT(m_pHand->HasCard(pLeadCard));
return pLeadCard;
}
}
// else can't lead partner's suit, so check each of our suits
for(int i=0;i<4;i++)
{
int nSuit = m_pHand->GetSuitsByPreference(i);
if (nSuit == nTrumpSuit)
continue; // but don't lead a trump
CSuitHoldings& suit = m_pHand->GetSuit(nSuit);
if (suit.GetNumCards() == 0)
continue;
CCardList& seq = suit.GetTopSequence();
if (seq.GetNumCards() > 1)
{
// we have a sequence in our favorite suit, so lead from the top
pLeadCard = seq[0];
status << "PLYLD15! Lead the " & pLeadCard->GetName() &
" from the top of the { " & seq.GetHoldingsString() &
"} sequence.\n";
ASSERT(m_pHand->HasCard(pLeadCard));
return pLeadCard;
}
}
// lead out an ace if available
if (m_pHand->GetNumAces() > 0)
{
// but not the trump ace
for(int i=0;i<4;i++)
{
CSuitHoldings& suit = m_pHand->GetSuit(i);
if (suit.HasAce() && (i != nTrumpSuit))
{
pLeadCard = suit[0];
status << "PLYLD18! Lead the " & pLeadCard->GetName() & " to grab a quick trick.\n";
ASSERT(m_pHand->HasCard(pLeadCard));
return pLeadCard;
}
}
}
// lead a trump if appropriate (add code here later)
// here, no other lead was found, so simply lead 4th best
return Get4thBestLeadCard();
}
else
{
// else here we're playing in a notrump contract
// lead the the top card from a 3-card honor sequence, or
// otherwise 4th card of the best suit
int nSuit = m_pHand->GetSuitsByPreference(0);
CSuitHoldings& suit = m_pHand->GetSuit(nSuit);
CCardList& seq = suit.GetTopSequence();
if ((seq.GetNumCards() >= 3) && (IsHonor(seq[0]->GetFaceValue())))
{
// we have a 3-card honor sequence in out favorite suit, so lead from the top
pLeadCard = seq[0];
m_nPrioritySuit = nSuit;
status << "PLYLD22! Lead the " & pLeadCard->GetName() &
" from the top of the " & seq.GetNumCards() &
"-card sequence {" & seq.GetHoldingsString() & "}.\n";
}
else
{
// lead fourth-best from this suit OR our longest suit
CCard* pLeadCard = Get4thBestLeadCard();
m_nPrioritySuit = pLeadCard->GetSuit();
return pLeadCard;
}
}
}
else
{
//
// playing in the second or later round
//
// on the second trick, see if we can defeat a slam contract
// by playing a winner
int nDefendingTeam = m_pPlayer->GetTeam();
CHandHoldings& dummyHand = pDOC->GetDummyPlayer()->GetHand();
//
if ((nRound == 1) && (nContractLevel == 6) && (m_pHand->GetNumWinners() > 0) &&
(pDOC->GetNumTricksWonByTeam(nDefendingTeam) == 1))
{
// lead an Ace if we have one
if (m_pHand->GetNumAces() >= 1)
{
// lead Aces from the shortest suit first
int nSuit, nIndex = 3;
while(nIndex >= 0)
{
nSuit = m_pHand->GetSuitsByLength(nIndex);
if (m_pHand->GetSuit(nSuit).HasAce())
break;
nSuit = m_pHand->GetSuitsByLength(nIndex);
nIndex--;
}
pLeadCard = m_pHand->GetSuit(nSuit).GetTopCard();
if (ISSUIT(nTrumpSuit))
status << "PLYLDS6a! Try to defeat the contract now by playing the " & pLeadCard->GetName() & ".\n";
else
status << "PLYLDS6b! Try to defeat the contract now by playing the " & pLeadCard->GetName() & ".\n";
return pLeadCard;
}
// else play a king
if (m_pHand->GetNumKings() >= 1)
{
// lead Aces from the shortest suit first
int nSuit, nIndex = 3;
while(nIndex >= 0)
{
nSuit = m_pHand->GetSuitsByLength(nIndex);
if (m_pHand->GetSuit(nSuit).HasKing())
break;
nSuit = m_pHand->GetSuitsByLength(nIndex);
nIndex--;
}
pLeadCard = m_pHand->GetSuit(nSuit).GetTopCard();
if (ISSUIT(nTrumpSuit))
status << "PLYLDS6a! Try to defeat the contract now by playing the " & pLeadCard->GetName() & ".\n";
else
status << "PLYLDS6b! Defeat the contract now by playing the " & pLeadCard->GetName() & ".\n";
return pLeadCard;
}
}
// if we're leading in the second or later round after winning
// the prior round which partner led, return partner's suit
CCard* pPartnersLastCard = pDOC->GetGameTrickCard(nRound-1, nPartnerPos);
int nSuit = pPartnersLastCard->GetSuit();
int nLead = pDOC->GetGameTrickLead(nRound-1);
if ( (pDOC->GetGameTrickWinner(nRound-1) == nPlayerPos) &&
(pDOC->GetGameTrickLead(nRound-1) == nPartnerPos) &&
(m_pHand->GetNumCardsInSuit(nSuit) > 0) )
{
// but don't bother returning the suit if this is a suit contract
// and dummy is void in the suit with trumps available
BOOL bDummyCanTrump = FALSE;
CHandHoldings& dummyHand = pDOC->GetDummyPlayer()->GetHand();
if ((ISSUIT(nTrumpSuit)) && (dummyHand.GetNumCardsInSuit(nSuit) == 0) &&
(dummyHand.GetNumTrumps() > 0))
{
status << "3PLYLDRT1! We'd like to return partner's " & STS(nSuit) &
" suit, but dummy might ruff.\n";
bDummyCanTrump = TRUE;
}
// also don't bother returning the suit if this is a suit contract
// and dummy has the top cards outstanding -- i.e., if dummy's
// top card is higher than any outstanding cards
CCardList outstandingCards;
int numOutstandingCards = GetOutstandingCards(nSuit, outstandingCards);
if ((ISSUIT(nTrumpSuit)) && (dummyHand.GetNumCardsInSuit(nSuit) > 0) &&
(numOutstandingCards > 0) &&
(dummyHand.GetSuit(nSuit).GetTopCardVal() > outstandingCards[0]->GetFaceValue()))
{
status << "3PLYLDRT2! We'd like to return partner's " & STS(nSuit) &
" suit, but dummy will most likely win.\n";
bDummyCanTrump = TRUE;
}
// also see if declarer might trump
// that's if there are trumps outstanding, but none in dummy and
// partner has shown out
BOOL bDeclarerCanTrump = FALSE;
if ( (ISSUIT(nTrumpSuit)) && (GetNumOutstandingCards(nTrumpSuit) > 0) &&
(IsPartnerVoidInSuit(nTrumpSuit)) &&
(dummyHand.GetNumTrumps() == 0) )
{
status << "3PLYLDRT4! We'd like to return partner's " & STS(nSuit) &
" suit, but declarer might ruff.\n";
bDeclarerCanTrump = TRUE;
}
// return a winner in this suit if we have one; else return a low card
CSuitHoldings& suit = m_pHand->GetSuit(nSuit);
if ((suit.GetNumCards() > 0) && (!bDummyCanTrump) && (!bDeclarerCanTrump))
{
// mark this as the priority suit
m_nPrioritySuit = nSuit;
if (suit.GetNumWinners() > 0)
pLeadCard = suit.GetTopSequence().GetBottomCard();
else
pLeadCard = suit.GetBottomCard();
status << "PLYLDRT8! Return partner's " & STSS(nSuit) & " suit with the " &
pLeadCard->GetFaceName() & ".\n";
ASSERT(m_pHand->HasCard(pLeadCard));
return pLeadCard;
}
}
// see if we're playing in a suit contract
if (ISSUIT(nTrumpSuit))
{
// check if partner has shown out of a suit
int numPartnerVoidSuits = 0;
int nPartnerVoidSuits[4] = { NONE, NONE, NONE, NONE };
for(int i=0;i<4;i++)
{
if (IsPartnerVoidInSuit(i) && (i != nTrumpSuit))
nPartnerVoidSuits[numPartnerVoidSuits++] = i;
}
// see if there are trumps outstanding, partner has not shown out
// of trumps, and _has_ shown out of a suit, _and_ we have no
// winners in that suit (whew!)
int numOutstandingTrumps = GetNumOutstandingCards(nTrumpSuit);
if ( (numOutstandingTrumps > 0) &&
(!IsPartnerVoidInSuit(nTrumpSuit)) && (numPartnerVoidSuits > 0))
{
// see if we can lead the suit
for(int j=0;j<numPartnerVoidSuits;j++)
{
CSuitHoldings& suit = m_pHand->GetSuit(nPartnerVoidSuits[j]);
// if (suit.GetNumLosers() > 0)
if ((suit.GetNumCards() > 0) && (suit.GetNumWinners() == 0))
{
// by all means, lead the suit
pLeadCard = suit.GetBottomCard();
status << "PLYLD32! Partner is void in " & STS(nPartnerVoidSuits[j]) &
" and may still have some trumps, so lead the " &
pLeadCard->GetName() & ".\n";
ASSERT(m_pHand->HasCard(pLeadCard));
return pLeadCard;
}
}
}
// here, we had no luck in finding a suit for partner to ruff, so
// see if he expressed a suit preference
pLeadCard = FindLeadCardFromPartnerPreference();
if (pLeadCard)
{
ASSERT(m_pHand->HasCard(pLeadCard));
return pLeadCard;
}
// no luck in finding suit for partner, so pick our own suit to lead
// if all trumps have been accounted for, cash winners
// but not if it's a trump winner -- save it for later
pLeadCard = CashWinners();
if (pLeadCard && pLeadCard->GetSuit() != nTrumpSuit)
{
ASSERT(m_pHand->HasCard(pLeadCard));
return pLeadCard;
}
// else we've got nothing to lead, so try to lead something safe
// lead a suit that the opponents won't ruff
/*
CGuessedSuitHoldings* pDeclarerTrumps = m_pPlayer->GetGuessedHand(nDeclarer)->GetSuit(nTrumpSuit);
CSuitHoldings& dummyTrumps = dummyHand.Suit(nTrumpSuit);
BOOL bDeclarerMayTrump = FALSE, bDummyMayTrump = FALSE;
if ((numOutstandingTrumps > 0) && !pDeclarerTrumps->IsSuitShownOut())
bDeclarerMayTrump = TRUE;
if (dummyTrumps.GetnumCards() > 0)
bDummyMayTrump = TRUE;
*/
// check each suit
for(int j=0;j<4;j++)
{
if (j == nTrumpSuit)
continue;
CSuitHoldings& suit = m_pHand->GetSuit(j);
if (suit.GetNumCards() == 0)
continue;
CGuessedSuitHoldings& declarerSuit = m_pPlayer->GetGuessedHand(nDeclarer)->GetSuit(j);
CSuitHoldings& dummySuit = dummyHand.GetSuit(j);
int numOutstandingCards = GetNumOutstandingCards(j);
// lead a suit if declarer/dummy has not shown out of the suit
if (!declarerSuit.IsSuitShownOut() && (dummySuit.GetNumCards() > 0) &&
(numOutstandingCards >= 1) )
{
pLeadCard = suit.GetBottomCard();
status << "PLYLD42! With no good card to lead, lead the " & pLeadCard->GetName() &
" since the opponents have not shown out of the " & STSS(pLeadCard->GetSuit()) &
" suit.\n";
ASSERT(m_pHand->HasCard(pLeadCard));
return pLeadCard;
}
}
// else lead a suit that dummy won't ruff
for(j=0;j<4;j++)
{
CSuitHoldings& suit = m_pHand->GetSuit(j);
if (suit.GetNumCards() == 0)
continue;
CSuitHoldings& dummySuit = dummyHand.GetSuit(j);
int numOutstandingCards = GetNumOutstandingCards(j);
if ((dummySuit.GetNumCards() > 0) && (numOutstandingCards >= 1))
{
pLeadCard = suit.GetBottomCard();
status << "PLYLD43! With no good card to lead, lead the " & pLeadCard->GetName() &
" since dummy is not void in the " & STSS(pLeadCard->GetSuit()) &
" suit (we don't know about declarer, though).\n";
ASSERT(m_pHand->HasCard(pLeadCard));
return pLeadCard;
}
}
}
else
{
// playing in notrump
// see if partner has expressed a suit preference and has not shown out
// of that suit
pLeadCard = FindLeadCardFromPartnerPreference();
if (!ISSUIT(m_nPrioritySuit) && pLeadCard && !IsPartnerVoidInSuit(pLeadCard->GetSuit()))
{
// if we like this suit also, mark it as our priority suit
int nPartnerSuit = pLeadCard->GetSuit();
// if (!ISSUIT(m_nPrioritySuit) && (m_pHand->GetSuitStrength(nPartnerSuit) >= SS_GOOD_SUPPORT))
// mark this as the suit to develop if we like it
if (m_pHand->GetSuitStrength(nPartnerSuit) >= SS_GOOD_SUPPORT)
m_nPrioritySuit = nPartnerSuit;
//
ASSERT(m_pHand->HasCard(pLeadCard));
return pLeadCard;
}
// see if we're currently trying to develop a suit
if (ISSUIT(m_nPrioritySuit))
{
// see if we should keep trying to develop the suit
if ((m_pHand->GetSuit(m_nPrioritySuit).GetNumCards() <= 1))
{
status << "4PLYLD50! With " &
((m_pHand->GetSuit(m_nPrioritySuit).GetNumCards() == 1)? "only one card" : "no cards") &
" left in " & STS(m_nPrioritySuit) &
", there's no point in continuing to try and develop the suit.\n";
m_nPrioritySuit = NONE;
}
//
if (ISSUIT(m_nPrioritySuit))
{
// also stop developing the suit if partner and either N/S
// show out (i.e., the opponents have no entries)
BOOL bPartnerShownOut = m_ppGuessedHands[m_nPartnerPosition]->IsSuitShownOut(m_nPrioritySuit);
BOOL bLHOShowOut = m_ppGuessedHands[GetNextPlayer(m_nPosition)]->IsSuitShownOut(m_nPrioritySuit);
BOOL bRHOShowOut = m_ppGuessedHands[GetPrevPlayer(m_nPosition)]->IsSuitShownOut(m_nPrioritySuit);
if ( (bPartnerShownOut && bLHOShowOut && !bRHOShowOut) ||
(bPartnerShownOut && bLHOShowOut && !bRHOShowOut) )
{
status << "4PLYLD50! Since both partner and " & (bLHOShowOut? "LHO" : "RHO") &
" have shown out, there is no point in trying to keep developing the " &
STSS(m_nPrioritySuit) & " suit, as we'll only provide entries for the oponents.\n";
m_nPrioritySuit = NONE;
}
}
}
// if we're still trying to develop or work a suit,
// keep playing the suit
int nSuit = NONE;
if (ISSUIT(m_nPrioritySuit))
nSuit = m_nPrioritySuit;
else if (ISSUIT(m_nPartnersPrioritySuit) && (m_pHand->GetSuit(m_nPartnersPrioritySuit).GetNumCards() >= 1))
nSuit = m_nPartnersPrioritySuit;
if (ISSUIT(nSuit))
{
CSuitHoldings& suit = m_pHand->GetSuit(nSuit);
pLeadCard = suit[0];
status << "PLYLD76! Continue developing " &
((nSuit == m_nPrioritySuit)? "our " : "partner's ") & STSS(nSuit) &
" suit by playing the " & pLeadCard->GetName() & ".\n";
ASSERT(m_pHand->HasCard(pLeadCard));
// check for suit depletion
if (suit.GetNumCards() == 1)
{
// mark the the priority suit is no longer a priority!
if (nSuit == m_nPrioritySuit)
m_nPrioritySuit = NONE;
else if (nSuit == m_nPartnersPrioritySuit)
m_nPartnersPrioritySuit = NONE;
}
return pLeadCard;
}
// if we have no suit we're trying to develop yet, but have a good suit
// with an honor sequence, lead the top card of the sequence to
// try and promote the cards below
nSuit = m_pHand->GetPreferredSuit();
CSuitHoldings& suit = m_pHand->GetSuit(nSuit);
if (!ISSUIT(m_nPrioritySuit) && (suit.GetStrength() >= SS_OPENABLE) &&
(suit.GetNumHonors() >= 2) && (nRound <= 6))
{
// pLeadCard = suit[0];
// mark this as the suit to develop
m_nPrioritySuit = nSuit;
pLeadCard = suit.GetLowestCardAbove(9);
status << "PLYLD80! Try to develop our " & STSS(nSuit) &
" suit by playing the " & pLeadCard->GetName() & ".\n";
ASSERT(m_pHand->HasCard(pLeadCard));
return pLeadCard;
}
// no luck in finding suit for partner, so pick our own suit to lead
// try cashing winners
pLeadCard = CashWinners();
if (pLeadCard)
{
status << "PLYLD84! With no other obvious plays, cash a winner with the " & pLeadCard->GetName() & ".\n";
ASSERT(m_pHand->HasCard(pLeadCard));
return pLeadCard;
}
// else we're SOL
}
}
// if all else fails, give up and call the base class
// TODO: specialize the code from the base class to pick the suit that dummy
// is weakest in
if (pLeadCard == NULL)
pLeadCard = CPlayEngine::GetLeadCard();
// done
return pLeadCard;
}
//
// FindLeadCardFromPartnerPreference()
//
// - see if partner expressed a suit preference, and if so, see whether
// we can lead a card in the suit
//
// - returns a suit partner bid, if any
//
int CDefenderPlayEngine::ReviewBiddingHistory()
{
int numBidsMade = pDOC->GetNumBidsMade();
int numBiddingRounds = numBidsMade / 4;
// we have the # of bidding rounds (rounded up)
// check partner's bids for each round
int nPartner = GetPartnerPosition();
int nTopSuit = NONE;
int nTopSuitVal = 0;
for(int i=0;i<4;i++)
{
int nBid = pDOC->GetBidByPlayer(nPartner, i);
if (ISBID(nBid))
{
int nSuit = BID_SUIT(nBid);
m_nPartnerSuitPreference[nSuit]++;
if (m_nPartnerSuitPreference[nSuit] > nTopSuitVal)
{
// this suit is the "favorite"
nTopSuit = nSuit;
nTopSuitVal = m_nPartnerSuitPreference[nSuit];
}
}
}
// return the suit bid most by partner, if any
return nTopSuit;
}
//
// FindLeadCardFromPartnerPreference()
//
// - see if partner expressed a suit preference, and if so, see whether