-
Notifications
You must be signed in to change notification settings - Fork 1
/
Drop.cpp
357 lines (314 loc) · 11.2 KB
/
Drop.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
//----------------------------------------------------------------------------------------
//
// 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.
//
//----------------------------------------------------------------------------------------
//
// Drop.cpp
//
#include "stdafx.h"
#include "EasyB.h"
#include "EasyBDoc.h"
#include "Deck.h"
#include "Card.h"
#include "Drop.h"
#include "Player.h"
#include "PlayEngine.h"
#include "CombinedHoldings.h"
#include "CardLocation.h"
#include "GuessedHandHoldings.h"
#include "PlayerStatusDialog.h"
//
//==================================================================
// constructon & destruction
CDrop::CDrop(int nTargetHand, int nTargetOpponent, CCardList* pEnemyCards, int nSuit, int nCardVal) :
CPlay(CPlay::DROP, nTargetHand, nSuit, PP_LIKELY_WINNER),
m_nTargetOpponent(nTargetOpponent),
m_nCardVal(nCardVal)
{
Init();
m_pEnemyOrKeyCardsList = pEnemyCards;
}
CDrop::CDrop(int nTargetHand, int nTargetOpponent, CCardList* pEnemyCards, CCard* pCard) :
CPlay(CPlay::DROP, nTargetHand, NONE, PP_LIKELY_WINNER),
m_nTargetOpponent(nTargetOpponent)
{
VERIFY(pCard);
m_nSuit = pCard->GetSuit();
m_nCardVal = pCard->GetFaceValue();
m_pEnemyOrKeyCardsList = pEnemyCards;
Init();
}
CDrop::CDrop(CDrop& srcPlay) :
CPlay(srcPlay.m_nPlayType, srcPlay.m_nTargetHand)
{
m_nTargetHand = srcPlay.m_nTargetHand;
m_nTargetOpponent = AGAINST_EITHER;
m_nSuit = srcPlay.m_nSuit;
m_nCardVal = srcPlay.m_nCardVal;
m_pEnemyOrKeyCardsList = srcPlay.m_pEnemyOrKeyCardsList;
}
CDrop::~CDrop()
{
}
//
void CDrop::Clear()
{
CPlay::Clear();
//
m_nCardVal = NONE;
m_nTargetOpponent = AGAINST_EITHER;
m_numTargetCards = 0;
m_strDropCards.Empty();
}
//
void CDrop::Init()
{
CPlay::Init();
//
m_pConsumedCard = deck.GetCard(m_nSuit, m_nCardVal);
m_numTargetCards = m_pEnemyOrKeyCardsList->GetNumCards();
if (m_numTargetCards == 1)
m_strDropCards.Format("the %s", m_pEnemyOrKeyCardsList->GetAt(0)->GetName());
else
m_strDropCards.Format("one or more of the { %s }", m_pEnemyOrKeyCardsList->GetHoldingsString());
// form name & description
m_strDropMessage = " to try and drop " + m_strDropCards;
m_strName.Format("Drop %s", m_strDropCards);
m_strDescription.Format("Drop %s w/ the %s.", m_strDropCards, m_pConsumedCard->GetFaceName());
}
//
CString CDrop::GetFullDescription()
{
// form description
return FormString("Play the %s from %s%s.", m_pConsumedCard->GetName(), ((m_nTargetHand == IN_HAND)? "hand" : "Dummy"), m_strDropMessage);
}
//
// Perform()
//
PlayResult CDrop::Perform(CPlayEngine& playEngine, CCombinedHoldings& combinedHand,
CCardLocation& cardLocation, CGuessedHandHoldings** ppGuessedHands,
CPlayerStatusDialog& status, CCard*& pPlayCard)
{
// this code is almost, but not quite, the same as a cash
// 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);
CCombinedSuitHoldings& combinedSuit = combinedHand.GetSuit(m_nSuit);
CCard* pCardLed = pDOC->GetCurrentTrickCardByOrder(0);
int nSuitLed = pCardLed? pCardLed->GetSuit() : NONE;
// see if a trump was played in this round
BOOL bTrumped = FALSE;
if ((nSuitLed != pDOC->GetTrumpSuit()) && (pDOC->WasTrumpPlayed()))
bTrumped = TRUE;
pPlayCard = NULL;
BOOL bDropSucceeded = FALSE;
BOOL bLHODropped = FALSE;
CCard *pRHOCard, *pLHOCard;
//
CString strLHO = bPlayingInHand? playEngine.szLHO : playEngine.szRHO;
CString strRHO = bPlayingInHand? playEngine.szRHO : playEngine.szLHO;
// test preconditions
if (!CPlay::IsPlayUsable(combinedHand, playEngine))
{
m_nStatusCode = PLAY_INACTIVE;
return PLAY_POSTPONE;
}
// check our position in the play
switch(nOrdinal)
{
case 0:
// we're leading, player #0
if ( ((bPlayingInHand) && (m_nTargetHand == IN_HAND)) ||
((!bPlayingInHand) && (m_nTargetHand == IN_DUMMY)) )
{
// leading high from hand
pPlayCard = m_pConsumedCard;
status << "PLDRP10! Lead the " & pPlayCard->GetName() &
m_strDropMessage & ".\n";
}
else
{
// in the opposite hand, so lead
if ( (bPlayingInHand && playerSuit.IsVoid()) ||
(!bPlayingInHand && dummySuit.IsVoid()) )
{
// oops, have no card to lead from this hand
m_nStatusCode = PLAY_INACTIVE;
return PLAY_POSTPONE;
}
//
pPlayCard = bPlayingInHand? playerSuit.GetBottomCard(): dummySuit.GetBottomCard();
status << "PLDRP11! Lead low with the " & pPlayCard->GetName() &
" from " & (bPlayingInHand? "hand" : "dummy") & " up to the " &
m_pConsumedCard->GetFaceName() & " in " &
(bPlayingInHand? "dummy" : "hand") & m_strDropMessage & ".\n";
}
// all went OK
m_nStatusCode = PLAY_IN_PROGRESS;
break;
case 1:
// playing second -- check if the correct suit was led
if (nSuitLed != m_nSuit)
{
// wrong suit led, so no point here
m_nStatusCode = PLAY_INACTIVE;
return PLAY_POSTPONE;
}
// LHO at least led the correct suit, so try the drop
// see if it's aimed at either RHO or both
if ((m_nTargetOpponent != AGAINST_RHO) && (m_nTargetOpponent != AGAINST_EITHER))
{
// no can do
m_nStatusCode = PLAY_INACTIVE;
return PLAY_POSTPONE;
}
// see if LHO dropped a key card
pLHOCard = pDOC->GetCurrentTrickCardByOrder(0);
if (m_pEnemyOrKeyCardsList->HasCard(pLHOCard))
bLHODropped = TRUE;
// see if this is the correct hand for the cash
if ( ((bPlayingInHand) && (m_nTargetHand == IN_HAND)) ||
((!bPlayingInHand) && (m_nTargetHand == IN_DUMMY)) )
{
pPlayCard = m_pConsumedCard;
if (bLHODropped)
status << "PLDRP50! " & strLHO & " led the " & pCardLed->GetName() &
"; capture it with the " & pPlayCard->GetFaceName() &
" in " & (bPlayingInHand? "hand" : "dummy") & ".\n";
else
status << "PLDRP51! " & strLHO & " led the " & pCardLed->GetName() &
"; try to drop the " & m_strDropCards & " from " & strRHO &
"by playing the "& pPlayCard->GetFaceName() & " from " & (bPlayingInHand? "hand" : "dummy") & ".\n";
m_nStatusCode = PLAY_IN_PROGRESS;
}
else
{
// in the opposite hand, so skip the play
m_nStatusCode = PLAY_POSTPONE;
return m_nStatusCode;
}
break;
case 2:
// playing third -- make sure play is in progress
if (m_nStatusCode != PLAY_IN_PROGRESS)
return PLAY_INACTIVE;
// see if the wrong suit was led (which shouldn't happen,
// if the play is still active)
if (nSuitLed != m_nSuit)
{
m_nStatusCode = PLAY_INACTIVE;
return PLAY_POSTPONE;
}
// see if RHO has trumped
if (bTrumped)
{
status << "4PLDRP64! " & strRHO & " has trumped, so abandon the drop play for this round.\n";
m_nStatusCode = PLAY_INACTIVE;
return PLAY_POSTPONE;
}
// see if RHO dropped a key card
pRHOCard = pDOC->GetCurrentTrickCardByOrder(1);
if (m_pEnemyOrKeyCardsList->HasCard(pRHOCard))
bDropSucceeded = TRUE;
// check which hand we're plaing in
if ( ((bPlayingInHand) && (m_nTargetHand == IN_HAND)) ||
((!bPlayingInHand) && (m_nTargetHand == IN_DUMMY)) )
{
// cashing from hand, so do it
pPlayCard = m_pConsumedCard;
if (bDropSucceeded)
status << "PLDRP60! " & strRHO & " drped the " & pRHOCard->GetName() &
" under our " & pCardLed->GetFaceName() & " as hoped; rein it in with the " &
pPlayCard->GetName() & " in " & ((bPlayingInHand)? "hand" : "dummy") & ".\n";
else
status << "PLDRP61! " & strRHO & " did not drop a desired card (" & m_strDropCards &
"), but perhaps " & strLHO & " might, so continue and play the " &
pPlayCard->GetName() & " from " & ((bPlayingInHand)? "hand" : "dummy") &
" and hope " & strLHO & " drops " & m_strDropCards & ".\n";
}
else
{
// cash is/was in the other hand, so discard
pPlayCard = playEngine.GetDiscard();
if (bDropSucceeded)
status << "PLDRP64! " & strRHO & " dropped the " & pRHOCard->GetName() &
" under our " & pCardLed->GetFaceName() & " as hoped; now discard the " & pPlayCard->GetName() &
" from " & ((bPlayingInHand)? "hand" : "dummy") & ".\n";
else
status << "PLDRP65! " & strRHO & " did not drop a desired card (" & m_strDropCards &
"), but perhaps " & strLHO & " might; continue the play by discarding the " &
pPlayCard->GetName() & " from " & ((bPlayingInHand)? "hand" : "dummy") & ".\n";
}
// all's OK
m_nStatusCode = PLAY_COMPLETE;
break;
case 3:
// playing fourth -- make sure the play is in progress
// play was started by LHO, and we hoped to drop RHO's card
if (m_nStatusCode != PLAY_IN_PROGRESS)
return PLAY_INACTIVE;
// see if we succeeded -- i.e., RHO dropped the desired card
pRHOCard = pDOC->GetCurrentTrickCardByOrder(2);
if (m_pEnemyOrKeyCardsList->HasCard(pRHOCard))
bDropSucceeded = TRUE;
// if the play ends here, cash if the drop worked
if ( ((bPlayingInHand) && (m_nTargetHand == IN_HAND)) ||
((!bPlayingInHand) && (m_nTargetHand == IN_DUMMY)) )
{
/*
* shouldn't be here!
if (bDropSucceeded)
{
// yup, the card dropped, so rein it in
pPlayCard = m_pConsumedCard;
status << "PLDRP80! " & strRHO & " dropped the " & pRHOCard->GetName() &
", so finish the drop play with the " & m_pConsumedCard->GetName() & ".\n";
m_nStatusCode = PLAY_COMPLETE;
return m_nStatusCode;
}
else
{
// the drop did not work
status << "PLDRP81! " & strRHO & " did not drop the " & pRHOCard->GetName() &
" as hoped for, so forget the drop play.\n";
m_nStatusCode = PLAY_INACTIVE;
return PLAY_POSTPONE;
}
*/
m_nStatusCode = PLAY_POSTPONE;
return m_nStatusCode;
}
// else we cashed from the other hand, so discard here
pPlayCard = playEngine.GetDiscard();
if (bDropSucceeded)
{
// yup, the card dropped, so rein it in
status << "PLDRP90! " & strRHO & " dropped the " & pRHOCard->GetName() &
", so finish the drop play and discard the " & pPlayCard->GetName() &
" from " & (bPlayingInHand? "hand" : "dummy") & ".\n";
}
else
{
// the drop did not work
status << "4PLDRP81! The play failed, as " & strRHO & " did not drop the " & pRHOCard->GetName() &
" as hoped for -- discard the " & pPlayCard->GetName() &
" from " & (bPlayingInHand? "hand" : "dummy") & ".\n";
m_nStatusCode = PLAY_INACTIVE;
return PLAY_POSTPONE;
}
//
m_nStatusCode = PLAY_COMPLETE;
break;
}
// done
ASSERT(pPlayCard->IsValid());
return m_nStatusCode;
}