game.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: game.cpp
  3. // Purpose: Forty Thieves patience game
  4. // Author: Chris Breeze
  5. // Modified by:
  6. // Created: 21/07/97
  7. // Copyright: (c) 1993-1998 Chris Breeze
  8. // Licence: wxWindows licence
  9. //---------------------------------------------------------------------------
  10. // Last modified: 22nd July 1998 - ported to wxWidgets 2.0
  11. /////////////////////////////////////////////////////////////////////////////
  12. // For compilers that support precompilation, includes "wx/wx.h".
  13. #include "wx/wxprec.h"
  14. #ifdef __BORLANDC__
  15. #pragma hdrstop
  16. #endif
  17. #ifndef WX_PRECOMP
  18. #include "wx/wx.h"
  19. #endif
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <time.h>
  23. #include <string.h>
  24. #include "forty.h"
  25. #include "game.h"
  26. Game::Game(int wins, int games, int score) :
  27. m_inPlay(false),
  28. m_moveIndex(0),
  29. m_redoIndex(0),
  30. m_bmap(0),
  31. m_bmapCard(0)
  32. {
  33. int i;
  34. m_pack = new Pack(2, 2 + 4 * (CardHeight + 2));
  35. srand(time(0));
  36. for (i = 0; i < 5; i++) m_pack->Shuffle();
  37. m_discard = new Discard(2, 2 + 5 * (CardHeight + 2));
  38. for (i = 0; i < 8; i++)
  39. {
  40. m_foundations[i] = new Foundation(2 + (i / 4) * (CardWidth + 2),
  41. 2 + (i % 4) * (CardHeight + 2));
  42. }
  43. for (i = 0; i < 10; i++)
  44. {
  45. m_bases[i] = new Base(8 + (i + 2) * (CardWidth + 2), 2);
  46. }
  47. Deal();
  48. m_srcPile = 0;
  49. m_liftedCard = 0;
  50. // copy the input parameters for future reference
  51. m_numWins = wins;
  52. m_numGames = games;
  53. m_totalScore = score;
  54. m_currentScore = 0;
  55. }
  56. void Game::Layout()
  57. {
  58. int i;
  59. m_pack->SetPos(2, 2 + 4 * (CardHeight + 2));
  60. m_discard->SetPos(2, 2 + 5 * (CardHeight + 2));
  61. for (i = 0; i < 8; i++)
  62. {
  63. m_foundations[i]->SetPos(2 + (i / 4) * (CardWidth + 2),
  64. 2 + (i % 4) * (CardHeight + 2));
  65. }
  66. for (i = 0; i < 10; i++)
  67. {
  68. m_bases[i]->SetPos(8 + (i + 2) * (CardWidth + 2), 2);
  69. }
  70. delete m_bmap;
  71. delete m_bmapCard;
  72. m_bmap = 0;
  73. m_bmapCard = 0;
  74. }
  75. // Make sure we delete all objects created by the game object
  76. Game::~Game()
  77. {
  78. int i;
  79. delete m_pack;
  80. delete m_discard;
  81. for (i = 0; i < 8; i++)
  82. {
  83. delete m_foundations[i];
  84. }
  85. for (i = 0; i < 10; i++)
  86. {
  87. delete m_bases[i];
  88. }
  89. delete m_bmap;
  90. delete m_bmapCard;
  91. }
  92. /*
  93. Set the score for a new player.
  94. NB: call Deal() first if the new player is to start
  95. a new game
  96. */
  97. void Game::NewPlayer(int wins, int games, int score)
  98. {
  99. m_numWins = wins;
  100. m_numGames = games;
  101. m_totalScore = score;
  102. m_currentScore = 0;
  103. }
  104. // Undo the last move
  105. void Game::Undo(wxDC& dc)
  106. {
  107. if (m_moveIndex > 0)
  108. {
  109. m_moveIndex--;
  110. Card* card = m_moves[m_moveIndex].dest->RemoveTopCard(dc);
  111. m_moves[m_moveIndex].src->AddCard(dc, card);
  112. DisplayScore(dc);
  113. }
  114. }
  115. // Redo the last move
  116. void Game::Redo(wxDC& dc)
  117. {
  118. if (m_moveIndex < m_redoIndex)
  119. {
  120. Card* card = m_moves[m_moveIndex].src->RemoveTopCard(dc);
  121. if (m_moves[m_moveIndex].src == m_pack)
  122. {
  123. m_pack->Redraw(dc);
  124. card->TurnCard(faceup);
  125. }
  126. m_moves[m_moveIndex].dest->AddCard(dc, card);
  127. DisplayScore(dc);
  128. m_moveIndex++;
  129. }
  130. }
  131. void Game::DoMove(wxDC& dc, Pile* src, Pile* dest)
  132. {
  133. if (m_moveIndex < MaxMoves)
  134. {
  135. if (src == dest)
  136. {
  137. wxMessageBox(wxT("Game::DoMove() src == dest"), wxT("Debug message"),
  138. wxOK | wxICON_EXCLAMATION);
  139. }
  140. m_moves[m_moveIndex].src = src;
  141. m_moves[m_moveIndex].dest = dest;
  142. m_moveIndex++;
  143. // when we do a move any moves in redo buffer are discarded
  144. m_redoIndex = m_moveIndex;
  145. }
  146. else
  147. {
  148. wxMessageBox(wxT("Game::DoMove() Undo buffer full"), wxT("Debug message"),
  149. wxOK | wxICON_EXCLAMATION);
  150. }
  151. if (!m_inPlay)
  152. {
  153. m_inPlay = true;
  154. m_numGames++;
  155. }
  156. DisplayScore(dc);
  157. if (HaveYouWon())
  158. {
  159. wxWindow *frame = wxTheApp->GetTopWindow();
  160. wxWindow *canvas = (wxWindow *) NULL;
  161. if (frame)
  162. {
  163. wxWindowList::compatibility_iterator node = frame->GetChildren().GetFirst();
  164. if (node) canvas = (wxWindow*)node->GetData();
  165. }
  166. // This game is over
  167. m_inPlay = false;
  168. // Redraw the score box to update games won
  169. DisplayScore(dc);
  170. if (wxMessageBox(wxT("Do you wish to play again?"),
  171. wxT("Well Done, You have won!"), wxYES_NO | wxICON_QUESTION) == wxYES)
  172. {
  173. Deal();
  174. canvas->Refresh();
  175. }
  176. else
  177. {
  178. // user cancelled the dialog - exit the app
  179. ((wxFrame*)canvas->GetParent())->Close(true);
  180. }
  181. }
  182. }
  183. void Game::DisplayScore(wxDC& dc)
  184. {
  185. wxColour bgColour = FortyApp::BackgroundColour();
  186. wxPen* pen = wxThePenList->FindOrCreatePen(bgColour, 1, wxSOLID);
  187. dc.SetTextBackground(bgColour);
  188. dc.SetTextForeground(FortyApp::TextColour());
  189. dc.SetBrush(FortyApp::BackgroundBrush());
  190. dc.SetPen(* pen);
  191. // count the number of cards in foundations
  192. m_currentScore = 0;
  193. for (int i = 0; i < 8; i++)
  194. {
  195. m_currentScore += m_foundations[i]->GetNumCards();
  196. }
  197. int x, y;
  198. m_pack->GetTopCardPos(x, y);
  199. x += 12 * CardWidth - 105;
  200. wxCoord w, h;
  201. {
  202. wxCoord width, height;
  203. dc.GetTextExtent(wxT("Average score:m_x"), &width, &height);
  204. w = width;
  205. h = height;
  206. }
  207. dc.DrawRectangle(x + w, y, 20, 4 * h);
  208. wxString str;
  209. str.Printf(wxT("%d"), m_currentScore);
  210. dc.DrawText(wxT("Score:"), x, y);
  211. dc.DrawText(str, x + w, y);
  212. y += h;
  213. str.Printf(wxT("%d"), m_numGames);
  214. dc.DrawText(wxT("Games played:"), x, y);
  215. dc.DrawText(str, x + w, y);
  216. y += h;
  217. str.Printf(wxT("%d"), m_numWins);
  218. dc.DrawText(wxT("Games won:"), x, y);
  219. dc.DrawText(str, x + w, y);
  220. y += h;
  221. int average = 0;
  222. if (m_numGames > 0)
  223. {
  224. average = (2 * (m_currentScore + m_totalScore) + m_numGames ) / (2 * m_numGames);
  225. }
  226. str.Printf(wxT("%d"), average);
  227. dc.DrawText(wxT("Average score:"), x, y);
  228. dc.DrawText(str, x + w, y);
  229. }
  230. // Shuffle the m_pack and deal the cards
  231. void Game::Deal()
  232. {
  233. int i, j;
  234. Card* card;
  235. // Reset all the piles, the undo buffer and shuffle the m_pack
  236. m_moveIndex = 0;
  237. m_pack->ResetPile();
  238. for (i = 0; i < 5; i++)
  239. {
  240. m_pack->Shuffle();
  241. }
  242. m_discard->ResetPile();
  243. for (i = 0; i < 10; i++)
  244. {
  245. m_bases[i]->ResetPile();
  246. }
  247. for (i = 0; i < 8; i++)
  248. {
  249. m_foundations[i]->ResetPile();
  250. }
  251. // Deal the initial 40 cards onto the bases
  252. for (i = 0; i < 10; i++)
  253. {
  254. for (j = 1; j <= 4; j++)
  255. {
  256. card = m_pack->RemoveTopCard();
  257. card->TurnCard(faceup);
  258. m_bases[i]->AddCard(card);
  259. }
  260. }
  261. if (m_inPlay)
  262. {
  263. // player has started the game and then redealt
  264. // and so we must add the score for this game to the total score
  265. m_totalScore += m_currentScore;
  266. }
  267. m_currentScore = 0;
  268. m_inPlay = false;
  269. }
  270. // Redraw the m_pack, discard pile, the bases and the foundations
  271. void Game::Redraw(wxDC& dc)
  272. {
  273. int i;
  274. m_pack->Redraw(dc);
  275. m_discard->Redraw(dc);
  276. for (i = 0; i < 8; i++)
  277. {
  278. m_foundations[i]->Redraw(dc);
  279. }
  280. for (i = 0; i < 10; i++)
  281. {
  282. m_bases[i]->Redraw(dc);
  283. }
  284. DisplayScore(dc);
  285. if (m_bmap == 0)
  286. {
  287. m_bmap = new wxBitmap(CardWidth, CardHeight);
  288. m_bmapCard = new wxBitmap(CardWidth, CardHeight);
  289. // Initialise the card bitmap to the background colour
  290. wxMemoryDC memoryDC;
  291. memoryDC.SelectObject(*m_bmapCard);
  292. memoryDC.SetPen( *wxTRANSPARENT_PEN );
  293. memoryDC.SetBrush(FortyApp::BackgroundBrush());
  294. memoryDC.DrawRectangle(0, 0, CardWidth, CardHeight);
  295. memoryDC.SelectObject(*m_bmap);
  296. memoryDC.DrawRectangle(0, 0, CardWidth, CardHeight);
  297. memoryDC.SelectObject(wxNullBitmap);
  298. }
  299. }
  300. // Test to see if the point (x, y) is over the top card of one of the piles
  301. // Returns pointer to the pile, or 0 if (x, y) is not over a pile
  302. // or the pile is empty
  303. Pile* Game::WhichPile(int x, int y)
  304. {
  305. if (m_pack->GetCard(x, y) &&
  306. m_pack->GetCard(x, y) == m_pack->GetTopCard())
  307. {
  308. return m_pack;
  309. }
  310. if (m_discard->GetCard(x, y) &&
  311. m_discard->GetCard(x, y) == m_discard->GetTopCard())
  312. {
  313. return m_discard;
  314. }
  315. int i;
  316. for (i = 0; i < 8; i++)
  317. {
  318. if (m_foundations[i]->GetCard(x, y) &&
  319. m_foundations[i]->GetCard(x, y) == m_foundations[i]->GetTopCard())
  320. {
  321. return m_foundations[i];
  322. }
  323. }
  324. for (i = 0; i < 10; i++)
  325. {
  326. if (m_bases[i]->GetCard(x, y) &&
  327. m_bases[i]->GetCard(x, y) == m_bases[i]->GetTopCard())
  328. {
  329. return m_bases[i];
  330. }
  331. }
  332. return 0;
  333. }
  334. // Left button is pressed - if cursor is over the m_pack then deal a card
  335. // otherwise if it is over a card pick it up ready to be dragged - see MouseMove()
  336. bool Game::LButtonDown(wxDC& dc, int x, int y)
  337. {
  338. m_srcPile = WhichPile(x, y);
  339. if (m_srcPile == m_pack)
  340. {
  341. Card* card = m_pack->RemoveTopCard();
  342. if (card)
  343. {
  344. m_pack->Redraw(dc);
  345. card->TurnCard(faceup);
  346. m_discard->AddCard(dc, card);
  347. DoMove(dc, m_pack, m_discard);
  348. }
  349. m_srcPile = 0;
  350. }
  351. else if (m_srcPile)
  352. {
  353. m_srcPile->GetTopCardPos(m_xPos, m_yPos);
  354. m_xOffset = m_xPos - x;
  355. m_yOffset = m_yPos - y;
  356. // Copy the area under the card
  357. // Initialise the card bitmap to the background colour
  358. {
  359. wxMemoryDC memoryDC;
  360. memoryDC.SelectObject(*m_bmap);
  361. m_liftedCard = m_srcPile->RemoveTopCard(memoryDC, m_xPos, m_yPos);
  362. }
  363. // Draw the card in card bitmap ready for blitting onto
  364. // the screen
  365. {
  366. wxMemoryDC memoryDC;
  367. memoryDC.SelectObject(*m_bmapCard);
  368. m_liftedCard->Draw(memoryDC, 0, 0);
  369. }
  370. }
  371. return m_srcPile != 0;
  372. }
  373. // Called when the left button is double clicked
  374. // If a card is under the pointer and it can move elsewhere then move it.
  375. // Move onto a foundation as first choice, a populated base as second and
  376. // an empty base as third choice.
  377. // NB Cards in the m_pack cannot be moved in this way - they aren't in play
  378. // yet
  379. void Game::LButtonDblClk(wxDC& dc, int x, int y)
  380. {
  381. Pile* pile = WhichPile(x, y);
  382. if (!pile) return;
  383. // Double click on m_pack is the same as left button down
  384. if (pile == m_pack)
  385. {
  386. LButtonDown(dc, x, y);
  387. }
  388. else
  389. {
  390. Card* card = pile->GetTopCard();
  391. if (card)
  392. {
  393. int i;
  394. // if the card is an ace then try to place it next
  395. // to an ace of the same suit
  396. if (card->GetPipValue() == 1)
  397. {
  398. for(i = 0; i < 4; i++)
  399. {
  400. Card* m_topCard = m_foundations[i]->GetTopCard();
  401. if ( m_topCard )
  402. {
  403. if (m_topCard->GetSuit() == card->GetSuit() &&
  404. m_foundations[i + 4] != pile &&
  405. m_foundations[i + 4]->GetTopCard() == 0)
  406. {
  407. pile->RemoveTopCard(dc);
  408. m_foundations[i + 4]->AddCard(dc, card);
  409. DoMove(dc, pile, m_foundations[i + 4]);
  410. return;
  411. }
  412. }
  413. }
  414. }
  415. // try to place the card on a foundation
  416. for(i = 0; i < 8; i++)
  417. {
  418. if (m_foundations[i]->AcceptCard(card) && m_foundations[i] != pile)
  419. {
  420. pile->RemoveTopCard(dc);
  421. m_foundations[i]->AddCard(dc, card);
  422. DoMove(dc, pile, m_foundations[i]);
  423. return;
  424. }
  425. }
  426. // try to place the card on a populated base
  427. for(i = 0; i < 10; i++)
  428. {
  429. if (m_bases[i]->AcceptCard(card) &&
  430. m_bases[i] != pile &&
  431. m_bases[i]->GetTopCard())
  432. {
  433. pile->RemoveTopCard(dc);
  434. m_bases[i]->AddCard(dc, card);
  435. DoMove(dc, pile, m_bases[i]);
  436. return;
  437. }
  438. }
  439. // try to place the card on any base
  440. for(i = 0; i < 10; i++)
  441. {
  442. if (m_bases[i]->AcceptCard(card) && m_bases[i] != pile)
  443. {
  444. pile->RemoveTopCard(dc);
  445. m_bases[i]->AddCard(dc, card);
  446. DoMove(dc, pile, m_bases[i]);
  447. return;
  448. }
  449. }
  450. }
  451. }
  452. }
  453. // Test to see whether the game has been won:
  454. // i.e. m_pack, discard and bases are empty
  455. bool Game::HaveYouWon()
  456. {
  457. if (m_pack->GetTopCard()) return false;
  458. if (m_discard->GetTopCard()) return false;
  459. for(int i = 0; i < 10; i++)
  460. {
  461. if (m_bases[i]->GetTopCard()) return false;
  462. }
  463. m_numWins++;
  464. m_totalScore += m_currentScore;
  465. m_currentScore = 0;
  466. return true;
  467. }
  468. // See whether the card under the cursor can be moved somewhere else
  469. // Returns 'true' if it can be moved, 'false' otherwise
  470. bool Game::CanYouGo(int x, int y)
  471. {
  472. Pile* pile = WhichPile(x, y);
  473. if (pile && pile != m_pack)
  474. {
  475. Card* card = pile->GetTopCard();
  476. if (card)
  477. {
  478. int i;
  479. for(i = 0; i < 8; i++)
  480. {
  481. if (m_foundations[i]->AcceptCard(card) && m_foundations[i] != pile)
  482. {
  483. return true;
  484. }
  485. }
  486. for(i = 0; i < 10; i++)
  487. {
  488. if (m_bases[i]->GetTopCard() &&
  489. m_bases[i]->AcceptCard(card) &&
  490. m_bases[i] != pile)
  491. {
  492. return true;
  493. }
  494. }
  495. }
  496. }
  497. return false;
  498. }
  499. // Called when the left button is released after dragging a card
  500. // Scan the piles to see if this card overlaps a pile and can be added
  501. // to the pile. If the card overlaps more than one pile on which it can be placed
  502. // then put it on the nearest pile.
  503. void Game::LButtonUp(wxDC& dc, int x, int y)
  504. {
  505. if (m_srcPile)
  506. {
  507. // work out the position of the dragged card
  508. x += m_xOffset;
  509. y += m_yOffset;
  510. Pile* nearestPile = 0;
  511. int distance = (CardHeight + CardWidth) * (CardHeight + CardWidth);
  512. // find the nearest pile which will accept the card
  513. int i;
  514. for (i = 0; i < 8; i++)
  515. {
  516. if (DropCard(x, y, m_foundations[i], m_liftedCard))
  517. {
  518. if (m_foundations[i]->CalcDistance(x, y) < distance)
  519. {
  520. nearestPile = m_foundations[i];
  521. distance = nearestPile->CalcDistance(x, y);
  522. }
  523. }
  524. }
  525. for (i = 0; i < 10; i++)
  526. {
  527. if (DropCard(x, y, m_bases[i], m_liftedCard))
  528. {
  529. if (m_bases[i]->CalcDistance(x, y) < distance)
  530. {
  531. nearestPile = m_bases[i];
  532. distance = nearestPile->CalcDistance(x, y);
  533. }
  534. }
  535. }
  536. // Restore the area under the card
  537. wxMemoryDC memoryDC;
  538. memoryDC.SelectObject(*m_bmap);
  539. dc.Blit(m_xPos, m_yPos, CardWidth, CardHeight,
  540. &memoryDC, 0, 0, wxCOPY);
  541. // Draw the card in its new position
  542. if (nearestPile)
  543. {
  544. // Add to new pile
  545. nearestPile->AddCard(dc, m_liftedCard);
  546. if (nearestPile != m_srcPile)
  547. {
  548. DoMove(dc, m_srcPile, nearestPile);
  549. }
  550. }
  551. else
  552. {
  553. // Return card to src pile
  554. m_srcPile->AddCard(dc, m_liftedCard);
  555. }
  556. m_srcPile = 0;
  557. m_liftedCard = 0;
  558. }
  559. }
  560. bool Game::DropCard(int x, int y, Pile* pile, Card* card)
  561. {
  562. bool retval = false;
  563. if (pile->Overlap(x, y))
  564. {
  565. if (pile->AcceptCard(card))
  566. {
  567. retval = true;
  568. }
  569. }
  570. return retval;
  571. }
  572. void Game::MouseMove(wxDC& dc, int mx, int my)
  573. {
  574. if (m_liftedCard)
  575. {
  576. wxMemoryDC memoryDC;
  577. memoryDC.SelectObject(*m_bmap);
  578. int dx = mx + m_xOffset - m_xPos;
  579. int dy = my + m_yOffset - m_yPos;
  580. if (abs(dx) >= CardWidth || abs(dy) >= CardHeight)
  581. {
  582. // Restore the area under the card
  583. dc.Blit(m_xPos, m_yPos, CardWidth, CardHeight,
  584. &memoryDC, 0, 0, wxCOPY);
  585. // Copy the area under the card in the new position
  586. memoryDC.Blit(0, 0, CardWidth, CardHeight,
  587. &dc, m_xPos + dx, m_yPos + dy, wxCOPY);
  588. }
  589. else if (dx >= 0)
  590. {
  591. // dx >= 0
  592. dc.Blit(m_xPos, m_yPos, dx, CardHeight, &memoryDC, 0, 0, wxCOPY);
  593. if (dy >= 0)
  594. {
  595. // dy >= 0
  596. dc.Blit(m_xPos + dx, m_yPos, CardWidth - dx, dy, &memoryDC, dx, 0, wxCOPY);
  597. memoryDC.Blit(0, 0, CardWidth - dx, CardHeight - dy,
  598. &memoryDC, dx, dy, wxCOPY);
  599. memoryDC.Blit(0, CardHeight - dy, CardWidth - dx, dy,
  600. &dc, m_xPos + dx, m_yPos + CardHeight, wxCOPY);
  601. }
  602. else
  603. {
  604. // dy < 0
  605. dc.Blit(m_xPos + dx, m_yPos + dy + CardHeight, CardWidth - dx, -dy,
  606. &memoryDC, dx, CardHeight + dy, wxCOPY);
  607. memoryDC.Blit(0, -dy, CardWidth - dx, CardHeight + dy,
  608. &memoryDC, dx, 0, wxCOPY);
  609. memoryDC.Blit(0, 0, CardWidth - dx, -dy,
  610. &dc, m_xPos + dx, m_yPos + dy, wxCOPY);
  611. }
  612. memoryDC.Blit(CardWidth - dx, 0, dx, CardHeight,
  613. &dc, m_xPos + CardWidth, m_yPos + dy, wxCOPY);
  614. }
  615. else
  616. {
  617. // dx < 0
  618. dc.Blit(m_xPos + CardWidth + dx, m_yPos, -dx, CardHeight,
  619. &memoryDC, CardWidth + dx, 0, wxCOPY);
  620. if (dy >= 0)
  621. {
  622. dc.Blit(m_xPos, m_yPos, CardWidth + dx, dy, &memoryDC, 0, 0, wxCOPY);
  623. memoryDC.Blit(-dx, 0, CardWidth + dx, CardHeight - dy,
  624. &memoryDC, 0, dy, wxCOPY);
  625. memoryDC.Blit(-dx, CardHeight - dy, CardWidth + dx, dy,
  626. &dc, m_xPos, m_yPos + CardHeight, wxCOPY);
  627. }
  628. else
  629. {
  630. // dy < 0
  631. dc.Blit(m_xPos, m_yPos + CardHeight + dy, CardWidth + dx, -dy,
  632. &memoryDC, 0, CardHeight + dy, wxCOPY);
  633. memoryDC.Blit(-dx, -dy, CardWidth + dx, CardHeight + dy,
  634. &memoryDC, 0, 0, wxCOPY);
  635. memoryDC.Blit(-dx, 0, CardWidth + dx, -dy,
  636. &dc, m_xPos, m_yPos + dy, wxCOPY);
  637. }
  638. memoryDC.Blit(0, 0, -dx, CardHeight,
  639. &dc, m_xPos + dx, m_yPos + dy, wxCOPY);
  640. }
  641. m_xPos += dx;
  642. m_yPos += dy;
  643. // draw the card in its new position
  644. memoryDC.SelectObject(*m_bmapCard);
  645. dc.Blit(m_xPos, m_yPos, CardWidth, CardHeight,
  646. &memoryDC, 0, 0, wxCOPY);
  647. }
  648. }
  649. //----------------------------------------------//
  650. // The Pack class: holds the two decks of cards //
  651. //----------------------------------------------//
  652. Pack::Pack(int x, int y) : Pile(x, y, 0, 0)
  653. {
  654. for (m_topCard = 0; m_topCard < NumCards; m_topCard++)
  655. {
  656. m_cards[m_topCard] = new Card(1 + m_topCard / 2, facedown);
  657. }
  658. m_topCard = NumCards - 1;
  659. }
  660. void Pack::Shuffle()
  661. {
  662. Card* temp[NumCards];
  663. int i;
  664. // Don't try to shuffle an empty m_pack!
  665. if (m_topCard < 0) return;
  666. // Copy the cards into a temporary array. Start by clearing
  667. // the array and then copy the card into a random position.
  668. // If the position is occupied then find the next lower position.
  669. for (i = 0; i <= m_topCard; i++)
  670. {
  671. temp[i] = 0;
  672. }
  673. for (i = 0; i <= m_topCard; i++)
  674. {
  675. int pos = rand() % (m_topCard + 1);
  676. while (temp[pos])
  677. {
  678. pos--;
  679. if (pos < 0) pos = m_topCard;
  680. }
  681. m_cards[i]->TurnCard(facedown);
  682. temp[pos] = m_cards[i];
  683. m_cards[i] = 0;
  684. }
  685. // Copy each card back into the m_pack in a random
  686. // position. If position is occupied then find nearest
  687. // unoccupied position after the random position.
  688. for (i = 0; i <= m_topCard; i++)
  689. {
  690. int pos = rand() % (m_topCard + 1);
  691. while (m_cards[pos])
  692. {
  693. pos++;
  694. if (pos > m_topCard) pos = 0;
  695. }
  696. m_cards[pos] = temp[i];
  697. }
  698. }
  699. void Pack::Redraw(wxDC& dc)
  700. {
  701. Pile::Redraw(dc);
  702. wxString str;
  703. str.Printf(wxT("%d "), m_topCard + 1);
  704. dc.SetBackgroundMode( wxSOLID );
  705. dc.SetTextBackground(FortyApp::BackgroundColour());
  706. dc.SetTextForeground(FortyApp::TextColour());
  707. dc.DrawText(str, m_x + CardWidth + 5, m_y + CardHeight / 2);
  708. }
  709. void Pack::AddCard(Card* card)
  710. {
  711. if (card == m_cards[m_topCard + 1])
  712. {
  713. m_topCard++;
  714. }
  715. else
  716. {
  717. wxMessageBox(wxT("Pack::AddCard() Undo error"), wxT("Forty Thieves: Warning"),
  718. wxOK | wxICON_EXCLAMATION);
  719. }
  720. card->TurnCard(facedown);
  721. }
  722. Pack::~Pack()
  723. {
  724. for (m_topCard = 0; m_topCard < NumCards; m_topCard++)
  725. {
  726. delete m_cards[m_topCard];
  727. }
  728. }
  729. //------------------------------------------------------//
  730. // The Base class: holds the initial pile of four cards //
  731. //------------------------------------------------------//
  732. Base::Base(int x, int y) : Pile(x, y, 0, 12)
  733. {
  734. m_topCard = -1;
  735. }
  736. bool Base::AcceptCard(Card* card)
  737. {
  738. bool retval = false;
  739. if (m_topCard >= 0)
  740. {
  741. if (m_cards[m_topCard]->GetSuit() == card->GetSuit() &&
  742. m_cards[m_topCard]->GetPipValue() - 1 == card->GetPipValue())
  743. {
  744. retval = true;
  745. }
  746. }
  747. else
  748. {
  749. // pile is empty - ACCEPT
  750. retval = true;
  751. }
  752. return retval;
  753. }
  754. //----------------------------------------------------------------//
  755. // The Foundation class: holds the cards built up from the ace... //
  756. //----------------------------------------------------------------//
  757. Foundation::Foundation(int x, int y) : Pile(x, y, 0, 0)
  758. {
  759. m_topCard = -1;
  760. }
  761. bool Foundation::AcceptCard(Card* card)
  762. {
  763. bool retval = false;
  764. if (m_topCard >= 0)
  765. {
  766. if (m_cards[m_topCard]->GetSuit() == card->GetSuit() &&
  767. m_cards[m_topCard]->GetPipValue() + 1 == card->GetPipValue())
  768. {
  769. retval = true;
  770. }
  771. }
  772. else if (card->GetPipValue() == 1)
  773. {
  774. // It's an ace and the pile is empty - ACCEPT
  775. retval = true;
  776. }
  777. return retval;
  778. }
  779. //----------------------------------------------------//
  780. // The Discard class: holds cards dealt from the m_pack //
  781. //----------------------------------------------------//
  782. Discard::Discard(int x, int y) : Pile(x, y, 19, 0)
  783. {
  784. m_topCard = -1;
  785. }
  786. void Discard::Redraw(wxDC& dc)
  787. {
  788. if (m_topCard >= 0)
  789. {
  790. if (m_dx == 0 && m_dy == 0)
  791. {
  792. m_cards[m_topCard]->Draw(dc, m_x, m_y);
  793. }
  794. else
  795. {
  796. int x = m_x;
  797. int y = m_y;
  798. for (int i = 0; i <= m_topCard; i++)
  799. {
  800. m_cards[i]->Draw(dc, x, y);
  801. x += m_dx;
  802. y += m_dy;
  803. if (i == 31)
  804. {
  805. x = m_x;
  806. y = m_y + CardHeight / 3;
  807. }
  808. }
  809. }
  810. }
  811. else
  812. {
  813. Card::DrawNullCard(dc, m_x, m_y);
  814. }
  815. }
  816. void Discard::GetTopCardPos(int& x, int& y)
  817. {
  818. if (m_topCard < 0)
  819. {
  820. x = m_x;
  821. y = m_y;
  822. }
  823. else if (m_topCard > 31)
  824. {
  825. x = m_x + m_dx * (m_topCard - 32);
  826. y = m_y + CardHeight / 3;
  827. }
  828. else
  829. {
  830. x = m_x + m_dx * m_topCard;
  831. y = m_y;
  832. }
  833. }
  834. Card* Discard::RemoveTopCard(wxDC& dc, int m_xOffset, int m_yOffset)
  835. {
  836. Card* card;
  837. if (m_topCard <= 31)
  838. {
  839. card = Pile::RemoveTopCard(dc, m_xOffset, m_yOffset);
  840. }
  841. else
  842. {
  843. int topX, topY, x, y;
  844. GetTopCardPos(topX, topY);
  845. card = Pile::RemoveTopCard();
  846. card->Erase(dc, topX - m_xOffset, topY - m_yOffset);
  847. GetTopCardPos(x, y);
  848. dc.SetClippingRegion(topX - m_xOffset, topY - m_yOffset,
  849. CardWidth, CardHeight);
  850. for (int i = m_topCard - 31; i <= m_topCard - 31 + CardWidth / m_dx; i++)
  851. {
  852. m_cards[i]->Draw(dc, m_x - m_xOffset + i * m_dx, m_y - m_yOffset);
  853. }
  854. if (m_topCard > 31)
  855. {
  856. m_cards[m_topCard]->Draw(dc, topX - m_xOffset - m_dx, topY - m_yOffset);
  857. }
  858. dc.DestroyClippingRegion();
  859. }
  860. return card;
  861. }