1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Forty Thieves patience game
4 // Author: Chris Breeze
8 // Copyright: (c) 1993-1998 Chris Breeze
9 // Licence: wxWindows licence
10 //---------------------------------------------------------------------------
11 // Last modified: 22nd July 1998 - ported to wxWidgets 2.0
12 /////////////////////////////////////////////////////////////////////////////
14 // For compilers that support precompilation, includes "wx/wx.h".
15 #include "wx/wxprec.h"
32 Game::Game(int wins
, int games
, int score
) :
41 m_pack
= new Pack(2, 2 + 4 * (CardHeight
+ 2));
44 for (i
= 0; i
< 5; i
++) m_pack
->Shuffle();
46 m_discard
= new Discard(2, 2 + 5 * (CardHeight
+ 2));
48 for (i
= 0; i
< 8; i
++)
50 m_foundations
[i
] = new Foundation(2 + (i
/ 4) * (CardWidth
+ 2),
51 2 + (i
% 4) * (CardHeight
+ 2));
54 for (i
= 0; i
< 10; i
++)
56 m_bases
[i
] = new Base(8 + (i
+ 2) * (CardWidth
+ 2), 2);
62 // copy the input parameters for future reference
74 m_pack
->SetPos(2, 2 + 4 * (CardHeight
+ 2));
76 m_discard
->SetPos(2, 2 + 5 * (CardHeight
+ 2));
78 for (i
= 0; i
< 8; i
++)
80 m_foundations
[i
]->SetPos(2 + (i
/ 4) * (CardWidth
+ 2),
81 2 + (i
% 4) * (CardHeight
+ 2));
84 for (i
= 0; i
< 10; i
++)
86 m_bases
[i
]->SetPos(8 + (i
+ 2) * (CardWidth
+ 2), 2);
94 // Make sure we delete all objects created by the game object
101 for (i
= 0; i
< 8; i
++)
103 delete m_foundations
[i
];
105 for (i
= 0; i
< 10; i
++)
114 Set the score for a new player.
115 NB: call Deal() first if the new player is to start
118 void Game::NewPlayer(int wins
, int games
, int score
)
122 m_totalScore
= score
;
126 // Undo the last move
127 void Game::Undo(wxDC
& dc
)
132 Card
* card
= m_moves
[m_moveIndex
].dest
->RemoveTopCard(dc
);
133 m_moves
[m_moveIndex
].src
->AddCard(dc
, card
);
138 // Redo the last move
139 void Game::Redo(wxDC
& dc
)
141 if (m_moveIndex
< m_redoIndex
)
143 Card
* card
= m_moves
[m_moveIndex
].src
->RemoveTopCard(dc
);
144 if (m_moves
[m_moveIndex
].src
== m_pack
)
147 card
->TurnCard(faceup
);
149 m_moves
[m_moveIndex
].dest
->AddCard(dc
, card
);
155 void Game::DoMove(wxDC
& dc
, Pile
* src
, Pile
* dest
)
157 if (m_moveIndex
< MaxMoves
)
161 wxMessageBox(_T("Game::DoMove() src == dest"), _T("Debug message"),
162 wxOK
| wxICON_EXCLAMATION
);
164 m_moves
[m_moveIndex
].src
= src
;
165 m_moves
[m_moveIndex
].dest
= dest
;
168 // when we do a move any moves in redo buffer are discarded
169 m_redoIndex
= m_moveIndex
;
173 wxMessageBox(_T("Game::DoMove() Undo buffer full"), _T("Debug message"),
174 wxOK
| wxICON_EXCLAMATION
);
186 wxWindow
*frame
= wxTheApp
->GetTopWindow();
187 wxWindow
*canvas
= (wxWindow
*) NULL
;
191 wxWindowList::compatibility_iterator node
= frame
->GetChildren().GetFirst();
192 if (node
) canvas
= (wxWindow
*)node
->GetData();
198 // Redraw the score box to update games won
201 if (wxMessageBox(_T("Do you wish to play again?"),
202 _T("Well Done, You have won!"), wxYES_NO
| wxICON_QUESTION
) == wxYES
)
209 // user cancelled the dialog - exit the app
210 ((wxFrame
*)canvas
->GetParent())->Close(true);
216 void Game::DisplayScore(wxDC
& dc
)
218 wxColour bgColour
= FortyApp::BackgroundColour();
219 wxPen
* pen
= wxThePenList
->FindOrCreatePen(bgColour
, 1, wxSOLID
);
220 dc
.SetTextBackground(bgColour
);
221 dc
.SetTextForeground(FortyApp::TextColour());
222 dc
.SetBrush(FortyApp::BackgroundBrush());
225 // count the number of cards in foundations
227 for (int i
= 0; i
< 8; i
++)
229 m_currentScore
+= m_foundations
[i
]->GetNumCards();
233 m_pack
->GetTopCardPos(x
, y
);
234 x
+= 12 * CardWidth
- 105;
238 wxCoord width
, height
;
239 dc
.GetTextExtent(_T("Average score:m_x"), &width
, &height
);
243 dc
.DrawRectangle(x
+ w
, y
, 20, 4 * h
);
246 str
.Printf(_T("%d"), m_currentScore
);
247 dc
.DrawText(_T("Score:"), x
, y
);
248 dc
.DrawText(str
, x
+ w
, y
);
251 str
.Printf(_T("%d"), m_numGames
);
252 dc
.DrawText(_T("Games played:"), x
, y
);
253 dc
.DrawText(str
, x
+ w
, y
);
256 str
.Printf(_T("%d"), m_numWins
);
257 dc
.DrawText(_T("Games won:"), x
, y
);
258 dc
.DrawText(str
, x
+ w
, y
);
264 average
= (2 * (m_currentScore
+ m_totalScore
) + m_numGames
) / (2 * m_numGames
);
266 str
.Printf(_T("%d"), average
);
267 dc
.DrawText(_T("Average score:"), x
, y
);
268 dc
.DrawText(str
, x
+ w
, y
);
272 // Shuffle the m_pack and deal the cards
278 // Reset all the piles, the undo buffer and shuffle the m_pack
281 for (i
= 0; i
< 5; i
++)
285 m_discard
->ResetPile();
286 for (i
= 0; i
< 10; i
++)
288 m_bases
[i
]->ResetPile();
290 for (i
= 0; i
< 8; i
++)
292 m_foundations
[i
]->ResetPile();
295 // Deal the initial 40 cards onto the bases
296 for (i
= 0; i
< 10; i
++)
298 for (j
= 1; j
<= 4; j
++)
300 card
= m_pack
->RemoveTopCard();
301 card
->TurnCard(faceup
);
302 m_bases
[i
]->AddCard(card
);
308 // player has started the game and then redealt
309 // and so we must add the score for this game to the total score
310 m_totalScore
+= m_currentScore
;
317 // Redraw the m_pack, discard pile, the bases and the foundations
318 void Game::Redraw(wxDC
& dc
)
322 m_discard
->Redraw(dc
);
323 for (i
= 0; i
< 8; i
++)
325 m_foundations
[i
]->Redraw(dc
);
327 for (i
= 0; i
< 10; i
++)
329 m_bases
[i
]->Redraw(dc
);
335 m_bmap
= new wxBitmap(CardWidth
, CardHeight
);
336 m_bmapCard
= new wxBitmap(CardWidth
, CardHeight
);
338 // Initialise the card bitmap to the background colour
340 memoryDC
.SelectObject(*m_bmapCard
);
341 memoryDC
.SetPen( *wxTRANSPARENT_PEN
);
342 memoryDC
.SetBrush(FortyApp::BackgroundBrush());
343 memoryDC
.DrawRectangle(0, 0, CardWidth
, CardHeight
);
344 memoryDC
.SelectObject(*m_bmap
);
345 memoryDC
.DrawRectangle(0, 0, CardWidth
, CardHeight
);
346 memoryDC
.SelectObject(wxNullBitmap
);
351 // Test to see if the point (x, y) is over the top card of one of the piles
352 // Returns pointer to the pile, or 0 if (x, y) is not over a pile
353 // or the pile is empty
354 Pile
* Game::WhichPile(int x
, int y
)
356 if (m_pack
->GetCard(x
, y
) &&
357 m_pack
->GetCard(x
, y
) == m_pack
->GetTopCard())
362 if (m_discard
->GetCard(x
, y
) &&
363 m_discard
->GetCard(x
, y
) == m_discard
->GetTopCard())
369 for (i
= 0; i
< 8; i
++)
371 if (m_foundations
[i
]->GetCard(x
, y
) &&
372 m_foundations
[i
]->GetCard(x
, y
) == m_foundations
[i
]->GetTopCard())
374 return m_foundations
[i
];
378 for (i
= 0; i
< 10; i
++)
380 if (m_bases
[i
]->GetCard(x
, y
) &&
381 m_bases
[i
]->GetCard(x
, y
) == m_bases
[i
]->GetTopCard())
390 // Left button is pressed - if cursor is over the m_pack then deal a card
391 // otherwise if it is over a card pick it up ready to be dragged - see MouseMove()
392 bool Game::LButtonDown(wxDC
& dc
, int x
, int y
)
394 m_srcPile
= WhichPile(x
, y
);
395 if (m_srcPile
== m_pack
)
397 Card
* card
= m_pack
->RemoveTopCard();
401 card
->TurnCard(faceup
);
402 m_discard
->AddCard(dc
, card
);
403 DoMove(dc
, m_pack
, m_discard
);
409 m_srcPile
->GetTopCardPos(m_xPos
, m_yPos
);
410 m_xOffset
= m_xPos
- x
;
411 m_yOffset
= m_yPos
- y
;
413 // Copy the area under the card
414 // Initialise the card bitmap to the background colour
417 memoryDC
.SelectObject(*m_bmap
);
418 m_liftedCard
= m_srcPile
->RemoveTopCard(memoryDC
, m_xPos
, m_yPos
);
421 // Draw the card in card bitmap ready for blitting onto
425 memoryDC
.SelectObject(*m_bmapCard
);
426 m_liftedCard
->Draw(memoryDC
, 0, 0);
429 return m_srcPile
!= 0;
432 // Called when the left button is double clicked
433 // If a card is under the pointer and it can move elsewhere then move it.
434 // Move onto a foundation as first choice, a populated base as second and
435 // an empty base as third choice.
436 // NB Cards in the m_pack cannot be moved in this way - they aren't in play
438 void Game::LButtonDblClk(wxDC
& dc
, int x
, int y
)
440 Pile
* pile
= WhichPile(x
, y
);
443 // Double click on m_pack is the same as left button down
446 LButtonDown(dc
, x
, y
);
450 Card
* card
= pile
->GetTopCard();
456 // if the card is an ace then try to place it next
457 // to an ace of the same suit
458 if (card
->GetPipValue() == 1)
460 for(i
= 0; i
< 4; i
++)
462 Card
* m_topCard
= m_foundations
[i
]->GetTopCard();
465 if (m_topCard
->GetSuit() == card
->GetSuit() &&
466 m_foundations
[i
+ 4] != pile
&&
467 m_foundations
[i
+ 4]->GetTopCard() == 0)
469 pile
->RemoveTopCard(dc
);
470 m_foundations
[i
+ 4]->AddCard(dc
, card
);
471 DoMove(dc
, pile
, m_foundations
[i
+ 4]);
478 // try to place the card on a foundation
479 for(i
= 0; i
< 8; i
++)
481 if (m_foundations
[i
]->AcceptCard(card
) && m_foundations
[i
] != pile
)
483 pile
->RemoveTopCard(dc
);
484 m_foundations
[i
]->AddCard(dc
, card
);
485 DoMove(dc
, pile
, m_foundations
[i
]);
489 // try to place the card on a populated base
490 for(i
= 0; i
< 10; i
++)
492 if (m_bases
[i
]->AcceptCard(card
) &&
493 m_bases
[i
] != pile
&&
494 m_bases
[i
]->GetTopCard())
496 pile
->RemoveTopCard(dc
);
497 m_bases
[i
]->AddCard(dc
, card
);
498 DoMove(dc
, pile
, m_bases
[i
]);
502 // try to place the card on any base
503 for(i
= 0; i
< 10; i
++)
505 if (m_bases
[i
]->AcceptCard(card
) && m_bases
[i
] != pile
)
507 pile
->RemoveTopCard(dc
);
508 m_bases
[i
]->AddCard(dc
, card
);
509 DoMove(dc
, pile
, m_bases
[i
]);
518 // Test to see whether the game has been won:
519 // i.e. m_pack, discard and bases are empty
520 bool Game::HaveYouWon()
522 if (m_pack
->GetTopCard()) return false;
523 if (m_discard
->GetTopCard()) return false;
524 for(int i
= 0; i
< 10; i
++)
526 if (m_bases
[i
]->GetTopCard()) return false;
529 m_totalScore
+= m_currentScore
;
535 // See whether the card under the cursor can be moved somewhere else
536 // Returns 'true' if it can be moved, 'false' otherwise
537 bool Game::CanYouGo(int x
, int y
)
539 Pile
* pile
= WhichPile(x
, y
);
540 if (pile
&& pile
!= m_pack
)
542 Card
* card
= pile
->GetTopCard();
547 for(i
= 0; i
< 8; i
++)
549 if (m_foundations
[i
]->AcceptCard(card
) && m_foundations
[i
] != pile
)
554 for(i
= 0; i
< 10; i
++)
556 if (m_bases
[i
]->GetTopCard() &&
557 m_bases
[i
]->AcceptCard(card
) &&
569 // Called when the left button is released after dragging a card
570 // Scan the piles to see if this card overlaps a pile and can be added
571 // to the pile. If the card overlaps more than one pile on which it can be placed
572 // then put it on the nearest pile.
573 void Game::LButtonUp(wxDC
& dc
, int x
, int y
)
577 // work out the position of the dragged card
581 Pile
* nearestPile
= 0;
582 int distance
= (CardHeight
+ CardWidth
) * (CardHeight
+ CardWidth
);
584 // find the nearest pile which will accept the card
586 for (i
= 0; i
< 8; i
++)
588 if (DropCard(x
, y
, m_foundations
[i
], m_liftedCard
))
590 if (m_foundations
[i
]->CalcDistance(x
, y
) < distance
)
592 nearestPile
= m_foundations
[i
];
593 distance
= nearestPile
->CalcDistance(x
, y
);
597 for (i
= 0; i
< 10; i
++)
599 if (DropCard(x
, y
, m_bases
[i
], m_liftedCard
))
601 if (m_bases
[i
]->CalcDistance(x
, y
) < distance
)
603 nearestPile
= m_bases
[i
];
604 distance
= nearestPile
->CalcDistance(x
, y
);
609 // Restore the area under the card
611 memoryDC
.SelectObject(*m_bmap
);
612 dc
.Blit(m_xPos
, m_yPos
, CardWidth
, CardHeight
,
613 &memoryDC
, 0, 0, wxCOPY
);
615 // Draw the card in its new position
619 nearestPile
->AddCard(dc
, m_liftedCard
);
620 if (nearestPile
!= m_srcPile
)
622 DoMove(dc
, m_srcPile
, nearestPile
);
627 // Return card to src pile
628 m_srcPile
->AddCard(dc
, m_liftedCard
);
638 bool Game::DropCard(int x
, int y
, Pile
* pile
, Card
* card
)
641 if (pile
->Overlap(x
, y
))
643 if (pile
->AcceptCard(card
))
652 void Game::MouseMove(wxDC
& dc
, int mx
, int my
)
657 memoryDC
.SelectObject(*m_bmap
);
659 int dx
= mx
+ m_xOffset
- m_xPos
;
660 int dy
= my
+ m_yOffset
- m_yPos
;
662 if (abs(dx
) >= CardWidth
|| abs(dy
) >= CardHeight
)
664 // Restore the area under the card
665 dc
.Blit(m_xPos
, m_yPos
, CardWidth
, CardHeight
,
666 &memoryDC
, 0, 0, wxCOPY
);
668 // Copy the area under the card in the new position
669 memoryDC
.Blit(0, 0, CardWidth
, CardHeight
,
670 &dc
, m_xPos
+ dx
, m_yPos
+ dy
, wxCOPY
);
675 dc
.Blit(m_xPos
, m_yPos
, dx
, CardHeight
, &memoryDC
, 0, 0, wxCOPY
);
679 dc
.Blit(m_xPos
+ dx
, m_yPos
, CardWidth
- dx
, dy
, &memoryDC
, dx
, 0, wxCOPY
);
680 memoryDC
.Blit(0, 0, CardWidth
- dx
, CardHeight
- dy
,
681 &memoryDC
, dx
, dy
, wxCOPY
);
682 memoryDC
.Blit(0, CardHeight
- dy
, CardWidth
- dx
, dy
,
683 &dc
, m_xPos
+ dx
, m_yPos
+ CardHeight
, wxCOPY
);
688 dc
.Blit(m_xPos
+ dx
, m_yPos
+ dy
+ CardHeight
, CardWidth
- dx
, -dy
,
689 &memoryDC
, dx
, CardHeight
+ dy
, wxCOPY
);
690 memoryDC
.Blit(0, -dy
, CardWidth
- dx
, CardHeight
+ dy
,
691 &memoryDC
, dx
, 0, wxCOPY
);
692 memoryDC
.Blit(0, 0, CardWidth
- dx
, -dy
,
693 &dc
, m_xPos
+ dx
, m_yPos
+ dy
, wxCOPY
);
695 memoryDC
.Blit(CardWidth
- dx
, 0, dx
, CardHeight
,
696 &dc
, m_xPos
+ CardWidth
, m_yPos
+ dy
, wxCOPY
);
701 dc
.Blit(m_xPos
+ CardWidth
+ dx
, m_yPos
, -dx
, CardHeight
,
702 &memoryDC
, CardWidth
+ dx
, 0, wxCOPY
);
705 dc
.Blit(m_xPos
, m_yPos
, CardWidth
+ dx
, dy
, &memoryDC
, 0, 0, wxCOPY
);
706 memoryDC
.Blit(-dx
, 0, CardWidth
+ dx
, CardHeight
- dy
,
707 &memoryDC
, 0, dy
, wxCOPY
);
708 memoryDC
.Blit(-dx
, CardHeight
- dy
, CardWidth
+ dx
, dy
,
709 &dc
, m_xPos
, m_yPos
+ CardHeight
, wxCOPY
);
714 dc
.Blit(m_xPos
, m_yPos
+ CardHeight
+ dy
, CardWidth
+ dx
, -dy
,
715 &memoryDC
, 0, CardHeight
+ dy
, wxCOPY
);
716 memoryDC
.Blit(-dx
, -dy
, CardWidth
+ dx
, CardHeight
+ dy
,
717 &memoryDC
, 0, 0, wxCOPY
);
718 memoryDC
.Blit(-dx
, 0, CardWidth
+ dx
, -dy
,
719 &dc
, m_xPos
, m_yPos
+ dy
, wxCOPY
);
721 memoryDC
.Blit(0, 0, -dx
, CardHeight
,
722 &dc
, m_xPos
+ dx
, m_yPos
+ dy
, wxCOPY
);
727 // draw the card in its new position
728 memoryDC
.SelectObject(*m_bmapCard
);
729 dc
.Blit(m_xPos
, m_yPos
, CardWidth
, CardHeight
,
730 &memoryDC
, 0, 0, wxCOPY
);
736 //----------------------------------------------//
737 // The Pack class: holds the two decks of cards //
738 //----------------------------------------------//
739 Pack::Pack(int x
, int y
) : Pile(x
, y
, 0, 0)
741 for (m_topCard
= 0; m_topCard
< NumCards
; m_topCard
++)
743 m_cards
[m_topCard
] = new Card(1 + m_topCard
/ 2, facedown
);
745 m_topCard
= NumCards
- 1;
751 Card
* temp
[NumCards
];
754 // Don't try to shuffle an empty m_pack!
755 if (m_topCard
< 0) return;
757 // Copy the cards into a temporary array. Start by clearing
758 // the array and then copy the card into a random position.
759 // If the position is occupied then find the next lower position.
760 for (i
= 0; i
<= m_topCard
; i
++)
764 for (i
= 0; i
<= m_topCard
; i
++)
766 int pos
= rand() % (m_topCard
+ 1);
770 if (pos
< 0) pos
= m_topCard
;
772 m_cards
[i
]->TurnCard(facedown
);
773 temp
[pos
] = m_cards
[i
];
777 // Copy each card back into the m_pack in a random
778 // position. If position is occupied then find nearest
779 // unoccupied position after the random position.
780 for (i
= 0; i
<= m_topCard
; i
++)
782 int pos
= rand() % (m_topCard
+ 1);
786 if (pos
> m_topCard
) pos
= 0;
788 m_cards
[pos
] = temp
[i
];
792 void Pack::Redraw(wxDC
& dc
)
797 str
.Printf(_T("%d "), m_topCard
+ 1);
799 dc
.SetBackgroundMode( wxSOLID
);
800 dc
.SetTextBackground(FortyApp::BackgroundColour());
801 dc
.SetTextForeground(FortyApp::TextColour());
802 dc
.DrawText(str
, m_x
+ CardWidth
+ 5, m_y
+ CardHeight
/ 2);
806 void Pack::AddCard(Card
* card
)
808 if (card
== m_cards
[m_topCard
+ 1])
814 wxMessageBox(_T("Pack::AddCard() Undo error"), _T("Forty Thieves: Warning"),
815 wxOK
| wxICON_EXCLAMATION
);
817 card
->TurnCard(facedown
);
823 for (m_topCard
= 0; m_topCard
< NumCards
; m_topCard
++)
825 delete m_cards
[m_topCard
];
830 //------------------------------------------------------//
831 // The Base class: holds the initial pile of four cards //
832 //------------------------------------------------------//
833 Base::Base(int x
, int y
) : Pile(x
, y
, 0, 12)
839 bool Base::AcceptCard(Card
* card
)
845 if (m_cards
[m_topCard
]->GetSuit() == card
->GetSuit() &&
846 m_cards
[m_topCard
]->GetPipValue() - 1 == card
->GetPipValue())
853 // pile is empty - ACCEPT
860 //----------------------------------------------------------------//
861 // The Foundation class: holds the cards built up from the ace... //
862 //----------------------------------------------------------------//
863 Foundation::Foundation(int x
, int y
) : Pile(x
, y
, 0, 0)
868 bool Foundation::AcceptCard(Card
* card
)
874 if (m_cards
[m_topCard
]->GetSuit() == card
->GetSuit() &&
875 m_cards
[m_topCard
]->GetPipValue() + 1 == card
->GetPipValue())
880 else if (card
->GetPipValue() == 1)
882 // It's an ace and the pile is empty - ACCEPT
889 //----------------------------------------------------//
890 // The Discard class: holds cards dealt from the m_pack //
891 //----------------------------------------------------//
892 Discard::Discard(int x
, int y
) : Pile(x
, y
, 19, 0)
897 void Discard::Redraw(wxDC
& dc
)
901 if (m_dx
== 0 && m_dy
== 0)
903 m_cards
[m_topCard
]->Draw(dc
, m_x
, m_y
);
909 for (int i
= 0; i
<= m_topCard
; i
++)
911 m_cards
[i
]->Draw(dc
, x
, y
);
917 y
= m_y
+ CardHeight
/ 3;
924 Card::DrawNullCard(dc
, m_x
, m_y
);
929 void Discard::GetTopCardPos(int& x
, int& y
)
936 else if (m_topCard
> 31)
938 x
= m_x
+ m_dx
* (m_topCard
- 32);
939 y
= m_y
+ CardHeight
/ 3;
943 x
= m_x
+ m_dx
* m_topCard
;
949 Card
* Discard::RemoveTopCard(wxDC
& dc
, int m_xOffset
, int m_yOffset
)
955 card
= Pile::RemoveTopCard(dc
, m_xOffset
, m_yOffset
);
959 int topX
, topY
, x
, y
;
960 GetTopCardPos(topX
, topY
);
961 card
= Pile::RemoveTopCard();
962 card
->Erase(dc
, topX
- m_xOffset
, topY
- m_yOffset
);
964 dc
.SetClippingRegion(topX
- m_xOffset
, topY
- m_yOffset
,
965 CardWidth
, CardHeight
);
967 for (int i
= m_topCard
- 31; i
<= m_topCard
- 31 + CardWidth
/ m_dx
; i
++)
969 m_cards
[i
]->Draw(dc
, m_x
- m_xOffset
+ i
* m_dx
, m_y
- m_yOffset
);
973 m_cards
[m_topCard
]->Draw(dc
, topX
- m_xOffset
- m_dx
, topY
- m_yOffset
);
975 dc
.DestroyClippingRegion();