Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

399 lines
11 KiB

  1. /***************************************************************************/
  2. /** Microsoft Windows **/
  3. /** Copyright(c) Microsoft Corp., 1991, 1992 **/
  4. /***************************************************************************/
  5. /****************************************************************************
  6. lead.cpp
  7. Aug 92, JimH
  8. May 93, JimH chico port
  9. Logic to select lead card is here
  10. ****************************************************************************/
  11. #include "hearts.h"
  12. #include "main.h"
  13. #include "resource.h"
  14. #include "debug.h" // undef _DEBUG instead to remove messages
  15. /****************************************************************************
  16. computer::SelectLeadCard
  17. ****************************************************************************/
  18. SLOT computer::SelectLeadCard(handinfotype &h)
  19. {
  20. SLOT s;
  21. TRACE0("leading. ");
  22. // count cards left and check for two of clubs
  23. SLOT s2Clubs = EMPTY;
  24. int cTricksLeft = 0;
  25. for (s = 0; s < MAXSLOT; s++)
  26. {
  27. if (cd[s].IsValid())
  28. {
  29. cTricksLeft++;
  30. if (cd[s].ID() == TWOCLUBS)
  31. s2Clubs = s;
  32. }
  33. }
  34. if (s2Clubs != EMPTY)
  35. {
  36. TRACE0("lead 2 of clubs. ");
  37. return s2Clubs;
  38. }
  39. // If the Queen of Spades has not yet been played, try to force it out.
  40. // See if we are "spade safe" -- i.e., if we have no spades that are
  41. // queen or higher. If we are safe, lead the lowest spade.
  42. if (!h.bQSPlayed) // this is only interesting if queen not yet played
  43. {
  44. BOOL bHaveSpades = (sLowCard[SPADES] != EMPTY);
  45. BOOL bSpadeSafe = (nHighVal[SPADES] < QUEEN);
  46. if (bHaveSpades && bSpadeSafe)
  47. {
  48. TRACE0("try to force out spades, ");
  49. PLAY(sLowCard[SPADES]);
  50. return sLowCard[SPADES];
  51. }
  52. }
  53. // Now we just want to lead the lowest card of the best suit
  54. int suit = SureLossSuit(h.bHeartsBroken);
  55. if (suit == EMPTY)
  56. suit = BestSuitToLose(h.bHeartsBroken);
  57. // Be brave early on and lead a card near the midpoint of the suit
  58. // (if the queen of spades has been played already.)
  59. // Later on, just lead the lowest card of that suit.
  60. if (cTricksLeft > 8 && suit != HEARTS && h.bQSPlayed)
  61. {
  62. TRACE0("try midslot. ");
  63. s = MidSlot(suit);
  64. }
  65. else
  66. {
  67. s = sLowCard[suit];
  68. }
  69. PLAY(s);
  70. return s;
  71. }
  72. /****************************************************************************
  73. computer::NotifyNewRound
  74. Each player gets this call immediately after cards are passed.
  75. It is a chance to initialize tables, etc.
  76. ****************************************************************************/
  77. void computer::NotifyNewRound()
  78. {
  79. // assume all cards are available
  80. for (int suit = 0; suit < MAXSUIT; suit++)
  81. for (int i = 0; i < (KING+2); i++)
  82. nAvailable[suit][i] = TRUE;
  83. // mark cards in hand as unavailable
  84. for (SLOT s = 0; s < MAXSLOT; s++)
  85. nAvailable[cd[s].Suit()][cd[s].Value2()] = FALSE;
  86. }
  87. /****************************************************************************
  88. computer::NotifyEndHand
  89. Each player gets this call immediately after the hand is completed.
  90. The computer player here looks through the handplayed and marks
  91. those cards are no longer available.
  92. ****************************************************************************/
  93. void computer::NotifyEndHand(handinfotype &h)
  94. {
  95. for (int i = 0; i < 4; i++)
  96. nAvailable[h.cardplayed[i]->Suit()][h.cardplayed[i]->Value2()] = FALSE;
  97. }
  98. /****************************************************************************
  99. computer::CardsAboveLow(int suit)
  100. Returns number of available (not yet played) cards that can beat the
  101. low card held for the given suit.
  102. ****************************************************************************/
  103. int computer::CardsAboveLow(int suit)
  104. {
  105. int count = 0;
  106. for (int i = nLowVal[suit]+1; i < (KING+2); i++)
  107. if (nAvailable[suit][i])
  108. {
  109. int j = i+1; // zero offset
  110. count++;
  111. }
  112. return count;
  113. }
  114. /****************************************************************************
  115. computer::CardsBelowLow(int suit)
  116. Returns number of available (not yet played) cards that are less than the
  117. low card held for the given suit.
  118. ****************************************************************************/
  119. int computer::CardsBelowLow(int suit)
  120. {
  121. int count = 0;
  122. for (int i = ACE+1; i < nLowVal[suit]; i++)
  123. if (nAvailable[suit][i])
  124. {
  125. int j = i+1; // zero offset
  126. count++;
  127. }
  128. return count;
  129. }
  130. /****************************************************************************
  131. computer::BestSuitToLose
  132. Returns the suit with the most CardsAboveLow()
  133. bIncludeHearts defaults to TRUE. Use FALSE if hearts not
  134. yet broken and you're looking for a card to lead.
  135. ****************************************************************************/
  136. int computer::BestSuitToLose(BOOL bIncludeHearts)
  137. {
  138. int best = -1;
  139. int bestsuit = EMPTY;
  140. for (int suit = 0; suit < MAXSUIT; suit++)
  141. {
  142. if (sLowCard[suit] != EMPTY) // if we have a card of this suit
  143. {
  144. if (suit != HEARTS || bIncludeHearts)
  145. {
  146. int count = CardsAboveLow(suit);
  147. #ifdef _DEBUG
  148. TRACE2("%c=%d ", suitid[suit], count);
  149. #endif
  150. if (count == best) // if they're the same, pick lower card
  151. {
  152. if (nLowVal[suit] < nLowVal[bestsuit])
  153. {
  154. bestsuit = suit;
  155. best = count;
  156. }
  157. }
  158. else if (count > best)
  159. {
  160. bestsuit = suit;
  161. best = count;
  162. }
  163. }
  164. }
  165. }
  166. if (bestsuit == EMPTY) // only if all we have are unbroken hearts
  167. return HEARTS;
  168. else
  169. return bestsuit;
  170. }
  171. /****************************************************************************
  172. computer::BestSuitToDump
  173. Returns the suit with the most CardsBelowLow()
  174. This is the most-vulnerable suit.
  175. bIncludeHearts defaults to TRUE.
  176. ****************************************************************************/
  177. int computer::BestSuitToDump(BOOL bIncludeHearts)
  178. {
  179. int best = -1;
  180. int bestsuit = EMPTY;
  181. for (int suit = 0; suit < MAXSUIT; suit++)
  182. {
  183. if (sLowCard[suit] != EMPTY) // if we have a card of this suit
  184. {
  185. if (suit != HEARTS || bIncludeHearts)
  186. {
  187. int count = CardsBelowLow(suit);
  188. #ifdef _DEBUG
  189. TRACE2("%c=%d ", suitid[suit], count);
  190. #endif
  191. if (count == best) // if they're the same, pick lower card
  192. {
  193. if (nLowVal[suit] > nLowVal[bestsuit])
  194. {
  195. bestsuit = suit;
  196. best = count;
  197. }
  198. }
  199. else if (count > best)
  200. {
  201. bestsuit = suit;
  202. best = count;
  203. }
  204. }
  205. }
  206. }
  207. if (bestsuit == EMPTY) // only if all we have are unbroken hearts
  208. return HEARTS;
  209. else
  210. return bestsuit;
  211. }
  212. /****************************************************************************
  213. computer::MidSlot
  214. Instead of choosing the high or low card from a given suit, this function
  215. can be used to pick something in the middle, where middle is defined as
  216. the card with about the same number of available cards above and below it.
  217. ****************************************************************************/
  218. SLOT computer::MidSlot(int suit)
  219. {
  220. SLOT midslot = sLowCard[suit];
  221. int maxtricks = CardsAbove(sLowCard[suit]);
  222. int tricks = maxtricks;
  223. for (SLOT s = 0; s < MAXSLOT; s++)
  224. {
  225. if ((cd[s].IsValid()) && (cd[s].Suit()==suit) && (s!=sLowCard[suit]))
  226. {
  227. int above = CardsAbove(s);
  228. if ((above < tricks) && (above > (maxtricks / 2)))
  229. {
  230. midslot = s;
  231. tricks = above;
  232. }
  233. }
  234. }
  235. return midslot;
  236. }
  237. /****************************************************************************
  238. computer::CardsAbove
  239. Similar to CardsAboveLow except that it finds number of cards in the same
  240. suit available from an arbitrary card.
  241. ****************************************************************************/
  242. int computer::CardsAbove(SLOT s)
  243. {
  244. int suit = cd[s].Suit();
  245. int count = 0;
  246. for (int i = cd[s].Value2()+1; i < (KING+2); i++)
  247. if (nAvailable[suit][i])
  248. count++;
  249. return count;
  250. }
  251. /****************************************************************************
  252. computer::CardBelow
  253. returns the slot of the next highest card of the same suit, or EMPTY
  254. ****************************************************************************/
  255. SLOT computer::CardBelow(SLOT slot)
  256. {
  257. SLOT sBelow = EMPTY;
  258. int suit = cd[slot].Suit();
  259. int value = cd[slot].Value2();
  260. int best = -1;
  261. for (SLOT s = 0; s < MAXSLOT; s++)
  262. {
  263. if ((cd[s].IsValid()) && (cd[s].Suit()==suit) && (cd[s].Value2()<value))
  264. {
  265. if (cd[s].Value2() > best)
  266. {
  267. best = cd[s].Value2();
  268. sBelow = s;
  269. }
  270. }
  271. }
  272. return sBelow;
  273. }
  274. /****************************************************************************
  275. computer::SureLossSuit
  276. Returns a suit that can be led which guarantees a loss of
  277. lead, or EMPTY if none is found.
  278. ****************************************************************************/
  279. int computer::SureLossSuit(BOOL bIncludeHearts)
  280. {
  281. for (int suit = (MAXSUIT-1); suit >= 0; --suit)
  282. {
  283. if (sLowCard[suit] != EMPTY) // if we have a card of this suit
  284. {
  285. if (suit != HEARTS || bIncludeHearts)
  286. {
  287. if (CardsAboveLow(suit) > 0 && CardsBelowLow(suit) == 0)
  288. {
  289. TRACE0("can lose this trick. ");
  290. return suit;
  291. }
  292. }
  293. }
  294. }
  295. return EMPTY;
  296. }