]>
git.saurik.com Git - wxWidgets.git/blob - demos/forty/game.cpp
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 wxWindows 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
75 // Make sure we delete all objects created by the game object
82 for (i
= 0; i
< 8; i
++)
84 delete m_foundations
[i
];
86 for (i
= 0; i
< 10; i
++)
95 Set the score for a new player.
96 NB: call Deal() first if the new player is to start
99 void Game::NewPlayer(int wins
, int games
, int score
)
103 m_totalScore
= score
;
107 // Undo the last move
108 void Game::Undo(wxDC
& dc
)
113 Card
* card
= m_moves
[m_moveIndex
].dest
->RemoveTopCard(dc
);
114 m_moves
[m_moveIndex
].src
->AddCard(dc
, card
);
119 // Redo the last move
120 void Game::Redo(wxDC
& dc
)
122 if (m_moveIndex
< m_redoIndex
)
124 Card
* card
= m_moves
[m_moveIndex
].src
->RemoveTopCard(dc
);
125 if (m_moves
[m_moveIndex
].src
== m_pack
)
128 card
->TurnCard(faceup
);
130 m_moves
[m_moveIndex
].dest
->AddCard(dc
, card
);
136 void Game::DoMove(wxDC
& dc
, Pile
* src
, Pile
* dest
)
138 if (m_moveIndex
< MaxMoves
)
142 wxMessageBox("Game::DoMove() src == dest", "Debug message",
143 wxOK
| wxICON_EXCLAMATION
);
145 m_moves
[m_moveIndex
].src
= src
;
146 m_moves
[m_moveIndex
].dest
= dest
;
149 // when we do a move any moves in redo buffer are discarded
150 m_redoIndex
= m_moveIndex
;
154 wxMessageBox("Game::DoMove() Undo buffer full", "Debug message",
155 wxOK
| wxICON_EXCLAMATION
);
167 wxWindow
*frame
= wxTheApp
->GetTopWindow();
168 wxWindow
*canvas
= (wxWindow
*) NULL
;
172 wxNode
*node
= frame
->GetChildren().First();
173 if (node
) canvas
= (wxWindow
*)node
->Data();
179 // Redraw the score box to update games won
182 if (wxMessageBox("Do you wish to play again?",
183 "Well Done, You have won!", wxYES_NO
| wxICON_QUESTION
) == wxYES
)
190 // user cancelled the dialog - exit the app
191 ((wxFrame
*)canvas
->GetParent())->Close(TRUE
);
197 void Game::DisplayScore(wxDC
& dc
)
199 wxColour bgColour
= FortyApp::BackgroundColour();
200 wxPen
* pen
= wxThePenList
->FindOrCreatePen(bgColour
, 1, wxSOLID
);
201 dc
.SetTextBackground(bgColour
);
202 dc
.SetTextForeground(FortyApp::TextColour());
203 dc
.SetBrush(FortyApp::BackgroundBrush());
206 // count the number of cards in foundations
208 for (int i
= 0; i
< 8; i
++)
210 m_currentScore
+= m_foundations
[i
]->GetNumCards();
214 m_pack
->GetTopCardPos(x
, y
);
215 x
+= 12 * CardWidth
- 105;
220 dc
.GetTextExtent("Average score:m_x", &width
, &height
);
224 dc
.DrawRectangle(x
+ w
, y
, 20, 4 * h
);
227 sprintf(str
, "%d", m_currentScore
);
228 dc
.DrawText("Score:", x
, y
);
229 dc
.DrawText(str
, x
+ w
, y
);
232 sprintf(str
, "%d", m_numGames
);
233 dc
.DrawText("Games played:", x
, y
);
234 dc
.DrawText(str
, x
+ w
, y
);
237 sprintf(str
, "%d", m_numWins
);
238 dc
.DrawText("Games won:", x
, y
);
239 dc
.DrawText(str
, x
+ w
, y
);
245 average
= (2 * (m_currentScore
+ m_totalScore
) + m_numGames
) / (2 * m_numGames
);
247 sprintf(str
, "%d", average
);
248 dc
.DrawText("Average score:", x
, y
);
249 dc
.DrawText(str
, x
+ w
, y
);
253 // Shuffle the m_pack and deal the cards
259 // Reset all the piles, the undo buffer and shuffle the m_pack
262 for (i
= 0; i
< 5; i
++)
266 m_discard
->ResetPile();
267 for (i
= 0; i
< 10; i
++)
269 m_bases
[i
]->ResetPile();
271 for (i
= 0; i
< 8; i
++)
273 m_foundations
[i
]->ResetPile();
276 // Deal the initial 40 cards onto the bases
277 for (i
= 0; i
< 10; i
++)
279 for (j
= 1; j
<= 4; j
++)
281 card
= m_pack
->RemoveTopCard();
282 card
->TurnCard(faceup
);
283 m_bases
[i
]->AddCard(card
);
289 // player has started the game and then redealt
290 // and so we must add the score for this game to the total score
291 m_totalScore
+= m_currentScore
;
298 // Redraw the m_pack, discard pile, the bases and the foundations
299 void Game::Redraw(wxDC
& dc
)
303 m_discard
->Redraw(dc
);
304 for (i
= 0; i
< 8; i
++)
306 m_foundations
[i
]->Redraw(dc
);
308 for (i
= 0; i
< 10; i
++)
310 m_bases
[i
]->Redraw(dc
);
316 m_bmap
= new wxBitmap(CardWidth
, CardHeight
);
317 m_bmapCard
= new wxBitmap(CardWidth
, CardHeight
);
319 // Initialise the card bitmap to the background colour
321 memoryDC
.SelectObject(*m_bmapCard
);
322 memoryDC
.SetPen( *wxTRANSPARENT_PEN
);
323 memoryDC
.SetBrush(FortyApp::BackgroundBrush());
324 memoryDC
.DrawRectangle(0, 0, CardWidth
, CardHeight
);
325 memoryDC
.SelectObject(*m_bmap
);
326 memoryDC
.DrawRectangle(0, 0, CardWidth
, CardHeight
);
327 memoryDC
.SelectObject(wxNullBitmap
);
332 // Test to see if the point (x, y) is over the top card of one of the piles
333 // Returns pointer to the pile, or 0 if (x, y) is not over a pile
334 // or the pile is empty
335 Pile
* Game::WhichPile(int x
, int y
)
337 if (m_pack
->GetCard(x
, y
) &&
338 m_pack
->GetCard(x
, y
) == m_pack
->GetTopCard())
343 if (m_discard
->GetCard(x
, y
) &&
344 m_discard
->GetCard(x
, y
) == m_discard
->GetTopCard())
350 for (i
= 0; i
< 8; i
++)
352 if (m_foundations
[i
]->GetCard(x
, y
) &&
353 m_foundations
[i
]->GetCard(x
, y
) == m_foundations
[i
]->GetTopCard())
355 return m_foundations
[i
];
359 for (i
= 0; i
< 10; i
++)
361 if (m_bases
[i
]->GetCard(x
, y
) &&
362 m_bases
[i
]->GetCard(x
, y
) == m_bases
[i
]->GetTopCard())
371 // Left button is pressed - if cursor is over the m_pack then deal a card
372 // otherwise if it is over a card pick it up ready to be dragged - see MouseMove()
373 bool Game::LButtonDown(wxDC
& dc
, int x
, int y
)
375 m_srcPile
= WhichPile(x
, y
);
376 if (m_srcPile
== m_pack
)
378 Card
* card
= m_pack
->RemoveTopCard();
382 card
->TurnCard(faceup
);
383 m_discard
->AddCard(dc
, card
);
384 DoMove(dc
, m_pack
, m_discard
);
390 m_srcPile
->GetTopCardPos(m_xPos
, m_yPos
);
391 m_xOffset
= m_xPos
- x
;
392 m_yOffset
= m_yPos
- y
;
394 // Copy the area under the card
395 // Initialise the card bitmap to the background colour
398 memoryDC
.SelectObject(*m_bmap
);
399 m_liftedCard
= m_srcPile
->RemoveTopCard(memoryDC
, m_xPos
, m_yPos
);
402 // Draw the card in card bitmap ready for blitting onto
406 memoryDC
.SelectObject(*m_bmapCard
);
407 m_liftedCard
->Draw(memoryDC
, 0, 0);
410 return m_srcPile
!= 0;
413 // Called when the left button is double clicked
414 // If a card is under the pointer and it can move elsewhere then move it.
415 // Move onto a foundation as first choice, a populated base as second and
416 // an empty base as third choice.
417 // NB Cards in the m_pack cannot be moved in this way - they aren't in play
419 void Game::LButtonDblClk(wxDC
& dc
, int x
, int y
)
421 Pile
* pile
= WhichPile(x
, y
);
424 // Double click on m_pack is the same as left button down
427 LButtonDown(dc
, x
, y
);
431 Card
* card
= pile
->GetTopCard();
437 // if the card is an ace then try to place it next
438 // to an ace of the same suit
439 if (card
->GetPipValue() == 1)
441 for(i
= 0; i
< 4; i
++)
443 Card
* m_topCard
= m_foundations
[i
]->GetTopCard();
446 if (m_topCard
->GetSuit() == card
->GetSuit() &&
447 m_foundations
[i
+ 4] != pile
&&
448 m_foundations
[i
+ 4]->GetTopCard() == 0)
450 pile
->RemoveTopCard(dc
);
451 m_foundations
[i
+ 4]->AddCard(dc
, card
);
452 DoMove(dc
, pile
, m_foundations
[i
+ 4]);
459 // try to place the card on a foundation
460 for(i
= 0; i
< 8; i
++)
462 if (m_foundations
[i
]->AcceptCard(card
) && m_foundations
[i
] != pile
)
464 pile
->RemoveTopCard(dc
);
465 m_foundations
[i
]->AddCard(dc
, card
);
466 DoMove(dc
, pile
, m_foundations
[i
]);
470 // try to place the card on a populated base
471 for(i
= 0; i
< 10; i
++)
473 if (m_bases
[i
]->AcceptCard(card
) &&
474 m_bases
[i
] != pile
&&
475 m_bases
[i
]->GetTopCard())
477 pile
->RemoveTopCard(dc
);
478 m_bases
[i
]->AddCard(dc
, card
);
479 DoMove(dc
, pile
, m_bases
[i
]);
483 // try to place the card on any base
484 for(i
= 0; i
< 10; i
++)
486 if (m_bases
[i
]->AcceptCard(card
) && m_bases
[i
] != pile
)
488 pile
->RemoveTopCard(dc
);
489 m_bases
[i
]->AddCard(dc
, card
);
490 DoMove(dc
, pile
, m_bases
[i
]);
499 // Test to see whether the game has been won:
500 // i.e. m_pack, discard and bases are empty
501 bool Game::HaveYouWon()
503 if (m_pack
->GetTopCard()) return FALSE
;
504 if (m_discard
->GetTopCard()) return FALSE
;
505 for(int i
= 0; i
< 10; i
++)
507 if (m_bases
[i
]->GetTopCard()) return FALSE
;
510 m_totalScore
+= m_currentScore
;
516 // See whether the card under the cursor can be moved somewhere else
517 // Returns TRUE if it can be moved, FALSE otherwise
518 bool Game::CanYouGo(int x
, int y
)
520 Pile
* pile
= WhichPile(x
, y
);
521 if (pile
&& pile
!= m_pack
)
523 Card
* card
= pile
->GetTopCard();
528 for(i
= 0; i
< 8; i
++)
530 if (m_foundations
[i
]->AcceptCard(card
) && m_foundations
[i
] != pile
)
535 for(i
= 0; i
< 10; i
++)
537 if (m_bases
[i
]->GetTopCard() &&
538 m_bases
[i
]->AcceptCard(card
) &&
550 // Called when the left button is released after dragging a card
551 // Scan the piles to see if this card overlaps a pile and can be added
552 // to the pile. If the card overlaps more than one pile on which it can be placed
553 // then put it on the nearest pile.
554 void Game::LButtonUp(wxDC
& dc
, int x
, int y
)
558 // work out the position of the dragged card
562 Pile
* nearestPile
= 0;
563 int distance
= (CardHeight
+ CardWidth
) * (CardHeight
+ CardWidth
);
565 // find the nearest pile which will accept the card
567 for (i
= 0; i
< 8; i
++)
569 if (DropCard(x
, y
, m_foundations
[i
], m_liftedCard
))
571 if (m_foundations
[i
]->CalcDistance(x
, y
) < distance
)
573 nearestPile
= m_foundations
[i
];
574 distance
= nearestPile
->CalcDistance(x
, y
);
578 for (i
= 0; i
< 10; i
++)
580 if (DropCard(x
, y
, m_bases
[i
], m_liftedCard
))
582 if (m_bases
[i
]->CalcDistance(x
, y
) < distance
)
584 nearestPile
= m_bases
[i
];
585 distance
= nearestPile
->CalcDistance(x
, y
);
590 // Restore the area under the card
592 memoryDC
.SelectObject(*m_bmap
);
593 dc
.Blit(m_xPos
, m_yPos
, CardWidth
, CardHeight
,
594 &memoryDC
, 0, 0, wxCOPY
);
596 // Draw the card in its new position
600 nearestPile
->AddCard(dc
, m_liftedCard
);
601 if (nearestPile
!= m_srcPile
)
603 DoMove(dc
, m_srcPile
, nearestPile
);
608 // Return card to src pile
609 m_srcPile
->AddCard(dc
, m_liftedCard
);
619 bool Game::DropCard(int x
, int y
, Pile
* pile
, Card
* card
)
622 if (pile
->Overlap(x
, y
))
624 if (pile
->AcceptCard(card
))
633 void Game::MouseMove(wxDC
& dc
, int mx
, int my
)
638 memoryDC
.SelectObject(*m_bmap
);
640 int dx
= mx
+ m_xOffset
- m_xPos
;
641 int dy
= my
+ m_yOffset
- m_yPos
;
643 if (abs(dx
) >= CardWidth
|| abs(dy
) >= CardHeight
)
645 // Restore the area under the card
646 dc
.Blit(m_xPos
, m_yPos
, CardWidth
, CardHeight
,
647 &memoryDC
, 0, 0, wxCOPY
);
649 // Copy the area under the card in the new position
650 memoryDC
.Blit(0, 0, CardWidth
, CardHeight
,
651 &dc
, m_xPos
+ dx
, m_yPos
+ dy
, wxCOPY
);
656 dc
.Blit(m_xPos
, m_yPos
, dx
, CardHeight
, &memoryDC
, 0, 0, wxCOPY
);
660 dc
.Blit(m_xPos
+ dx
, m_yPos
, CardWidth
- dx
, dy
, &memoryDC
, dx
, 0, wxCOPY
);
661 memoryDC
.Blit(0, 0, CardWidth
- dx
, CardHeight
- dy
,
662 &memoryDC
, dx
, dy
, wxCOPY
);
663 memoryDC
.Blit(0, CardHeight
- dy
, CardWidth
- dx
, dy
,
664 &dc
, m_xPos
+ dx
, m_yPos
+ CardHeight
, wxCOPY
);
669 dc
.Blit(m_xPos
+ dx
, m_yPos
+ dy
+ CardHeight
, CardWidth
- dx
, -dy
,
670 &memoryDC
, dx
, CardHeight
+ dy
, wxCOPY
);
671 memoryDC
.Blit(0, -dy
, CardWidth
- dx
, CardHeight
+ dy
,
672 &memoryDC
, dx
, 0, wxCOPY
);
673 memoryDC
.Blit(0, 0, CardWidth
- dx
, -dy
,
674 &dc
, m_xPos
+ dx
, m_yPos
+ dy
, wxCOPY
);
676 memoryDC
.Blit(CardWidth
- dx
, 0, dx
, CardHeight
,
677 &dc
, m_xPos
+ CardWidth
, m_yPos
+ dy
, wxCOPY
);
682 dc
.Blit(m_xPos
+ CardWidth
+ dx
, m_yPos
, -dx
, CardHeight
,
683 &memoryDC
, CardWidth
+ dx
, 0, wxCOPY
);
686 dc
.Blit(m_xPos
, m_yPos
, CardWidth
+ dx
, dy
, &memoryDC
, 0, 0, wxCOPY
);
687 memoryDC
.Blit(-dx
, 0, CardWidth
+ dx
, CardHeight
- dy
,
688 &memoryDC
, 0, dy
, wxCOPY
);
689 memoryDC
.Blit(-dx
, CardHeight
- dy
, CardWidth
+ dx
, dy
,
690 &dc
, m_xPos
, m_yPos
+ CardHeight
, wxCOPY
);
695 dc
.Blit(m_xPos
, m_yPos
+ CardHeight
+ dy
, CardWidth
+ dx
, -dy
,
696 &memoryDC
, 0, CardHeight
+ dy
, wxCOPY
);
697 memoryDC
.Blit(-dx
, -dy
, CardWidth
+ dx
, CardHeight
+ dy
,
698 &memoryDC
, 0, 0, wxCOPY
);
699 memoryDC
.Blit(-dx
, 0, CardWidth
+ dx
, -dy
,
700 &dc
, m_xPos
, m_yPos
+ dy
, wxCOPY
);
702 memoryDC
.Blit(0, 0, -dx
, CardHeight
,
703 &dc
, m_xPos
+ dx
, m_yPos
+ dy
, wxCOPY
);
708 // draw the card in its new position
709 memoryDC
.SelectObject(*m_bmapCard
);
710 dc
.Blit(m_xPos
, m_yPos
, CardWidth
, CardHeight
,
711 &memoryDC
, 0, 0, wxCOPY
);
717 //----------------------------------------------//
718 // The Pack class: holds the two decks of cards //
719 //----------------------------------------------//
720 Pack::Pack(int x
, int y
) : Pile(x
, y
, 0, 0)
722 for (m_topCard
= 0; m_topCard
< NumCards
; m_topCard
++)
724 m_cards
[m_topCard
] = new Card(1 + m_topCard
/ 2, facedown
);
726 m_topCard
= NumCards
- 1;
732 Card
* temp
[NumCards
];
735 // Don't try to shuffle an empty m_pack!
736 if (m_topCard
< 0) return;
738 // Copy the cards into a temporary array. Start by clearing
739 // the array and then copy the card into a random position.
740 // If the position is occupied then find the next lower position.
741 for (i
= 0; i
<= m_topCard
; i
++)
745 for (i
= 0; i
<= m_topCard
; i
++)
747 int pos
= rand() % (m_topCard
+ 1);
751 if (pos
< 0) pos
= m_topCard
;
753 m_cards
[i
]->TurnCard(facedown
);
754 temp
[pos
] = m_cards
[i
];
758 // Copy each card back into the m_pack in a random
759 // position. If position is occupied then find nearest
760 // unoccupied position after the random position.
761 for (i
= 0; i
<= m_topCard
; i
++)
763 int pos
= rand() % (m_topCard
+ 1);
767 if (pos
> m_topCard
) pos
= 0;
769 m_cards
[pos
] = temp
[i
];
773 void Pack::Redraw(wxDC
& dc
)
778 sprintf(str
, "%d ", m_topCard
+ 1);
780 dc
.SetBackgroundMode( wxSOLID
);
781 dc
.SetTextBackground(FortyApp::BackgroundColour());
782 dc
.SetTextForeground(FortyApp::TextColour());
783 dc
.DrawText(str
, m_x
+ CardWidth
+ 5, m_y
+ CardHeight
/ 2);
787 void Pack::AddCard(Card
* card
)
789 if (card
== m_cards
[m_topCard
+ 1])
795 wxMessageBox("Pack::AddCard() Undo error", "Forty Thieves: Warning",
796 wxOK
| wxICON_EXCLAMATION
);
798 card
->TurnCard(facedown
);
804 for (m_topCard
= 0; m_topCard
< NumCards
; m_topCard
++)
806 delete m_cards
[m_topCard
];
811 //------------------------------------------------------//
812 // The Base class: holds the initial pile of four cards //
813 //------------------------------------------------------//
814 Base::Base(int x
, int y
) : Pile(x
, y
, 0, 12)
820 bool Base::AcceptCard(Card
* card
)
826 if (m_cards
[m_topCard
]->GetSuit() == card
->GetSuit() &&
827 m_cards
[m_topCard
]->GetPipValue() - 1 == card
->GetPipValue())
834 // pile is empty - ACCEPT
842 // nothing special at the moment
846 //----------------------------------------------------------------//
847 // The Foundation class: holds the cards built up from the ace... //
848 //----------------------------------------------------------------//
849 Foundation::Foundation(int x
, int y
) : Pile(x
, y
, 0, 0)
854 bool Foundation::AcceptCard(Card
* card
)
860 if (m_cards
[m_topCard
]->GetSuit() == card
->GetSuit() &&
861 m_cards
[m_topCard
]->GetPipValue() + 1 == card
->GetPipValue())
866 else if (card
->GetPipValue() == 1)
868 // It's an ace and the pile is empty - ACCEPT
874 Foundation::~Foundation()
876 // nothing special at the moment
880 //----------------------------------------------------//
881 // The Discard class: holds cards dealt from the m_pack //
882 //----------------------------------------------------//
883 Discard::Discard(int x
, int y
) : Pile(x
, y
, 19, 0)
888 void Discard::Redraw(wxDC
& dc
)
892 if (m_dx
== 0 && m_dy
== 0)
894 m_cards
[m_topCard
]->Draw(dc
, m_x
, m_y
);
900 for (int i
= 0; i
<= m_topCard
; i
++)
902 m_cards
[i
]->Draw(dc
, x
, y
);
908 y
= m_y
+ CardHeight
/ 3;
915 Card::DrawNullCard(dc
, m_x
, m_y
);
920 void Discard::GetTopCardPos(int& x
, int& y
)
927 else if (m_topCard
> 31)
929 x
= m_x
+ m_dx
* (m_topCard
- 32);
930 y
= m_y
+ CardHeight
/ 3;
934 x
= m_x
+ m_dx
* m_topCard
;
940 Card
* Discard::RemoveTopCard(wxDC
& dc
, int m_xOffset
, int m_yOffset
)
946 card
= Pile::RemoveTopCard(dc
, m_xOffset
, m_yOffset
);
950 int topX
, topY
, x
, y
;
951 GetTopCardPos(topX
, topY
);
952 card
= Pile::RemoveTopCard();
953 card
->Erase(dc
, topX
- m_xOffset
, topY
- m_yOffset
);
955 dc
.SetClippingRegion(topX
- m_xOffset
, topY
- m_yOffset
,
956 CardWidth
, CardHeight
);
958 for (int i
= m_topCard
- 31; i
<= m_topCard
- 31 + CardWidth
/ m_dx
; i
++)
960 m_cards
[i
]->Draw(dc
, m_x
- m_xOffset
+ i
* m_dx
, m_y
- m_yOffset
);
964 m_cards
[m_topCard
]->Draw(dc
, topX
- m_xOffset
- m_dx
, topY
- m_yOffset
);
966 dc
.DestroyClippingRegion();
975 // nothing special at the moment