/
Ruff.cpp
504 lines (473 loc) · 14.5 KB
/
Ruff.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
//----------------------------------------------------------------------------------------
//
// 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.
//
//----------------------------------------------------------------------------------------
//
// Ruff.cpp
//
#include "stdafx.h"
#include "EasyB.h"
#include "EasyBDoc.h"
#include "card.h"
#include "Ruff.h"
#include "Player.h"
#include "PlayEngine.h"
#include "CombinedHoldings.h"
#include "CombinedSuitHoldings.h"
#include "CardLocation.h"
#include "GuessedHandHoldings.h"
#include "PlayerStatusDialog.h"
//
//==================================================================
// constructon & destruction
CRuff::CRuff(int nTargetHand, int numDiscardsRequired, int nSuit, PlayProspect nPlayProspect) :
CPlay(CPlay::RUFF, nTargetHand, nSuit, nPlayProspect),
m_numDiscardsRequired(numDiscardsRequired)
{
Init();
}
CRuff::CRuff(int nTargetHand, int numDiscardsRequired, CCard* pCard, PlayProspect nPlayProspect) :
CPlay(CPlay::RUFF, nTargetHand, NONE, nPlayProspect),
m_numDiscardsRequired(numDiscardsRequired)
{
VERIFY(pCard);
m_nSuit = pCard->GetSuit();
Init();
}
CRuff::CRuff(CRuff& srcPlay) :
CPlay(srcPlay.m_nPlayType)
{
m_nTargetHand = srcPlay.m_nTargetHand;
m_nStartingHand = srcPlay.m_nStartingHand;
m_nEndingHand = srcPlay.m_nEndingHand;
m_numDiscardsRequired = srcPlay.m_numDiscardsRequired;
}
CRuff::~CRuff()
{
}
//
void CRuff::Clear()
{
CPlay::Clear();
//
m_nSuit = NONE;
}
//
void CRuff::Init()
{
CPlay::Init();
//
m_nEndingHand = m_nTargetHand;
m_nStartingHand = (m_nEndingHand == IN_HAND)? IN_DUMMY : IN_HAND;
// form name & description
m_strName.Format("%s Ruff", STSS(m_nSuit));
m_strDescription.Format("Ruff a %s in %s",
SuitToSingularString(m_nSuit),
((m_nTargetHand == IN_HAND)? "hand" : "dummy"));
}
//
CString CRuff::GetFullDescription()
{
return FormString("Ruff a %s in %s.",
SuitToSingularString(m_nSuit),
(m_nTargetHand == 0)? "hand" : "dummy");
}
//
int CRuff::UsesUpEntry()
{
// this play does use up an entry
return (m_nTargetHand == IN_DUMMY)? ENTRY_DUMMY: ENTRY_HAND;
}
//
// Perform()
//
PlayResult CRuff::Perform(CPlayEngine& playEngine, CCombinedHoldings& combinedHand,
CCardLocation& cardLocation, CGuessedHandHoldings** ppGuessedHands,
CPlayerStatusDialog& status, CCard*& pPlayCard)
{
// 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 = NONE;
if (pCardLed)
nSuitLed = pCardLed->GetSuit();
// see if a trump was played in this round
BOOL bTrumped = FALSE;
int nTrumpSuit = pDOC->GetTrumpSuit();
if ((nSuitLed != nTrumpSuit) && (pDOC->WasTrumpPlayed()))
bTrumped = TRUE;
pPlayCard = NULL;
CCard* pOppCard = NULL;
//
CCard* pRoundTopCard = pDOC->GetCurrentTrickHighCard();
CCard* pDeclarerCard = pDOC->GetCurrentTrickCard(playEngine.GetPlayerPosition());
CCard* pDummysCard = pDOC->GetCurrentTrickCard(playEngine.GetPartnerPosition());
CCard* pPartnersCard = bPlayingInHand? pDummysCard : pDeclarerCard;
BOOL bPartnerHigh = (pRoundTopCard == pPartnersCard);
//
BOOL bValid = FALSE;
// 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)
{
// playing from our own hand (declarer)? can't do so!
if (m_nTargetHand == IN_HAND)
{
// can't ruff here
// status << "4PLRUF02! Can't ruff a card when leading.\n";
m_nStatusCode = PLAY_POSTPONE;
return m_nStatusCode;
}
else
{
// ruffing in dummy -- first check eligibility
if (dummyHand.GetNumCardsInSuit(m_nSuit) > 0)
{
// can't use this now
m_nStatusCode = PLAY_POSTPONE;
return m_nStatusCode;
}
// now lead a low card of the suit from hand
if (combinedSuit.GetNumDeclarerLosers() > 0)
{
pPlayCard = playerHand.GetSuit(m_nSuit).GetBottomCard();
status << "PLRUF04! Lead a low " & STSS(m_nSuit) &
" (the " & pPlayCard->GetFaceName() & ") from hand to ruff in dummy.\n";
}
else
{
// oops, no card in the suit to lead!
status << "4PLRUF08! Oops, we wanted to ruff a " & STSS(m_nSuit) &
" in dummy, but we have no " & STSS(m_nSuit) &
" losers in hand to lead, so we have to abandon the play.\n";
m_nStatusCode = PLAY_NOT_VIABLE;
return m_nStatusCode;
}
}
}
else
{
// leading from dummy
if (m_nTargetHand == IN_DUMMY)
{
// leading from dummy & ruffing in dummy? no can do
// status << "4PLRUF12! Can't lead from dummy and ruff in dummy at the same time.\n";
m_nStatusCode = PLAY_POSTPONE;
return m_nStatusCode;
}
else
{
// ruffing in hand -- first check eligibility
if (playerHand.GetNumCardsInSuit(m_nSuit) > 0)
{
// can't use this now
m_nStatusCode = PLAY_POSTPONE;
return m_nStatusCode;
}
// now lead a low card of the suit from dummy
if (combinedSuit.GetNumDummyLosers() > 0)
{
pPlayCard = dummyHand.GetSuit(m_nSuit).GetBottomCard();
status << "PLRUF14! Lead a low " & STSS(m_nSuit) &
" (the " & pPlayCard->GetFaceName() & ") from dummy to ruff in hand.\n";
}
else
{
// oops, no card in the suit to lead!
status << "4PLRUF18! Oops, we wanted to ruff a " & STSS(m_nSuit) &
" in hand, but we have no " & STS(m_nSuit) &
" losers in dummy to lead, so we have to abandon the play.\n";
m_nStatusCode = PLAY_NOT_VIABLE;
return m_nStatusCode;
}
}
}
// at this point, all went OK
m_nStatusCode = PLAY_IN_PROGRESS;
break;
case 1:
// playing second -- may be able to ruff here, intended or not
if (nSuitLed == nTrumpSuit)
{
// but not if a trump was led
m_nStatusCode = PLAY_POSTPONE;
return m_nStatusCode;
}
// an unintended ruff is OK only if the card led is not a trump card,
// _and_ we have zero cards in the suit in the appropriate hand
if ( ((m_nTargetHand == IN_HAND) && (playerHand.GetNumCardsInSuit(nSuitLed) == 0)) ||
((m_nTargetHand == IN_DUMMY) && (dummyHand.GetNumCardsInSuit(nSuitLed) == 0)) )
{
status << "3PLRUF20! We can ruff in the suit led by the opponent.\n";
}
else
{
// status << "3PLRUF21! A ruff here is not possible.\n";
m_nStatusCode = PLAY_POSTPONE;
return m_nStatusCode;
}
// see which hand this is
if (bPlayingInHand)
{
// playing second in hand -- see where the ruff is
if (m_nTargetHand == IN_HAND)
{
// ruff here
CSuitHoldings& playerTrumps = playerHand.GetSuit(nTrumpSuit);
if (playerTrumps.GetNumCards() > 0)
{
pPlayCard = playerTrumps.GetBottomCard();
status << "PLRUF30! Ruff in hand with the " & pPlayCard->GetName() & ".\n";
}
else
{
status << "4PLRUF32! Error - no trumps left in hand to use in a ruff.\n";
m_nStatusCode = PLAY_ERROR;
return m_nStatusCode;
}
}
else
{
// we'll be ruffing in dummy later, so discard
pPlayCard = playEngine.GetDiscard();
status << "PLRUF34! We'll be ruffing in dummy, so discard the " &
pPlayCard->GetName() & " from hand.\n";
}
}
else
{
// playing second in dummy -- see where the finese card is located
if (m_nTargetHand == IN_DUMMY)
{
// ruff here
CSuitHoldings& dummyTrumps = dummyHand.GetSuit(nTrumpSuit);
if (dummyTrumps.GetNumCards() > 0)
{
pPlayCard = dummyTrumps.GetBottomCard();
status << "PLRUF40! Ruff in dummy with the " & pPlayCard->GetName() & ".\n";
}
else
{
status << "4PLRUF42! Error - no trumps left in dummy to use in a ruff.\n";
m_nStatusCode = PLAY_ERROR;
return m_nStatusCode;
}
}
else
{
// finessing in hand, so discard from dummy
pPlayCard = playEngine.GetDiscard();
status << "PLRUF44! We'll be ruffing in hand, so discard the " &
pPlayCard->GetName() & " from dummy.\n";
}
}
// done here
m_nStatusCode = PLAY_IN_PROGRESS;
break;
case 2: case 3:
// playing third -- ruff if possible
// if (m_nStatusCode != PLAY_IN_PROGRESS)
// return PLAY_INACTIVE;
// unintended ruff is OK only if the card led is not a trump card,
// _and_ we have zero cards in the suit
if (bPlayingInHand)
{
if ( (m_nStatusCode == PLAY_IN_PROGRESS) ||
((nSuitLed != nTrumpSuit) && (playerHand.GetNumCardsInSuit(nSuitLed) == 0)) )
bValid = TRUE;
}
else
{
if ( (m_nStatusCode == PLAY_IN_PROGRESS) ||
((nSuitLed != nTrumpSuit) && (dummyHand.GetNumCardsInSuit(nSuitLed) == 0)) )
bValid = TRUE;
}
//
if (bValid)
{
// bonanza
// status << "3PLRUF50! We can ruff in the suit led by the opponent.\n";
// status << "3PLRUF50! We can ruff here.\n";
}
else
{
// status << "3PLRUF51! A ruff here is not possible.\n";
m_nStatusCode = PLAY_POSTPONE;
return m_nStatusCode;
}
// at this point, we MUST be in the proper hand to ruff
if (bPlayingInHand)
{
// playing third/fourth in hand -- see where the ruff is
if (m_nTargetHand == IN_HAND)
{
// ruff here
CSuitHoldings& playerTrumps = playerHand.GetSuit(nTrumpSuit);
if (playerTrumps.GetNumCards() > 0)
{
// see if RHO trumped
if (bTrumped && (pPartnersCard != pRoundTopCard))
{
// see if we can beat it
pPlayCard = playerTrumps.GetLowestCardAbove(pRoundTopCard);
if (pPlayCard)
{
status << "PLRUF56! Overruff RHO's trump " & pRoundTopCard->GetFaceName() &
" with the " & pPlayCard->GetFaceName() & ".\n";
}
else
{
status << "4PLRUF58! RHO ruffed, and we can't overruff.\n";
m_nStatusCode = PLAY_POSTPONE;
return m_nStatusCode;
}
}
else if (pPartnersCard == pRoundTopCard)
{
status << "4PLRUF59! Partner'" & pPartnersCard->GetName() & " is high, so don't ruff it.\n";
m_nStatusCode = PLAY_POSTPONE;
return m_nStatusCode;
}
else
{
// go ahead and ruff
pPlayCard = playerTrumps.GetBottomCard();
status << "PLRUF60! Ruff in hand with the " & pPlayCard->GetName() & ".\n";
}
}
else
{
status << "4PLRUF62! Error - no trumps left in hand to use in a ruff.\n";
m_nStatusCode = PLAY_ERROR;
return m_nStatusCode;
}
}
else
{
// if we're playing 4th, we can discard (ruffed opposite)
if (nOrdinal == 3)
{
if (bPartnerHigh)
{
pPlayCard = playEngine.GetDiscard();
status << "PLRUF63! Complete the ruff by discarding the " & pPlayCard->GetName() & " from hand.\n";
}
else
{
// oops, opponents overruffed
status << "3PLRUF63a! Oops, the opponents overruffed, so skip the play.\n";
m_nStatusCode = PLAY_FAILED;
return m_nStatusCode;
}
}
else
{
// can't use _this_ ruff here
// status << "4PLRUF64! Oops, we ended up in hand opposite a ruff in dummy.\n";
// m_nStatusCode = PLAY_ERROR;
m_nStatusCode = PLAY_POSTPONE;
return m_nStatusCode;
}
}
}
else
{
// playing second in dummy -- see if we're ruffing here
if (m_nTargetHand == IN_DUMMY)
{
// ruff here
CSuitHoldings& dummyTrumps = dummyHand.GetSuit(nTrumpSuit);
if (dummyTrumps.GetNumCards() > 0)
{
// see if RHO trumped
if (bTrumped && (pPartnersCard != pRoundTopCard))
{
// see if we can beat it
pPlayCard = dummyTrumps.GetLowestCardAbove(pRoundTopCard);
if (pPlayCard)
{
status << "PLRUF70! Overruff RHO's trump " & pRoundTopCard->GetFaceName() &
" with the " & pPlayCard->GetFaceName() & ".\n";
}
else
{
status << "3PLRUF72! RHO ruffed, and we can't overruff.\n";
m_nStatusCode = PLAY_POSTPONE;
return m_nStatusCode;
}
}
else if (pPartnersCard == pRoundTopCard)
{
status << "4PLRUF73! Partner'" & pPartnersCard->GetName() & " is high, so don't ruff it.\n";
m_nStatusCode = PLAY_POSTPONE;
return m_nStatusCode;
}
else
{
// go ahead and ruff
pPlayCard = dummyTrumps.GetBottomCard();
status << "PLRUF74! Ruff in dummy with the " & pPlayCard->GetName() & ".\n";
}
}
else
{
status << "4PLRUF76! Error - no trumps left in dummy to use in a ruff.\n";
m_nStatusCode = PLAY_ERROR;
return m_nStatusCode;
}
}
else
{
// if we're playing 4th, we can discard (ruffed opposite)
if (nOrdinal == 3)
{
if (bPartnerHigh)
{
pPlayCard = playEngine.GetDiscard();
status << "PLRUF77! Complete the ruff by discarding the " & pPlayCard->GetName() & " from hand.\n";
}
else
{
// oops, opponents overruffed
status << "3PLRUF77a! Oops, the opponents overruffed, so skip the play.\n";
m_nStatusCode = PLAY_FAILED;
return m_nStatusCode;
}
}
else
{
// can't ruff here
// status << "4PLRUF78! Oops, we ended up in dummy opposite a ruff in hand.\n";
// m_nStatusCode = PLAY_ERROR;
m_nStatusCode = PLAY_POSTPONE;
return m_nStatusCode;
}
}
}
// else all went OK
m_nStatusCode = PLAY_COMPLETE;
break;
}
// done
ASSERT(pPlayCard->IsValid());
return m_nStatusCode;
}