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 /////////////////////////////////////////////////////////////////////////////
15 #pragma implementation
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
37 Game::Game(int wins
, int games
, int score
) :
46 m_pack
= new Pack(2, 2 + 4 * (CardHeight
+ 2));
49 for (i
= 0; i
< 5; i
++) m_pack
->Shuffle();
51 m_discard
= new Discard(2, 2 + 5 * (CardHeight
+ 2));
53 for (i
= 0; i
< 8; i
++)
55 m_foundations
[i
] = new Foundation(2 + (i
/ 4) * (CardWidth
+ 2),
56 2 + (i
% 4) * (CardHeight
+ 2));
59 for (i
= 0; i
< 10; i
++)
61 m_bases
[i
] = new Base(8 + (i
+ 2) * (CardWidth
+ 2), 2);
67 // copy the input parameters for future reference
79 m_pack
->SetPos(2, 2 + 4 * (CardHeight
+ 2));
81 m_discard
->SetPos(2, 2 + 5 * (CardHeight
+ 2));
83 for (i
= 0; i
< 8; i
++)
85 m_foundations
[i
]->SetPos(2 + (i
/ 4) * (CardWidth
+ 2),
86 2 + (i
% 4) * (CardHeight
+ 2));
89 for (i
= 0; i
< 10; i
++)
91 m_bases
[i
]->SetPos(8 + (i
+ 2) * (CardWidth
+ 2), 2);
99 // Make sure we delete all objects created by the game object
106 for (i
= 0; i
< 8; i
++)
108 delete m_foundations
[i
];
110 for (i
= 0; i
< 10; i
++)
119 Set the score for a new player.
120 NB: call Deal() first if the new player is to start
123 void Game::NewPlayer(int wins
, int games
, int score
)
127 m_totalScore
= score
;
131 // Undo the last move
132 void Game::Undo(wxDC
& dc
)
137 Card
* card
= m_moves
[m_moveIndex
].dest
->RemoveTopCard(dc
);
138 m_moves
[m_moveIndex
].src
->AddCard(dc
, card
);
143 // Redo the last move
144 void Game::Redo(wxDC
& dc
)
146 if (m_moveIndex
< m_redoIndex
)
148 Card
* card
= m_moves
[m_moveIndex
].src
->RemoveTopCard(dc
);
149 if (m_moves
[m_moveIndex
].src
== m_pack
)
152 card
->TurnCard(faceup
);
154 m_moves
[m_moveIndex
].dest
->AddCard(dc
, card
);
160 void Game::DoMove(wxDC
& dc
, Pile
* src
, Pile
* dest
)
162 if (m_moveIndex
< MaxMoves
)
166 wxMessageBox(_T("Game::DoMove() src == dest"), _T("Debug message"),
167 wxOK
| wxICON_EXCLAMATION
);
169 m_moves
[m_moveIndex
].src
= src
;
170 m_moves
[m_moveIndex
].dest
= dest
;
173 // when we do a move any moves in redo buffer are discarded
174 m_redoIndex
= m_moveIndex
;
178 wxMessageBox(_T("Game::DoMove() Undo buffer full"), _T("Debug message"),
179 wxOK
| wxICON_EXCLAMATION
);
191 wxWindow
*frame
= wxTheApp
->GetTopWindow();
192 wxWindow
*canvas
= (wxWindow
*) NULL
;
196 wxWindowList::compatibility_iterator node
= frame
->GetChildren().GetFirst();
197 if (node
) canvas
= (wxWindow
*)node
->GetData();
203 // Redraw the score box to update games won
206 if (wxMessageBox(_T("Do you wish to play again?"),
207 _T("Well Done, You have won!"), wxYES_NO
| wxICON_QUESTION
) == wxYES
)
214 // user cancelled the dialog - exit the app
215 ((wxFrame
*)canvas
->GetParent())->Close(true);
221 void Game::DisplayScore(wxDC
& dc
)
223 wxColour bgColour
= FortyApp::BackgroundColour();
224 wxPen
* pen
= wxThePenList
->FindOrCreatePen(bgColour
, 1, wxSOLID
);
225 dc
.SetTextBackground(bgColour
);
226 dc
.SetTextForeground(FortyApp::TextColour());
227 dc
.SetBrush(FortyApp::BackgroundBrush());
230 // count the number of cards in foundations
232 for (int i
= 0; i
< 8; i
++)
234 m_currentScore
+= m_foundations
[i
]->GetNumCards();
238 m_pack
->GetTopCardPos(x
, y
);
239 x
+= 12 * CardWidth
- 105;
244 dc
.GetTextExtent(_T("Average score:m_x"), &width
, &height
);
248 dc
.DrawRectangle(x
+ w
, y
, 20, 4 * h
);
251 str
.Printf(_T("%d"), m_currentScore
);
252 dc
.DrawText(_T("Score:"), x
, y
);
253 dc
.DrawText(str
, x
+ w
, y
);
256 str
.Printf(_T("%d"), m_numGames
);
257 dc
.DrawText(_T("Games played:"), x
, y
);
258 dc
.DrawText(str
, x
+ w
, y
);
261 str
.Printf(_T("%d"), m_numWins
);
262 dc
.DrawText(_T("Games won:"), x
, y
);
263 dc
.DrawText(str
, x
+ w
, y
);
269 average
= (2 * (m_currentScore
+ m_totalScore
) + m_numGames
) / (2 * m_numGames
);
271 str
.Printf(_T("%d"), average
);
272 dc
.DrawText(_T("Average score:"), x
, y
);
273 dc
.DrawText(str
, x
+ w
, y
);
277 // Shuffle the m_pack and deal the cards
283 // Reset all the piles, the undo buffer and shuffle the m_pack
286 for (i
= 0; i
< 5; i
++)
290 m_discard
->ResetPile();
291 for (i
= 0; i
< 10; i
++)
293 m_bases
[i
]->ResetPile();
295 for (i
= 0; i
< 8; i
++)
297 m_foundations
[i
]->ResetPile();
300 // Deal the initial 40 cards onto the bases
301 for (i
= 0; i
< 10; i
++)
303 for (j
= 1; j
<= 4; j
++)
305 card
= m_pack
->RemoveTopCard();
306 card
->TurnCard(faceup
);
307 m_bases
[i
]->AddCard(card
);
313 // player has started the game and then redealt
314 // and so we must add the score for this game to the total score
315 m_totalScore
+= m_currentScore
;
322 // Redraw the m_pack, discard pile, the bases and the foundations
323 void Game::Redraw(wxDC
& dc
)
327 m_discard
->Redraw(dc
);
328 for (i
= 0; i
< 8; i
++)
330 m_foundations
[i
]->Redraw(dc
);
332 for (i
= 0; i
< 10; i
++)
334 m_bases
[i
]->Redraw(dc
);
340 m_bmap
= new wxBitmap(CardWidth
, CardHeight
);
341 m_bmapCard
= new wxBitmap(CardWidth
, CardHeight
);
343 // Initialise the card bitmap to the background colour
345 memoryDC
.SelectObject(*m_bmapCard
);
346 memoryDC
.SetPen( *wxTRANSPARENT_PEN
);
347 memoryDC
.SetBrush(FortyApp::BackgroundBrush());
348 memoryDC
.DrawRectangle(0, 0, CardWidth
, CardHeight
);
349 memoryDC
.SelectObject(*m_bmap
);
350 memoryDC
.DrawRectangle(0, 0, CardWidth
, CardHeight
);
351 memoryDC
.SelectObject(wxNullBitmap
);
356 // Test to see if the point (x, y) is over the top card of one of the piles
357 // Returns pointer to the pile, or 0 if (x, y) is not over a pile
358 // or the pile is empty
359 Pile
* Game::WhichPile(int x
, int y
)
361 if (m_pack
->GetCard(x
, y
) &&
362 m_pack
->GetCard(x
, y
) == m_pack
->GetTopCard())
367 if (m_discard
->GetCard(x
, y
) &&
368 m_discard
->GetCard(x
, y
) == m_discard
->GetTopCard())
374 for (i
= 0; i
< 8; i
++)
376 if (m_foundations
[i
]->GetCard(x
, y
) &&
377 m_foundations
[i
]->GetCard(x
, y
) == m_foundations
[i
]->GetTopCard())
379 return m_foundations
[i
];
383 for (i
= 0; i
< 10; i
++)
385 if (m_bases
[i
]->GetCard(x
, y
) &&
386 m_bases
[i
]->GetCard(x
, y
) == m_bases
[i
]->GetTopCard())
395 // Left button is pressed - if cursor is over the m_pack then deal a card
396 // otherwise if it is over a card pick it up ready to be dragged - see MouseMove()
397 bool Game::LButtonDown(wxDC
& dc
, int x
, int y
)
399 m_srcPile
= WhichPile(x
, y
);
400 if (m_srcPile
== m_pack
)
402 Card
* card
= m_pack
->RemoveTopCard();
406 card
->TurnCard(faceup
);
407 m_discard
->AddCard(dc
, card
);
408 DoMove(dc
, m_pack
, m_discard
);
414 m_srcPile
->GetTopCardPos(m_xPos
, m_yPos
);
415 m_xOffset
= m_xPos
- x
;
416 m_yOffset
= m_yPos
- y
;
418 // Copy the area under the card
419 // Initialise the card bitmap to the background colour
422 memoryDC
.SelectObject(*m_bmap
);
423 m_liftedCard
= m_srcPile
->RemoveTopCard(memoryDC
, m_xPos
, m_yPos
);
426 // Draw the card in card bitmap ready for blitting onto
430 memoryDC
.SelectObject(*m_bmapCard
);
431 m_liftedCard
->Draw(memoryDC
, 0, 0);
434 return m_srcPile
!= 0;
437 // Called when the left button is double clicked
438 // If a card is under the pointer and it can move elsewhere then move it.
439 // Move onto a foundation as first choice, a populated base as second and
440 // an empty base as third choice.
441 // NB Cards in the m_pack cannot be moved in this way - they aren't in play
443 void Game::LButtonDblClk(wxDC
& dc
, int x
, int y
)
445 Pile
* pile
= WhichPile(x
, y
);
448 // Double click on m_pack is the same as left button down
451 LButtonDown(dc
, x
, y
);
455 Card
* card
= pile
->GetTopCard();
461 // if the card is an ace then try to place it next
462 // to an ace of the same suit
463 if (card
->GetPipValue() == 1)
465 for(i
= 0; i
< 4; i
++)
467 Card
* m_topCard
= m_foundations
[i
]->GetTopCard();
470 if (m_topCard
->GetSuit() == card
->GetSuit() &&
471 m_foundations
[i
+ 4] != pile
&&
472 m_foundations
[i
+ 4]->GetTopCard() == 0)
474 pile
->RemoveTopCard(dc
);
475 m_foundations
[i
+ 4]->AddCard(dc
, card
);
476 DoMove(dc
, pile
, m_foundations
[i
+ 4]);
483 // try to place the card on a foundation
484 for(i
= 0; i
< 8; i
++)
486 if (m_foundations
[i
]->AcceptCard(card
) && m_foundations
[i
] != pile
)
488 pile
->RemoveTopCard(dc
);
489 m_foundations
[i
]->AddCard(dc
, card
);
490 DoMove(dc
, pile
, m_foundations
[i
]);
494 // try to place the card on a populated base
495 for(i
= 0; i
< 10; i
++)
497 if (m_bases
[i
]->AcceptCard(card
) &&
498 m_bases
[i
] != pile
&&
499 m_bases
[i
]->GetTopCard())
501 pile
->RemoveTopCard(dc
);
502 m_bases
[i
]->AddCard(dc
, card
);
503 DoMove(dc
, pile
, m_bases
[i
]);
507 // try to place the card on any base
508 for(i
= 0; i
< 10; i
++)
510 if (m_bases
[i
]->AcceptCard(card
) && m_bases
[i
] != pile
)
512 pile
->RemoveTopCard(dc
);
513 m_bases
[i
]->AddCard(dc
, card
);
514 DoMove(dc
, pile
, m_bases
[i
]);
523 // Test to see whether the game has been won:
524 // i.e. m_pack, discard and bases are empty
525 bool Game::HaveYouWon()
527 if (m_pack
->GetTopCard()) return false;
528 if (m_discard
->GetTopCard()) return false;
529 for(int i
= 0; i
< 10; i
++)
531 if (m_bases
[i
]->GetTopCard()) return false;
534 m_totalScore
+= m_currentScore
;
540 // See whether the card under the cursor can be moved somewhere else
541 // Returns 'true' if it can be moved, 'false' otherwise
542 bool Game::CanYouGo(int x
, int y
)
544 Pile
* pile
= WhichPile(x
, y
);
545 if (pile
&& pile
!= m_pack
)
547 Card
* card
= pile
->GetTopCard();
552 for(i
= 0; i
< 8; i
++)
554 if (m_foundations
[i
]->AcceptCard(card
) && m_foundations
[i
] != pile
)
559 for(i
= 0; i
< 10; i
++)
561 if (m_bases
[i
]->GetTopCard() &&
562 m_bases
[i
]->AcceptCard(card
) &&
574 // Called when the left button is released after dragging a card
575 // Scan the piles to see if this card overlaps a pile and can be added
576 // to the pile. If the card overlaps more than one pile on which it can be placed
577 // then put it on the nearest pile.
578 void Game::LButtonUp(wxDC
& dc
, int x
, int y
)
582 // work out the position of the dragged card
586 Pile
* nearestPile
= 0;
587 int distance
= (CardHeight
+ CardWidth
) * (CardHeight
+ CardWidth
);
589 // find the nearest pile which will accept the card
591 for (i
= 0; i
< 8; i
++)
593 if (DropCard(x
, y
, m_foundations
[i
], m_liftedCard
))
595 if (m_foundations
[i
]->CalcDistance(x
, y
) < distance
)
597 nearestPile
= m_foundations
[i
];
598 distance
= nearestPile
->CalcDistance(x
, y
);
602 for (i
= 0; i
< 10; i
++)
604 if (DropCard(x
, y
, m_bases
[i
], m_liftedCard
))
606 if (m_bases
[i
]->CalcDistance(x
, y
) < distance
)
608 nearestPile
= m_bases
[i
];
609 distance
= nearestPile
->CalcDistance(x
, y
);
614 // Restore the area under the card
616 memoryDC
.SelectObject(*m_bmap
);
617 dc
.Blit(m_xPos
, m_yPos
, CardWidth
, CardHeight
,
618 &memoryDC
, 0, 0, wxCOPY
);
620 // Draw the card in its new position
624 nearestPile
->AddCard(dc
, m_liftedCard
);
625 if (nearestPile
!= m_srcPile
)
627 DoMove(dc
, m_srcPile
, nearestPile
);
632 // Return card to src pile
633 m_srcPile
->AddCard(dc
, m_liftedCard
);
643 bool Game::DropCard(int x
, int y
, Pile
* pile
, Card
* card
)
646 if (pile
->Overlap(x
, y
))
648 if (pile
->AcceptCard(card
))
657 void Game::MouseMove(wxDC
& dc
, int mx
, int my
)
662 memoryDC
.SelectObject(*m_bmap
);
664 int dx
= mx
+ m_xOffset
- m_xPos
;
665 int dy
= my
+ m_yOffset
- m_yPos
;
667 if (abs(dx
) >= CardWidth
|| abs(dy
) >= CardHeight
)
669 // Restore the area under the card
670 dc
.Blit(m_xPos
, m_yPos
, CardWidth
, CardHeight
,
671 &memoryDC
, 0, 0, wxCOPY
);
673 // Copy the area under the card in the new position
674 memoryDC
.Blit(0, 0, CardWidth
, CardHeight
,
675 &dc
, m_xPos
+ dx
, m_yPos
+ dy
, wxCOPY
);
680 dc
.Blit(m_xPos
, m_yPos
, dx
, CardHeight
, &memoryDC
, 0, 0, wxCOPY
);
684 dc
.Blit(m_xPos
+ dx
, m_yPos
, CardWidth
- dx
, dy
, &memoryDC
, dx
, 0, wxCOPY
);
685 memoryDC
.Blit(0, 0, CardWidth
- dx
, CardHeight
- dy
,
686 &memoryDC
, dx
, dy
, wxCOPY
);
687 memoryDC
.Blit(0, CardHeight
- dy
, CardWidth
- dx
, dy
,
688 &dc
, m_xPos
+ dx
, m_yPos
+ CardHeight
, wxCOPY
);
693 dc
.Blit(m_xPos
+ dx
, m_yPos
+ dy
+ CardHeight
, CardWidth
- dx
, -dy
,
694 &memoryDC
, dx
, CardHeight
+ dy
, wxCOPY
);
695 memoryDC
.Blit(0, -dy
, CardWidth
- dx
, CardHeight
+ dy
,
696 &memoryDC
, dx
, 0, wxCOPY
);
697 memoryDC
.Blit(0, 0, CardWidth
- dx
, -dy
,
698 &dc
, m_xPos
+ dx
, m_yPos
+ dy
, wxCOPY
);
700 memoryDC
.Blit(CardWidth
- dx
, 0, dx
, CardHeight
,
701 &dc
, m_xPos
+ CardWidth
, m_yPos
+ dy
, wxCOPY
);
706 dc
.Blit(m_xPos
+ CardWidth
+ dx
, m_yPos
, -dx
, CardHeight
,
707 &memoryDC
, CardWidth
+ dx
, 0, wxCOPY
);
710 dc
.Blit(m_xPos
, m_yPos
, CardWidth
+ dx
, dy
, &memoryDC
, 0, 0, wxCOPY
);
711 memoryDC
.Blit(-dx
, 0, CardWidth
+ dx
, CardHeight
- dy
,
712 &memoryDC
, 0, dy
, wxCOPY
);
713 memoryDC
.Blit(-dx
, CardHeight
- dy
, CardWidth
+ dx
, dy
,
714 &dc
, m_xPos
, m_yPos
+ CardHeight
, wxCOPY
);
719 dc
.Blit(m_xPos
, m_yPos
+ CardHeight
+ dy
, CardWidth
+ dx
, -dy
,
720 &memoryDC
, 0, CardHeight
+ dy
, wxCOPY
);
721 memoryDC
.Blit(-dx
, -dy
, CardWidth
+ dx
, CardHeight
+ dy
,
722 &memoryDC
, 0, 0, wxCOPY
);
723 memoryDC
.Blit(-dx
, 0, CardWidth
+ dx
, -dy
,
724 &dc
, m_xPos
, m_yPos
+ dy
, wxCOPY
);
726 memoryDC
.Blit(0, 0, -dx
, CardHeight
,
727 &dc
, m_xPos
+ dx
, m_yPos
+ dy
, wxCOPY
);
732 // draw the card in its new position
733 memoryDC
.SelectObject(*m_bmapCard
);
734 dc
.Blit(m_xPos
, m_yPos
, CardWidth
, CardHeight
,
735 &memoryDC
, 0, 0, wxCOPY
);
741 //----------------------------------------------//
742 // The Pack class: holds the two decks of cards //
743 //----------------------------------------------//
744 Pack::Pack(int x
, int y
) : Pile(x
, y
, 0, 0)
746 for (m_topCard
= 0; m_topCard
< NumCards
; m_topCard
++)
748 m_cards
[m_topCard
] = new Card(1 + m_topCard
/ 2, facedown
);
750 m_topCard
= NumCards
- 1;
756 Card
* temp
[NumCards
];
759 // Don't try to shuffle an empty m_pack!
760 if (m_topCard
< 0) return;
762 // Copy the cards into a temporary array. Start by clearing
763 // the array and then copy the card into a random position.
764 // If the position is occupied then find the next lower position.
765 for (i
= 0; i
<= m_topCard
; i
++)
769 for (i
= 0; i
<= m_topCard
; i
++)
771 int pos
= rand() % (m_topCard
+ 1);
775 if (pos
< 0) pos
= m_topCard
;
777 m_cards
[i
]->TurnCard(facedown
);
778 temp
[pos
] = m_cards
[i
];
782 // Copy each card back into the m_pack in a random
783 // position. If position is occupied then find nearest
784 // unoccupied position after the random position.
785 for (i
= 0; i
<= m_topCard
; i
++)
787 int pos
= rand() % (m_topCard
+ 1);
791 if (pos
> m_topCard
) pos
= 0;
793 m_cards
[pos
] = temp
[i
];
797 void Pack::Redraw(wxDC
& dc
)
802 str
.Printf(_T("%d "), m_topCard
+ 1);
804 dc
.SetBackgroundMode( wxSOLID
);
805 dc
.SetTextBackground(FortyApp::BackgroundColour());
806 dc
.SetTextForeground(FortyApp::TextColour());
807 dc
.DrawText(str
, m_x
+ CardWidth
+ 5, m_y
+ CardHeight
/ 2);
811 void Pack::AddCard(Card
* card
)
813 if (card
== m_cards
[m_topCard
+ 1])
819 wxMessageBox(_T("Pack::AddCard() Undo error"), _T("Forty Thieves: Warning"),
820 wxOK
| wxICON_EXCLAMATION
);
822 card
->TurnCard(facedown
);
828 for (m_topCard
= 0; m_topCard
< NumCards
; m_topCard
++)
830 delete m_cards
[m_topCard
];
835 //------------------------------------------------------//
836 // The Base class: holds the initial pile of four cards //
837 //------------------------------------------------------//
838 Base::Base(int x
, int y
) : Pile(x
, y
, 0, 12)
844 bool Base::AcceptCard(Card
* card
)
850 if (m_cards
[m_topCard
]->GetSuit() == card
->GetSuit() &&
851 m_cards
[m_topCard
]->GetPipValue() - 1 == card
->GetPipValue())
858 // pile is empty - ACCEPT
866 // nothing special at the moment
870 //----------------------------------------------------------------//
871 // The Foundation class: holds the cards built up from the ace... //
872 //----------------------------------------------------------------//
873 Foundation::Foundation(int x
, int y
) : Pile(x
, y
, 0, 0)
878 bool Foundation::AcceptCard(Card
* card
)
884 if (m_cards
[m_topCard
]->GetSuit() == card
->GetSuit() &&
885 m_cards
[m_topCard
]->GetPipValue() + 1 == card
->GetPipValue())
890 else if (card
->GetPipValue() == 1)
892 // It's an ace and the pile is empty - ACCEPT
898 Foundation::~Foundation()
900 // nothing special at the moment
904 //----------------------------------------------------//
905 // The Discard class: holds cards dealt from the m_pack //
906 //----------------------------------------------------//
907 Discard::Discard(int x
, int y
) : Pile(x
, y
, 19, 0)
912 void Discard::Redraw(wxDC
& dc
)
916 if (m_dx
== 0 && m_dy
== 0)
918 m_cards
[m_topCard
]->Draw(dc
, m_x
, m_y
);
924 for (int i
= 0; i
<= m_topCard
; i
++)
926 m_cards
[i
]->Draw(dc
, x
, y
);
932 y
= m_y
+ CardHeight
/ 3;
939 Card::DrawNullCard(dc
, m_x
, m_y
);
944 void Discard::GetTopCardPos(int& x
, int& y
)
951 else if (m_topCard
> 31)
953 x
= m_x
+ m_dx
* (m_topCard
- 32);
954 y
= m_y
+ CardHeight
/ 3;
958 x
= m_x
+ m_dx
* m_topCard
;
964 Card
* Discard::RemoveTopCard(wxDC
& dc
, int m_xOffset
, int m_yOffset
)
970 card
= Pile::RemoveTopCard(dc
, m_xOffset
, m_yOffset
);
974 int topX
, topY
, x
, y
;
975 GetTopCardPos(topX
, topY
);
976 card
= Pile::RemoveTopCard();
977 card
->Erase(dc
, topX
- m_xOffset
, topY
- m_yOffset
);
979 dc
.SetClippingRegion(topX
- m_xOffset
, topY
- m_yOffset
,
980 CardWidth
, CardHeight
);
982 for (int i
= m_topCard
- 31; i
<= m_topCard
- 31 + CardWidth
/ m_dx
; i
++)
984 m_cards
[i
]->Draw(dc
, m_x
- m_xOffset
+ i
* m_dx
, m_y
- m_yOffset
);
988 m_cards
[m_topCard
]->Draw(dc
, topX
- m_xOffset
- m_dx
, topY
- m_yOffset
);
990 dc
.DestroyClippingRegion();
999 // nothing special at the moment