-
Notifications
You must be signed in to change notification settings - Fork 1
/
TypeAFinesse.cpp
228 lines (197 loc) · 6.22 KB
/
TypeAFinesse.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
//----------------------------------------------------------------------------------------
//
// 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.
//
//----------------------------------------------------------------------------------------
//
// TypeAFinesse.cpp
//
// Type A Finesse
// - opportunistic play of a non-top card in second position to
// finesse against LHO
//
#include "stdafx.h"
#include "EasyB.h"
#include "EasyBDoc.h"
#include "Player.h"
#include "Card.h"
#include "TypeAFinesse.h"
#include "PlayEngine.h"
#include "CombinedHoldings.h"
#include "CardLocation.h"
#include "GuessedHandHoldings.h"
#include "PlayerStatusDialog.h"
//
//==================================================================
// constructon & destruction
CTypeAFinesse::CTypeAFinesse(int nPlayerPosition, int nTargetHand, CCardList* pGapCards, int nSuit, int nCardVal) :
CFinesse(CFinesse::TYPE_A, nTargetHand, nPlayerPosition, pGapCards, nSuit, nCardVal, TRUE)
{
Init();
}
CTypeAFinesse::CTypeAFinesse(int nPlayerPosition, int nTargetHand, CCardList* pGapCards, CCard* pCard) :
CFinesse(CFinesse::TYPE_A, nTargetHand, nPlayerPosition, pGapCards, pCard, TRUE)
{
VERIFY(pCard);
Init();
}
CTypeAFinesse::~CTypeAFinesse()
{
}
//
void CTypeAFinesse::Clear()
{
CFinesse::Clear();
}
//
void CTypeAFinesse::Init()
{
// call base class
// CFinesse::Init();
// check the enemy and location
m_nTarget = (m_nEndingHand == CFinesse::IN_DUMMY)? AGAINST_LHO: AGAINST_RHO;
if (m_nTarget == AGAINST_LHO)
m_nTargetPos = GetNextPlayer(m_nPlayerPosition);
else
m_nTargetPos = GetPrevPlayer(m_nPlayerPosition);
// the ending hand is the target hand (opposite of default)
m_nStartingHand = m_nEndingHand = m_nTargetHand;
}
//
CString CTypeAFinesse::GetFullDescription()
{
CString strText;
return FormString("Opportunistically play the %s from %s to finesse against %s.",
m_pConsumedCard->GetFaceName(),
(m_nTargetHand == IN_HAND)? "hand" : "dummy",
PositionToString(m_nTargetPos));
// done
return strText;
}
//
// Perform()
//
PlayResult CTypeAFinesse::Perform(CPlayEngine& playEngine, CCombinedHoldings& combinedHand,
CCardLocation& cardLocation, CGuessedHandHoldings** ppGuessedHands,
CPlayerStatusDialog& status, CCard*& pPlayCard)
{
// Type A Finesse
// - opportunistic play of a non-top card in second position to
// finesse against LHO
// check which hand this is
int nOrdinal = pDOC->GetNumCardsPlayedInRound();
CPlayer* pPlayer = playEngine.GetPlayer();
BOOL bPlayingInHand = (pDOC->GetCurrentPlayer() == pPlayer);
CHandHoldings& playerHand = *(combinedHand.GetPlayerHand());
CHandHoldings& dummyHand = *(combinedHand.GetPartnerHand());
CSuitHoldings& playerSuit = playerHand.GetSuit(m_nSuit);
CSuitHoldings& dummySuit = dummyHand.GetSuit(m_nSuit);
CCard* pCardLed = pDOC->GetCurrentTrickCardByOrder(0);
int nSuitLed = NONE;
if (pCardLed)
nSuitLed = pCardLed->GetSuit();
// see if a trump was played in this round
BOOL bTrumped = FALSE;
if ((nSuitLed != pDOC->GetTrumpSuit()) && (pDOC->WasTrumpPlayed()))
bTrumped = TRUE;
CCard* pTopCard = pDOC->GetCurrentTrickHighCard();
pPlayCard = NULL;
CCard* pOppCard = NULL;
// test preconditions
if (!CPlay::IsPlayUsable(combinedHand, playEngine))
{
m_nStatusCode = PLAY_INACTIVE;
return PLAY_POSTPONE;
}
// check our position in the play
switch(nOrdinal)
{
case 0:
// can't use in first seat
m_nStatusCode = PLAY_NOT_VIABLE;
return m_nStatusCode;
case 1:
// playing second -- this is the key to the finesse
// see if the wrong suit was led
if (nSuitLed != m_nSuit)
{
m_nStatusCode = PLAY_INACTIVE;
return PLAY_POSTPONE;
}
// check which hand we're playing in
if (bPlayingInHand)
{
// playing second in our own hand (declarer)
// see if it's time to finesse
if (m_nTargetHand == IN_HAND)
{
// play the finesse card
pPlayCard = m_pConsumedCard;
status << "PLAFN20! Opportunistically finesse the " & pPlayCard->GetName() & " from hand in second position against " &
PositionToString(playEngine.GetLHOpponent()->GetPosition()) & ".\n";
}
else
{
// finessing in hand, but this is dummy? messed up
status << "4PLAFN30! We intended to finesse in hand, but ended up here in dummy in third position -- so skip this play.\n";
m_nStatusCode = PLAY_INACTIVE;
return PLAY_POSTPONE;
}
}
else
{
// playing third in dummy
// see if it's time to finnesse here
if (m_nTargetHand == IN_DUMMY)
{
// finesse the card from dummy
pPlayCard = m_pConsumedCard;
status << "PLAFN54! Opportunistically finesse the " & pPlayCard->GetName() & " from dummy in second position against " &
PositionToString(playEngine.GetRHOpponent()->GetPosition()) & ".\n";
}
else
{
// messed up
status << "4PLAFN60! We intended to finesse in hand, but ended up here in dummy in third position -- so skip this play.\n";
m_nStatusCode = PLAY_INACTIVE;
return PLAY_POSTPONE;
}
}
// all went OK
m_nStatusCode = PLAY_IN_PROGRESS;
break;
case 2:
// can't use this play in third posiiton
m_nStatusCode = PLAY_NOT_VIABLE;
return m_nStatusCode;
case 3:
// in 4th position, simply discard
if (m_nStatusCode == PLAY_IN_PROGRESS)
{
// discard here, but see if we won
if (pTopCard == m_pConsumedCard)
{
pPlayCard = playEngine.GetDiscard();
status << "PLAFN76! The finesse worked; finish the play by discarding the " & pPlayCard->GetName() & ".\n";
m_nStatusCode = PLAY_COMPLETE;
}
else
{
status << "PLAFN77! The finesse failed, so abandon the play\n";
m_nStatusCode = PLAY_NOT_VIABLE;
}
return m_nStatusCode;
}
else
{
// play is not active
m_nStatusCode = PLAY_INACTIVE;
return m_nStatusCode;
}
}
// done
ASSERT(pPlayCard->IsValid());
return m_nStatusCode;
}