-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomp2.cpp
More file actions
437 lines (333 loc) · 11.8 KB
/
comp2.cpp
File metadata and controls
437 lines (333 loc) · 11.8 KB
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
/***************************************************************************/
/** Microsoft Windows **/
/** Copyright(c) Microsoft Corp., 1991, 1992 **/
/***************************************************************************/
/****************************************************************************
comp2.cpp -- wxWidgets port
Aug 92, JimH
May 93, JimH chico port
Logic for computer player to select cards to play when not holding
the lead, and initializing data tables is here.
****************************************************************************/
#include "hearts.h"
#include "main.h"
#include "resource.h"
#include "debug.h" // undef _DEBUG instead to remove messages
/****************************************************************************
computer::SelectCardToPlay
computer player chooses a card to play.
****************************************************************************/
void computer::SelectCardToPlay(handinfotype &h, bool bCheating)
{
TRACE1("<%d> ", id);
Setup(h); // calculate values of private vars
SLOT s;
if (bFirst) // am I leading?
s = SelectLeadCard(h);
else
s = SelectNonLeadCard(h);
wxASSERT(s >= 0);
wxASSERT(s < MAXSLOT);
wxASSERT(cd[s].IsValid());
SetMode(WAITING);
cd[s].Play(); // mark card as played
h.cardplayed[id] = &(cd[s]); // update handinfo
// inform other players
::move.playerid = id;
::move.cardid = cd[s].ID();
::move.playerled = h.playerled;
::move.turn = h.turn;
// In the wx single-player port, DDE is not used.
// Moves are handled locally.
// inform gamemeister
wxCommandEvent evt(wxEVT_MENU, IDM_REF);
pMainWnd->GetEventHandler()->QueueEvent(evt.Clone());
TRACE0("\n");
}
/****************************************************************************
computer::SelectNonLeadCard
This is where cards to play are selected when the computer player is
not leading.
****************************************************************************/
SLOT computer::SelectNonLeadCard(handinfotype &h)
{
bool bFirstTrick = (cardled != NULL) && (cardled->ID() == TWOCLUBS);
// If we have at least one card of the led suit...
if (sHighCard[nSuitLed] != EMPTY)
{
TRACE0("can follow suit. ");
// If there's only one card of this suit, return it.
if (sHighCard[nSuitLed] == sLowCard[nSuitLed])
{
TRACE0("must ");
PLAY(sHighCard[nSuitLed]);
return sHighCard[nSuitLed];
}
// if it's the first trick, play the high club
if (bFirstTrick)
{
TRACE0("might as well ");
PLAY(sHighCard[nSuitLed]);
return sHighCard[nSuitLed];
}
// If I am the last player in this trick, and I've won the hand anyway,
// return highest legal card (unless it's the queen of spades.)
if (bLast && (nLowVal[nSuitLed] > currentval))
{
TRACE0("must win trick. ");
if (sHighCard[nSuitLed] != sBlackLady)
{
PLAY(sHighCard[nSuitLed]);
return sHighCard[nSuitLed];
}
else
{
TRACE0("avoid queen. ");
PLAY(sLowCard[nSuitLed]);
return sLowCard[nSuitLed];
}
}
// If I am the last player and I CAN win the trick....
if (bLast && (nHighVal[nSuitLed] > currentval))
{
TRACE0("can win. ");
// Don't grab the trick if there aren't enough low cards
// left in hand. The lead may be hard to lose!
if (nLowestVal < 7) // i.e., card val < 8
{
if ((nPoints == 0) && (sHighCard[nSuitLed] != sBlackLady))
{
TRACE0("go for it. ");
PLAY(sHighCard[nSuitLed]);
return sHighCard[nSuitLed];
}
// Take a few hearts if it means losing a high spade.
if ((!h.bQSPlayed) && nSuitLed == SPADES && nPoints < 4)
{
if (nHighVal[SPADES] > QUEEN)
{
TRACE0("sacrifice hearts to lose high spade. ");
PLAY(sHighCard[SPADES]);
return sHighCard[SPADES];
}
}
TRACE0("decline. ");
}
else
{
TRACE0("no low cards. ");
}
}
// Otherwise, try to find the highest safe card to play...
SLOT safe = SafeCard(h);
if (safe != EMPTY)
{
// if someone other than me is potentially shooting,
// hold back high cards.
if (h.bShootingRisk && h.bHumanShooter && (h.nMoonShooter != id))
{
TRACE0("2nd ");
SLOT s2 = CardBelow(safe);
if (s2 != EMPTY)
safe = s2;
}
TRACE0("highest safe card. ");
PLAY(safe);
return safe;
}
// And if that fails, just play the lowest card.
TRACE0("no safe card, choose lowest. ");
if (sLowCard[nSuitLed] != sBlackLady)
{
PLAY(sLowCard[nSuitLed]);
return sLowCard[nSuitLed];
}
else
{
TRACE0("try to avoid queen. ");
PLAY(sHighCard[nSuitLed]);
return sHighCard[nSuitLed];
}
}
TRACE0("can't follow suit. ");
// At this point, there are no cards of the led suit. The first
// priority is to try to sluff off the queen of spades.
if (!bFirstTrick || !::pMainWnd->IsFirstBloodEnforced())
{
if (sBlackLady != EMPTY)
{
TRACE0("gotcha! Queen of Spades. ");
return sBlackLady;
}
}
// The next priority is to dump high spades (if queen not yet played).
if ((!h.bQSPlayed) && (nHighVal[SPADES] > QUEEN))
{
TRACE0("lose high spade. ");
PLAY(sHighCard[SPADES]);
return sHighCard[SPADES];
}
// The next priority is to find the most vulnerable suit
int mvsuit = BestSuitToDump(!bFirstTrick);
// There is an unusual situation which must be checked for explicitly.
// It's possible BestSuitToDump may return SPADES, and the high card
// is the queen. This would still be illegal if it was first round.
if (bFirstTrick && ::pMainWnd->IsFirstBloodEnforced() && mvsuit == SPADES)
{
SLOT s = sHighCard[mvsuit];
if (cd[s].ID() == BLACKLADY)
{
if (sHighCard[DIAMONDS] != EMPTY) // we know there's no clubs
mvsuit = DIAMONDS;
else if (sLowCard[SPADES] != sHighCard[SPADES])
{
TRACE0("dump low spade. ");
return sLowCard[SPADES];
}
else
mvsuit = HEARTS;
}
}
// if someone other than me is potentially shooting, hold back high cards
if (h.bShootingRisk && h.bHumanShooter && (h.nMoonShooter != id) &&
(sHighCard[mvsuit] != sLowCard[mvsuit]))
{
SLOT s = sHighCard[mvsuit];
SLOT s2 = CardBelow(s);
if (s2 != EMPTY)
s = s2;
#ifdef _DEBUG
TRACE1("hold high %c. ", suitid[mvsuit]);
#endif
PLAY(s);
return s;
}
#ifdef _DEBUG
TRACE1("dump %c. ", suitid[mvsuit]);
#endif
PLAY(sHighCard[mvsuit]);
return sHighCard[mvsuit];
}
/****************************************************************************
computer::SafeCard
Returns highest safe card or EMPTY if no safe card found.
****************************************************************************/
SLOT computer::SafeCard(handinfotype &h)
{
// Special check. If Ace of Spades is current trick winner, play the
// Queen of Spades rather than the King, even though the King is higher.
if ((sBlackLady!=EMPTY) && (nSuitLed==SPADES) && (currentval==(KING+1)))
return sBlackLady;
// Look for highest card of same suit that won't win trick.
SLOT sSafe = EMPTY; // highest safe slot
int nSafeVal = -1; // value of highest safe card
for (SLOT s = 0; s < MAXSLOT; s++)
{
if (cd[s].IsValid())
{
if (cd[s].Suit() == nSuitLed)
{
int v = cd[s].Value2();
// If card is safe (v < currentval) and card is highest
// safe card found so far (v > nSaveVal)...
if ((v < currentval) && (v > nSafeVal))
{
sSafe = s;
nSafeVal = v;
}
}
}
}
return sSafe;
}
/****************************************************************************
computer::Setup
Set up reference tables for high and low cards in each suit, etc.
****************************************************************************/
void computer::Setup(handinfotype &h)
{
cardled = h.cardplayed[h.playerled];
if (cardled)
{
nSuitLed = cardled->Suit();
nValueLed = cardled->Value2();
}
else
{
nSuitLed = EMPTY;
nValueLed = EMPTY;
}
nPoints = 0; // points in hand already
// Initialize Tables
for (int suit = 0; suit < MAXSUIT; suit++) // highs and lows by suit
{
sHighCard[suit] = EMPTY;
sLowCard[suit] = EMPTY;
nHighVal[suit] = ACE - 1; // lower than any real card
nLowVal[suit] = KING + 2; // higher than any real card
}
sHighestCard = EMPTY; // highs and lows regardless of suit
sLowestCard = EMPTY;
nHighestVal = ACE - 1;
nLowestVal = KING + 2;
// Determine currentval (the value of the winning card so far) and nPoints.
currentval = nValueLed;
for (int i = 0; i < MAXPLAYER; i++)
{
card *c = h.cardplayed[i];
if (c != NULL)
{
if (c->IsValid())
{
// First, determine if there are any point cards in play.
if (c->Suit() == HEARTS)
nPoints++;
if (c->ID() == BLACKLADY)
nPoints += 13;
// Then, find the highest card (on table) of the led suit.
if (c->Suit() == nSuitLed)
{
int v = c->Value2();
if (v > currentval)
currentval = v;
}
}
}
}
// Calculate if we're leading or completing this trick.
bFirst = (h.playerled == id);
bLast = (((h.playerled + (MAXPLAYER-1)) % MAXPLAYER) == id);
// Special check for the Queen of Spades
sBlackLady = EMPTY; // assume we don't have it
// Collect information on high and low cards in each suit.
for (SLOT s = 0; s < MAXSLOT; s++)
{
if (cd[s].IsValid())
{
int suit = cd[s].Suit();
int v = cd[s].Value2();
if (cd[s].ID() == BLACKLADY)
sBlackLady = s;
if (v < nLowVal[suit])
{
nLowVal[suit] = v;
sLowCard[suit] = s;
}
if (v < nLowestVal)
{
nLowestVal = v;
sLowestCard = s;
}
if (v > nHighVal[suit])
{
nHighVal[suit] = v;
sHighCard[suit] = s;
}
if (v > nHighestVal)
{
nHighestVal = v;
sHighestCard = s;
}
}
}
}