]>
git.saurik.com Git - wxWidgets.git/blob - samples/forty/pile.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 /////////////////////////////////////////////////////////////////////////////
13 //+-------------------------------------------------------------+
15 //| The base class for holding piles of playing cards. |
16 //+-------------------------------------------------------------+
19 #pragma implementation
23 // For compilers that support precompilation, includes "wx/wx.h".
24 #include "wx/wxprec.h"
34 #pragma implementation
45 //+-------------------------------------------------------------+
47 //+-------------------------------------------------------------+
49 //| Initialise the pile to be empty of cards. |
50 //+-------------------------------------------------------------+
51 Pile::Pile(int x
, int y
, int dx
, int dy
)
57 for (m_topCard
= 0; m_topCard
< NumCards
; m_topCard
++)
59 m_cards
[m_topCard
] = 0;
61 m_topCard
= -1; // i.e. empty
65 //+-------------------------------------------------------------+
67 //+-------------------------------------------------------------+
69 //| Redraw the pile on the screen. If the pile is empty |
70 //| just draw a NULL card as a place holder for the pile. |
71 //| Otherwise draw the pile from the bottom up, starting |
72 //| at the origin of the pile, shifting each subsequent |
73 //| card by the pile's x and y offsets. |
74 //+-------------------------------------------------------------+
75 void Pile::Redraw(wxDC
& dc
)
79 if (m_dx
== 0 && m_dy
== 0)
81 m_cards
[m_topCard
]->Draw(dc
, m_x
, m_y
);
87 for (int i
= 0; i
<= m_topCard
; i
++)
89 m_cards
[i
]->Draw(dc
, x
, y
);
97 Card::DrawNullCard(dc
, m_x
, m_y
);
102 //+-------------------------------------------------------------+
103 //| Pile::GetTopCard() |
104 //+-------------------------------------------------------------+
106 //| Return a pointer to the top card in the pile or NULL |
107 //| if the pile is empty. |
108 //| NB: Gets a copy of the card without removing it from the |
110 //+-------------------------------------------------------------+
111 Card
* Pile::GetTopCard()
117 card
= m_cards
[m_topCard
];
123 //+-------------------------------------------------------------+
124 //| Pile::RemoveTopCard() |
125 //+-------------------------------------------------------------+
127 //| If the pile is not empty, remove the top card from the |
128 //| pile and return the pointer to the removed card. |
129 //| If the pile is empty return a NULL pointer. |
130 //+-------------------------------------------------------------+
131 Card
* Pile::RemoveTopCard()
137 card
= m_cards
[m_topCard
--];
143 //+-------------------------------------------------------------+
144 //| Pile::RemoveTopCard() |
145 //+-------------------------------------------------------------+
147 //| As RemoveTopCard() but also redraw the top of the pile |
148 //| after the card has been removed. |
149 //| NB: the offset allows for the redrawn area to be in a |
150 //| bitmap ready for 'dragging' cards acrosss the screen. |
151 //+-------------------------------------------------------------+
152 Card
* Pile::RemoveTopCard(wxDC
& dc
, int xOffset
, int yOffset
)
154 int topX
, topY
, x
, y
;
156 GetTopCardPos(topX
, topY
);
157 Card
* card
= RemoveTopCard();
161 card
->Erase(dc
, topX
- xOffset
, topY
- yOffset
);
165 Card::DrawNullCard(dc
, x
- xOffset
, y
- yOffset
);
169 m_cards
[m_topCard
]->Draw(dc
, x
- xOffset
, y
- yOffset
);
177 void Pile::GetTopCardPos(int& x
, int& y
)
186 x
= m_x
+ m_dx
* m_topCard
;
187 y
= m_y
+ m_dy
* m_topCard
;
191 void Pile::AddCard(Card
* card
)
193 if (m_topCard
< -1) m_topCard
= -1;
195 m_cards
[++m_topCard
] = card
;
198 void Pile::AddCard(wxDC
& dc
, Card
* card
)
203 card
->Draw(dc
, x
, y
);
206 // Can the card leave this pile.
207 // If it is a member of the pile then the answer is yes.
208 // Derived classes may override this behaviour to incorporate
209 // the rules of the game
210 bool Pile::CanCardLeave(Card
* card
)
212 for (int i
= 0; i
<= m_topCard
; i
++)
214 if (card
== m_cards
[i
]) return TRUE
;
219 // Calculate how far x, y is from top card in the pile
220 // Returns the square of the distance
221 int Pile::CalcDistance(int x
, int y
)
224 GetTopCardPos(cx
, cy
);
225 return ((cx
- x
) * (cx
- x
) + (cy
- y
) * (cy
- y
));
229 // Return the card at x, y. Check the top card first, then
230 // work down the pile. If a card is found then return a pointer
231 // to the card, otherwise return NULL
232 Card
* Pile::GetCard(int x
, int y
)
236 GetTopCardPos(cardX
, cardY
);
238 for (int i
= m_topCard
; i
>= 0; i
--)
240 if (x
>= cardX
&& x
<= cardX
+ CardWidth
&&
241 y
>= cardY
&& y
<= cardY
+ CardHeight
)
252 // Return the position of the given card. If it is not a member of this pile
253 // return the origin of the pile.
254 void Pile::GetCardPos(Card
* card
, int& x
, int& y
)
259 for (int i
= 0; i
<= m_topCard
; i
++)
261 if (card
== m_cards
[i
])
269 // card not found in pile, return origin of pile
275 bool Pile::Overlap(int x
, int y
)
279 GetTopCardPos(cardX
, cardY
);
281 if (x
>= cardX
- CardWidth
&& x
<= cardX
+ CardWidth
&&
282 y
>= cardY
- CardHeight
&& y
<= cardY
+ CardHeight
)
292 // nothing special at the moment