6 #include "cardlib/cardlib.h"
7 //#include "../catch22lib/trace.h"
13 #define TRACE(s) printf("%s(%i): %s",__FILE__,__LINE__,s)
17 bool fGameStarted
= false;
21 TRACE("ENTER NewGame()\n");
26 //create a new card-stack
32 //deal to each row stack..
33 for(i
= 0; i
< NUM_ROW_STACKS
; i
++)
38 pRowStack
[i
]->SetFaceDirection(CS_FACE_DOWNUP
, i
);
40 for(j
= 0; j
<= i
; j
++)
42 temp
.Push(deck
.Pop());
45 pRowStack
[i
]->SetCardStack(temp
);
48 //put the other cards onto the deck
49 pDeck
->SetCardStack(deck
);
55 TRACE("EXIT NewGame()\n");
59 // Now follow the stack callback functions. This is where we
60 // provide the game functionality and rules
64 // Can only drag face-up cards
66 bool CARDLIBPROC
RowStackDragProc(CardRegion
&stackobj
, int iNumDragCards
)
68 TRACE("ENTER RowStackDragProc()\n");
72 stackobj
.GetFaceDirection(&numfacedown
);
74 numcards
= stackobj
.NumCards();
76 TRACE("EXIT RowStackDragProc()\n");
77 if(iNumDragCards
<= numcards
-numfacedown
)
85 // Row a row-stack, we can only drop cards
86 // that are lower / different colour
88 bool CARDLIBPROC
RowStackDropProc(CardRegion
&stackobj
, const CardStack
&dragcards
)
90 TRACE("ENTER RowStackDropProc()\n");
91 Card dragcard
= dragcards
[dragcards
.NumCards() - 1];
93 //if we are empty, can only drop a stack with a King at bottom
94 if(stackobj
.NumCards() == 0)
96 if(dragcard
.LoVal() != 13)
98 TRACE("EXIT RowStackDropProc(false)\n");
104 const CardStack
&mystack
= stackobj
.GetCardStack();
106 //can only drop if card is 1 less
107 if(mystack
[0].LoVal() != dragcard
.LoVal() + 1)
109 TRACE("EXIT RowStackDropProc(false)\n");
113 //can only drop if card is different colour
114 if( mystack
[0].IsBlack() && !dragcard
.IsRed() ||
115 !mystack
[0].IsBlack() && dragcard
.IsRed() )
117 TRACE("EXIT RowStackDropProc(false)\n");
122 TRACE("EXIT RowStackDropProc(true)\n");
127 // Can only drop a card onto a suit-stack if the
128 // card is 1 higher, and is the same suit
130 bool CanDrop(CardRegion
&stackobj
, Card card
)
132 TRACE("ENTER CanDrop()\n");
135 const CardStack
&cardstack
= stackobj
.GetCardStack();
137 if(cardstack
.NumCards() > 0)
139 if(card
.Suit() != cardstack
[0].Suit())
141 TRACE("EXIT CanDrop()\n");
145 topval
= cardstack
[0].LoVal();
153 if(card
.LoVal() != (topval
+ 1))
155 TRACE("EXIT CanDrop()\n");
159 TRACE("EXIT CanDrop()\n");
164 // Can only drop a card onto suit stack if it is same suit, and 1 higher
166 bool CARDLIBPROC
SuitStackDropProc(CardRegion
&stackobj
, const CardStack
&dragcards
)
168 TRACE("ENTER SuitStackDropProc()\n");
169 //only drop 1 card at a time
170 if(dragcards
.NumCards() != 1)
172 TRACE("EXIT SuitStackDropProc()\n");
176 bool b
= CanDrop(stackobj
, dragcards
[0]);
177 TRACE("EXIT SuitStackDropProc()\n");
182 // Single-click on one of the row-stacks
183 // Turn the top-card over if they are all face-down
185 void CARDLIBPROC
RowStackClickProc(CardRegion
&stackobj
, int iNumClicked
)
187 TRACE("ENTER RowStackClickProc()\n");
190 stackobj
.GetFaceDirection(&numfacedown
);
192 //if all face-down, then make top card face-up
193 if(stackobj
.NumCards() == numfacedown
)
195 if(numfacedown
> 0) numfacedown
--;
196 stackobj
.SetFaceDirection(CS_FACE_DOWNUP
, numfacedown
);
199 TRACE("EXIT RowStackClickProc()\n");
203 // Find the suit-stack that can accept the specified card
205 CardRegion
*FindSuitStackFromCard(Card card
)
207 TRACE("ENTER FindSuitStackFromCard()\n");
208 for(int i
= 0; i
< 4; i
++)
210 if(CanDrop(*pSuitStack
[i
], card
))
212 TRACE("EXIT FindSuitStackFromCard()\n");
213 return pSuitStack
[i
];
217 TRACE("EXIT FindSuitStackFromCard()\n");
222 // What happens when we add a card to one of the suit stacks?
223 // Well, nothing (it is already added), but we need to
224 // check all four stacks (not just this one) to see if
225 // the game has finished.
227 void CARDLIBPROC
SuitStackAddProc(CardRegion
&stackobj
, const CardStack
&added
)
229 TRACE("ENTER SuitStackAddProc()\n");
230 bool fGameOver
= true;
232 for(int i
= 0; i
< 4; i
++)
234 if(pSuitStack
[i
]->NumCards() != 13)
243 MessageBox(SolWnd
, _T("Congratulations, you win!!"), szAppName
, MB_OK
| MB_ICONINFORMATION
);
245 for(int i
= 0; i
< 4; i
++)
247 pSuitStack
[i
]->Flash(11, 100);
250 TRACE("EXIT SuitStackAddProc()\n");
254 // Double-click on one of the row stacks
255 // The aim is to find a suit-stack to move the
256 // double-clicked card to.
258 void CARDLIBPROC
RowStackDblClickProc(CardRegion
&stackobj
, int iNumClicked
)
260 TRACE("ENTER RowStackDblClickProc()\n");
261 //can only move 1 card at a time
264 TRACE("EXIT RowStackDblClickProc()\n");
268 //find a suit-stack to move the card to...
269 const CardStack
&cardstack
= stackobj
.GetCardStack();
270 CardRegion
*pDest
= FindSuitStackFromCard(cardstack
[0]);
274 //stackobj.MoveCards(pDest, 1, true);
275 //use the SimulateDrag funcion, because we get the
276 //AddProc callbacks called for us on the destination stacks...
277 stackobj
.SimulateDrag(pDest
, 1, true);
279 TRACE("EXIT RowStackDblClickProc()\n");
283 // Face-up pile double-click
285 void CARDLIBPROC
PileDblClickProc(CardRegion
&stackobj
, int iNumClicked
)
287 TRACE("ENTER PileDblClickProc()\n");
288 RowStackDblClickProc(stackobj
, iNumClicked
);
289 TRACE("EXIT PileDblClickProc()\n");
293 // What happens when a card is removed from face-up pile?
295 void CARDLIBPROC
PileRemoveProc(CardRegion
&stackobj
, int iItems
)
297 TRACE("ENTER PileRemoveProc()\n");
298 //modify our "virtual" pile by removing the same card
299 //that was removed from the physical card stack
300 activepile
.Pop(iItems
);
302 //if there is just 1 card left, then modify the
303 //stack to contain ALL the face-up cards..the effect
304 //will be, the next time a card is dragged, all the
305 //previous card-triplets will be available underneath
306 if(stackobj
.NumCards() == 1)
308 stackobj
.SetOffsets(0,0);
309 stackobj
.SetCardStack(activepile
);
311 TRACE("EXIT PileRemoveProc()\n");
315 // Double-click on the deck
316 // Move 3 cards to the face-up pile
318 void CARDLIBPROC
DeckClickProc(CardRegion
&stackobj
, int iNumClicked
)
320 TRACE("ENTER DeckClickProc()\n");
321 CardStack cardstack
= stackobj
.GetCardStack();
322 CardStack pile
= pPile
->GetCardStack();
326 //reset the face-up pile to represent 3 cards
327 pPile
->SetOffsets(CS_DEFXOFF
, 1);
329 if(cardstack
.NumCards() == 0)
333 activepile
.Reverse();
334 cardstack
.Push(activepile
);
339 int numcards
= min(3, cardstack
.NumCards());
341 //make a "visible" copy of these cards
343 temp
= cardstack
.Pop(numcards
);
349 //remove the top 3 from deck
350 activepile
.Push(temp
);
355 pDeck
->SetCardStack(cardstack
);
356 pPile
->SetCardStack(pile
);
359 TRACE("EXIT DeckClickProc()\n");