]>
git.saurik.com Git - wxWidgets.git/blob - samples/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 void Game::DisplayScore(wxDC
& dc
)
169 wxColour
* bgColour
= FortyApp::BackgroundColour();
170 wxPen
* pen
= wxThePenList
->FindOrCreatePen(*bgColour
, 1, wxSOLID
);
171 dc
.SetTextBackground(*bgColour
);
172 dc
.SetTextForeground(*FortyApp::TextColour());
173 dc
.SetBrush(FortyApp::BackgroundBrush());
176 // count the number of cards in foundations
178 for (int i
= 0; i
< 8; i
++)
180 m_currentScore
+= m_foundations
[i
]->GetNumCards();
184 m_pack
->GetTopCardPos(x
, y
);
185 x
+= 12 * CardWidth
- 105;
190 dc
.GetTextExtent("Average score:m_x", &width
, &height
);
194 dc
.DrawRectangle(x
+ w
, y
, 20, 4 * h
);
197 sprintf(str
, "%d", m_currentScore
);
198 dc
.DrawText("Score:", x
, y
);
199 dc
.DrawText(str
, x
+ w
, y
);
202 sprintf(str
, "%d", m_numGames
);
203 dc
.DrawText("Games played:", x
, y
);
204 dc
.DrawText(str
, x
+ w
, y
);
207 sprintf(str
, "%d", m_numWins
);
208 dc
.DrawText("Games won:", x
, y
);
209 dc
.DrawText(str
, x
+ w
, y
);
215 average
= (2 * (m_currentScore
+ m_totalScore
) + m_numGames
) / (2 * m_numGames
);
217 sprintf(str
, "%d", average
);
218 dc
.DrawText("Average score:", x
, y
);
219 dc
.DrawText(str
, x
+ w
, y
);
223 // Shuffle the m_pack and deal the cards
229 // Reset all the piles, the undo buffer and shuffle the m_pack
232 for (i
= 0; i
< 5; i
++)
236 m_discard
->ResetPile();
237 for (i
= 0; i
< 10; i
++)
239 m_bases
[i
]->ResetPile();
241 for (i
= 0; i
< 8; i
++)
243 m_foundations
[i
]->ResetPile();
246 // Deal the initial 40 cards onto the bases
247 for (i
= 0; i
< 10; i
++)
249 for (j
= 1; j
<= 4; j
++)
251 card
= m_pack
->RemoveTopCard();
252 card
->TurnCard(faceup
);
253 m_bases
[i
]->AddCard(card
);
259 // player has started the game and then redealt
260 // and so we must add the score for this game to the total score
261 m_totalScore
+= m_currentScore
;
268 // Redraw the m_pack, discard pile, the bases and the foundations
269 void Game::Redraw(wxDC
& dc
)
273 m_discard
->Redraw(dc
);
274 for (i
= 0; i
< 8; i
++)
276 m_foundations
[i
]->Redraw(dc
);
278 for (i
= 0; i
< 10; i
++)
280 m_bases
[i
]->Redraw(dc
);
286 m_bmap
= new wxBitmap(CardWidth
, CardHeight
);
287 m_bmapCard
= new wxBitmap(CardWidth
, CardHeight
);
289 // Initialise the card bitmap to the background colour
291 memoryDC
.SelectObject(m_bmapCard
);
292 memoryDC
.SetBrush(FortyApp::BackgroundBrush());
293 memoryDC
.DrawRectangle(0, 0, CardWidth
, CardHeight
);
294 memoryDC
.SelectObject(m_bmap
);
295 memoryDC
.DrawRectangle(0, 0, CardWidth
, CardHeight
);
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
)
305 if (m_pack
->GetCard(x
, y
) &&
306 m_pack
->GetCard(x
, y
) == m_pack
->GetTopCard())
311 if (m_discard
->GetCard(x
, y
) &&
312 m_discard
->GetCard(x
, y
) == m_discard
->GetTopCard())
318 for (i
= 0; i
< 8; i
++)
320 if (m_foundations
[i
]->GetCard(x
, y
) &&
321 m_foundations
[i
]->GetCard(x
, y
) == m_foundations
[i
]->GetTopCard())
323 return m_foundations
[i
];
327 for (i
= 0; i
< 10; i
++)
329 if (m_bases
[i
]->GetCard(x
, y
) &&
330 m_bases
[i
]->GetCard(x
, y
) == m_bases
[i
]->GetTopCard())
339 // Left button is pressed - if cursor is over the m_pack then deal a card
340 // otherwise if it is over a card pick it up ready to be dragged - see MouseMove()
341 bool Game::LButtonDown(wxDC
& dc
, int x
, int y
)
343 m_srcPile
= WhichPile(x
, y
);
344 if (m_srcPile
== m_pack
)
346 Card
* card
= m_pack
->RemoveTopCard();
350 card
->TurnCard(faceup
);
351 m_discard
->AddCard(dc
, card
);
352 DoMove(dc
, m_pack
, m_discard
);
358 m_srcPile
->GetTopCardPos(m_xPos
, m_yPos
);
359 m_xOffset
= m_xPos
- x
;
360 m_yOffset
= m_yPos
- y
;
362 // Copy the area under the card
363 // Initialise the card bitmap to the background colour
366 memoryDC
.SelectObject(m_bmap
);
367 m_liftedCard
= m_srcPile
->RemoveTopCard(memoryDC
, m_xPos
, m_yPos
);
370 // Draw the card in card bitmap ready for blitting onto
374 memoryDC
.SelectObject(m_bmapCard
);
375 m_liftedCard
->Draw(memoryDC
, 0, 0);
378 return m_srcPile
!= 0;
381 // Called when the left button is double clicked
382 // If a card is under the pointer and it can move elsewhere then move it.
383 // Move onto a foundation as first choice, a populated base as second and
384 // an empty base as third choice.
385 // NB Cards in the m_pack cannot be moved in this way - they aren't in play
387 void Game::LButtonDblClk(wxDC
& dc
, int x
, int y
)
389 Pile
* pile
= WhichPile(x
, y
);
392 // Double click on m_pack is the same as left button down
395 LButtonDown(dc
, x
, y
);
399 Card
* card
= pile
->GetTopCard();
405 // if the card is an ace then try to place it next
406 // to an ace of the same suit
407 if (card
->GetPipValue() == 1)
409 for(i
= 0; i
< 4; i
++)
412 if ((m_topCard
= m_foundations
[i
]->GetTopCard()))
414 if (m_topCard
->GetSuit() == card
->GetSuit() &&
415 m_foundations
[i
+ 4] != pile
&&
416 m_foundations
[i
+ 4]->GetTopCard() == 0)
418 pile
->RemoveTopCard(dc
);
419 m_foundations
[i
+ 4]->AddCard(dc
, card
);
420 DoMove(dc
, pile
, m_foundations
[i
+ 4]);
427 // try to place the card on a foundation
428 for(i
= 0; i
< 8; i
++)
430 if (m_foundations
[i
]->AcceptCard(card
) && m_foundations
[i
] != pile
)
432 pile
->RemoveTopCard(dc
);
433 m_foundations
[i
]->AddCard(dc
, card
);
434 DoMove(dc
, pile
, m_foundations
[i
]);
438 // try to place the card on a populated base
439 for(i
= 0; i
< 10; i
++)
441 if (m_bases
[i
]->AcceptCard(card
) &&
442 m_bases
[i
] != pile
&&
443 m_bases
[i
]->GetTopCard())
445 pile
->RemoveTopCard(dc
);
446 m_bases
[i
]->AddCard(dc
, card
);
447 DoMove(dc
, pile
, m_bases
[i
]);
451 // try to place the card on any base
452 for(i
= 0; i
< 10; i
++)
454 if (m_bases
[i
]->AcceptCard(card
) && m_bases
[i
] != pile
)
456 pile
->RemoveTopCard(dc
);
457 m_bases
[i
]->AddCard(dc
, card
);
458 DoMove(dc
, pile
, m_bases
[i
]);
467 // Test to see whether the game has been won:
468 // i.e. m_pack, discard and bases are empty
469 bool Game::HaveYouWon()
471 if (m_pack
->GetTopCard()) return FALSE
;
472 if (m_discard
->GetTopCard()) return FALSE
;
473 for(int i
= 0; i
< 10; i
++)
475 if (m_bases
[i
]->GetTopCard()) return FALSE
;
478 m_totalScore
+= m_currentScore
;
484 // See whether the card under the cursor can be moved somewhere else
485 // Returns TRUE if it can be moved, FALSE otherwise
486 bool Game::CanYouGo(int x
, int y
)
488 Pile
* pile
= WhichPile(x
, y
);
489 if (pile
&& pile
!= m_pack
)
491 Card
* card
= pile
->GetTopCard();
496 for(i
= 0; i
< 8; i
++)
498 if (m_foundations
[i
]->AcceptCard(card
) && m_foundations
[i
] != pile
)
503 for(i
= 0; i
< 10; i
++)
505 if (m_bases
[i
]->GetTopCard() &&
506 m_bases
[i
]->AcceptCard(card
) &&
518 // Called when the left button is released after dragging a card
519 // Scan the piles to see if this card overlaps a pile and can be added
520 // to the pile. If the card overlaps more than one pile on which it can be placed
521 // then put it on the nearest pile.
522 void Game::LButtonUp(wxDC
& dc
, int x
, int y
)
526 // work out the position of the dragged card
530 Pile
* nearestPile
= 0;
531 int distance
= (CardHeight
+ CardWidth
) * (CardHeight
+ CardWidth
);
533 // find the nearest pile which will accept the card
535 for (i
= 0; i
< 8; i
++)
537 if (DropCard(x
, y
, m_foundations
[i
], m_liftedCard
))
539 if (m_foundations
[i
]->CalcDistance(x
, y
) < distance
)
541 nearestPile
= m_foundations
[i
];
542 distance
= nearestPile
->CalcDistance(x
, y
);
546 for (i
= 0; i
< 10; i
++)
548 if (DropCard(x
, y
, m_bases
[i
], m_liftedCard
))
550 if (m_bases
[i
]->CalcDistance(x
, y
) < distance
)
552 nearestPile
= m_bases
[i
];
553 distance
= nearestPile
->CalcDistance(x
, y
);
558 // Restore the area under the card
560 memoryDC
.SelectObject(m_bmap
);
561 dc
.Blit(m_xPos
, m_yPos
, CardWidth
, CardHeight
,
562 &memoryDC
, 0, 0, wxCOPY
);
564 // Draw the card in its new position
568 nearestPile
->AddCard(dc
, m_liftedCard
);
569 if (nearestPile
!= m_srcPile
)
571 DoMove(dc
, m_srcPile
, nearestPile
);
576 // Return card to src pile
577 m_srcPile
->AddCard(dc
, m_liftedCard
);
587 bool Game::DropCard(int x
, int y
, Pile
* pile
, Card
* card
)
590 if (pile
->Overlap(x
, y
))
592 if (pile
->AcceptCard(card
))
601 void Game::MouseMove(wxDC
& dc
, int mx
, int my
)
606 memoryDC
.SelectObject(m_bmap
);
608 int dx
= mx
+ m_xOffset
- m_xPos
;
609 int dy
= my
+ m_yOffset
- m_yPos
;
611 if (abs(dx
) >= CardWidth
|| abs(dy
) >= CardHeight
)
613 // Restore the area under the card
614 dc
.Blit(m_xPos
, m_yPos
, CardWidth
, CardHeight
,
615 &memoryDC
, 0, 0, wxCOPY
);
617 // Copy the area under the card in the new position
618 memoryDC
.Blit(0, 0, CardWidth
, CardHeight
,
619 &dc
, m_xPos
+ dx
, m_yPos
+ dy
, wxCOPY
);
624 dc
.Blit(m_xPos
, m_yPos
, dx
, CardHeight
, &memoryDC
, 0, 0, wxCOPY
);
628 dc
.Blit(m_xPos
+ dx
, m_yPos
, CardWidth
- dx
, dy
, &memoryDC
, dx
, 0, wxCOPY
);
629 memoryDC
.Blit(0, 0, CardWidth
- dx
, CardHeight
- dy
,
630 &memoryDC
, dx
, dy
, wxCOPY
);
631 memoryDC
.Blit(0, CardHeight
- dy
, CardWidth
- dx
, dy
,
632 &dc
, m_xPos
+ dx
, m_yPos
+ CardHeight
, wxCOPY
);
637 dc
.Blit(m_xPos
+ dx
, m_yPos
+ dy
+ CardHeight
, CardWidth
- dx
, -dy
,
638 &memoryDC
, dx
, CardHeight
+ dy
, wxCOPY
);
639 memoryDC
.Blit(0, -dy
, CardWidth
- dx
, CardHeight
+ dy
,
640 &memoryDC
, dx
, 0, wxCOPY
);
641 memoryDC
.Blit(0, 0, CardWidth
- dx
, -dy
,
642 &dc
, m_xPos
+ dx
, m_yPos
+ dy
, wxCOPY
);
644 memoryDC
.Blit(CardWidth
- dx
, 0, dx
, CardHeight
,
645 &dc
, m_xPos
+ CardWidth
, m_yPos
+ dy
, wxCOPY
);
650 dc
.Blit(m_xPos
+ CardWidth
+ dx
, m_yPos
, -dx
, CardHeight
,
651 &memoryDC
, CardWidth
+ dx
, 0, wxCOPY
);
654 dc
.Blit(m_xPos
, m_yPos
, CardWidth
+ dx
, dy
, &memoryDC
, 0, 0, wxCOPY
);
655 memoryDC
.Blit(-dx
, 0, CardWidth
+ dx
, CardHeight
- dy
,
656 &memoryDC
, 0, dy
, wxCOPY
);
657 memoryDC
.Blit(-dx
, CardHeight
- dy
, CardWidth
+ dx
, dy
,
658 &dc
, m_xPos
, m_yPos
+ CardHeight
, wxCOPY
);
663 dc
.Blit(m_xPos
, m_yPos
+ CardHeight
+ dy
, CardWidth
+ dx
, -dy
,
664 &memoryDC
, 0, CardHeight
+ dy
, wxCOPY
);
665 memoryDC
.Blit(-dx
, -dy
, CardWidth
+ dx
, CardHeight
+ dy
,
666 &memoryDC
, 0, 0, wxCOPY
);
667 memoryDC
.Blit(-dx
, 0, CardWidth
+ dx
, -dy
,
668 &dc
, m_xPos
, m_yPos
+ dy
, wxCOPY
);
670 memoryDC
.Blit(0, 0, -dx
, CardHeight
,
671 &dc
, m_xPos
+ dx
, m_yPos
+ dy
, wxCOPY
);
676 // draw the card in its new position
677 memoryDC
.SelectObject(m_bmapCard
);
678 dc
.Blit(m_xPos
, m_yPos
, CardWidth
, CardHeight
,
679 &memoryDC
, 0, 0, wxCOPY
);
685 //----------------------------------------------//
686 // The Pack class: holds the two decks of cards //
687 //----------------------------------------------//
688 Pack::Pack(int x
, int y
) : Pile(x
, y
, 0, 0)
690 for (m_topCard
= 0; m_topCard
< NumCards
; m_topCard
++)
692 m_cards
[m_topCard
] = new Card(1 + m_topCard
/ 2, facedown
);
694 m_topCard
= NumCards
- 1;
700 Card
* temp
[NumCards
];
703 // Don't try to shuffle an empty m_pack!
704 if (m_topCard
< 0) return;
706 // Copy the cards into a temporary array. Start by clearing
707 // the array and then copy the card into a random position.
708 // If the position is occupied then find the next lower position.
709 for (i
= 0; i
<= m_topCard
; i
++)
713 for (i
= 0; i
<= m_topCard
; i
++)
715 int pos
= rand() % (m_topCard
+ 1);
719 if (pos
< 0) pos
= m_topCard
;
721 m_cards
[i
]->TurnCard(facedown
);
722 temp
[pos
] = m_cards
[i
];
726 // Copy each card back into the m_pack in a random
727 // position. If position is occupied then find nearest
728 // unoccupied position after the random position.
729 for (i
= 0; i
<= m_topCard
; i
++)
731 int pos
= rand() % (m_topCard
+ 1);
735 if (pos
> m_topCard
) pos
= 0;
737 m_cards
[pos
] = temp
[i
];
741 void Pack::Redraw(wxDC
& dc
)
746 sprintf(str
, "%d ", m_topCard
+ 1);
748 dc
.SetTextBackground(*FortyApp::BackgroundColour());
749 dc
.SetTextForeground(*FortyApp::TextColour());
750 dc
.DrawText(str
, m_x
+ CardWidth
+ 5, m_y
+ CardHeight
/ 2);
754 void Pack::AddCard(Card
* card
)
756 if (card
== m_cards
[m_topCard
+ 1])
762 wxMessageBox("Pack::AddCard() Undo error", "Forty Thieves: Warning",
763 wxOK
| wxICON_EXCLAMATION
);
765 card
->TurnCard(facedown
);
771 for (m_topCard
= 0; m_topCard
< NumCards
; m_topCard
++)
773 delete m_cards
[m_topCard
];
778 //------------------------------------------------------//
779 // The Base class: holds the initial pile of four cards //
780 //------------------------------------------------------//
781 Base::Base(int x
, int y
) : Pile(x
, y
, 0, 12)
787 bool Base::AcceptCard(Card
* card
)
793 if (m_cards
[m_topCard
]->GetSuit() == card
->GetSuit() &&
794 m_cards
[m_topCard
]->GetPipValue() - 1 == card
->GetPipValue())
801 // pile is empty - ACCEPT
809 // nothing special at the moment
813 //----------------------------------------------------------------//
814 // The Foundation class: holds the cards built up from the ace... //
815 //----------------------------------------------------------------//
816 Foundation::Foundation(int x
, int y
) : Pile(x
, y
, 0, 0)
821 bool Foundation::AcceptCard(Card
* card
)
827 if (m_cards
[m_topCard
]->GetSuit() == card
->GetSuit() &&
828 m_cards
[m_topCard
]->GetPipValue() + 1 == card
->GetPipValue())
833 else if (card
->GetPipValue() == 1)
835 // It's an ace and the pile is empty - ACCEPT
841 Foundation::~Foundation()
843 // nothing special at the moment
847 //----------------------------------------------------//
848 // The Discard class: holds cards dealt from the m_pack //
849 //----------------------------------------------------//
850 Discard::Discard(int x
, int y
) : Pile(x
, y
, 19, 0)
855 void Discard::Redraw(wxDC
& dc
)
859 if (m_dx
== 0 && m_dy
== 0)
861 m_cards
[m_topCard
]->Draw(dc
, m_x
, m_y
);
867 for (int i
= 0; i
<= m_topCard
; i
++)
869 m_cards
[i
]->Draw(dc
, x
, y
);
875 y
= m_y
+ CardHeight
/ 3;
882 Card::DrawNullCard(dc
, m_x
, m_y
);
887 void Discard::GetTopCardPos(int& x
, int& y
)
894 else if (m_topCard
> 31)
896 x
= m_x
+ m_dx
* (m_topCard
- 32);
897 y
= m_y
+ CardHeight
/ 3;
901 x
= m_x
+ m_dx
* m_topCard
;
907 Card
* Discard::RemoveTopCard(wxDC
& dc
, int m_xOffset
, int m_yOffset
)
913 card
= Pile::RemoveTopCard(dc
, m_xOffset
, m_yOffset
);
917 int topX
, topY
, x
, y
;
918 GetTopCardPos(topX
, topY
);
919 card
= Pile::RemoveTopCard();
920 card
->Erase(dc
, topX
- m_xOffset
, topY
- m_yOffset
);
922 dc
.SetClippingRegion(topX
- m_xOffset
, topY
- m_yOffset
,
923 CardWidth
, CardHeight
);
925 for (int i
= m_topCard
- 31; i
<= m_topCard
- 31 + CardWidth
/ m_dx
; i
++)
927 m_cards
[i
]->Draw(dc
, m_x
- m_xOffset
+ i
* m_dx
, m_y
- m_yOffset
);
931 m_cards
[m_topCard
]->Draw(dc
, topX
- m_xOffset
- m_dx
, topY
- m_yOffset
);
933 dc
.DestroyClippingRegion();
942 // nothing special at the moment