Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenspigame.cpp
Go to the documentation of this file.
00001 /* 00002 * PROJECT: Spider Solitaire 00003 * LICENSE: See COPYING in top level directory 00004 * FILE: base/applications/games/spider/spigame.cpp 00005 * PURPOSE: Spider Solitaire game functions 00006 * PROGRAMMER: Gregor Schneider 00007 */ 00008 00009 #include "spider.h" 00010 00011 #define NUM_DECK_CARDS 5 00012 #define NUM_SMALLER_STACKS 4 00013 #define NUM_CARD_COLORS 4 00014 #define NUM_ONECOLOR_CARDS 13 00015 #define NUM_STD_CARDS 52 00016 #define NUM_SPIDER_CARDS 104 00017 00018 CardStack deck; 00019 CardRegion *from; 00020 CardRegion *pDeck; 00021 CardRegion *pStack[NUM_STACKS]; 00022 bool fGameStarted = false; 00023 int yRowStackCardOffset; 00024 int cardsFinished; 00025 extern TCHAR MsgDeal[]; 00026 extern TCHAR MsgWin[]; 00027 00028 CardStack CreatePlayDeck() 00029 { 00030 CardStack newStack; 00031 int i, colors = 1, num = 0; 00032 00033 switch (dwDifficulty) 00034 { 00035 case IDC_DIF_ONECOLOR: 00036 colors = 1; 00037 break; 00038 case IDC_DIF_TWOCOLORS: 00039 colors = 2; 00040 break; 00041 case IDC_DIF_FOURCOLORS: 00042 colors = 4; 00043 break; 00044 } 00045 for (i = 0; i < NUM_SPIDER_CARDS; i++) 00046 { 00047 num += NUM_CARD_COLORS / colors; 00048 Card newCard(num % NUM_STD_CARDS); 00049 newStack.Push(newCard); 00050 } 00051 return newStack; 00052 } 00053 00054 void NewGame(void) 00055 { 00056 int i, j; 00057 /* First four stack with five, all other with 4 */ 00058 int covCards = 5; 00059 CardStack fakeDeck, temp; 00060 00061 SpiderWnd.EmptyStacks(); 00062 00063 /* Create a new card-deck, fake deck */ 00064 deck = CreatePlayDeck(); 00065 deck.Shuffle(); 00066 fakeDeck.NewDeck(); 00067 fakeDeck.Shuffle(); 00068 00069 /* Reset progress value */ 00070 cardsFinished = 0; 00071 00072 /* Deal to each stack */ 00073 for (i = 0; i < NUM_STACKS; i++) 00074 { 00075 temp.Clear(); 00076 if (i == NUM_SMALLER_STACKS) 00077 { 00078 covCards--; 00079 } 00080 for (j = 0; j <= covCards; j++) 00081 { 00082 temp.Push(deck.Pop(1)); 00083 } 00084 pStack[i]->SetFaceDirection(CS_FACE_DOWNUP, covCards); 00085 pStack[i]->SetCardStack(temp); 00086 } 00087 /* Deal five fake cards to the deck */ 00088 pDeck->SetCardStack(fakeDeck.Pop(5)); 00089 00090 SpiderWnd.Redraw(); 00091 fGameStarted = false; 00092 } 00093 00094 bool stackLookingGood(const CardStack &mystack, int numChecks) 00095 { 00096 int i; 00097 for (i = 0; i < numChecks; i++) 00098 { 00099 if (mystack[i].LoVal() != mystack[i + 1].LoVal() - 1) 00100 { 00101 return false; 00102 } 00103 if (mystack[i].Suit() != mystack[i + 1].Suit()) 00104 { 00105 return false; 00106 } 00107 } 00108 return true; 00109 } 00110 00111 /* Card to be turned from a stack */ 00112 void TurnStackCard(CardRegion &stackobj) 00113 { 00114 int numfacedown; 00115 00116 stackobj.GetFaceDirection(&numfacedown); 00117 if (stackobj.NumCards() <= numfacedown) 00118 { 00119 if (numfacedown > 0) numfacedown--; 00120 stackobj.SetFaceDirection(CS_FACE_DOWNUP, numfacedown); 00121 stackobj.Redraw(); 00122 } 00123 } 00124 00125 /* Click on the deck */ 00126 void CARDLIBPROC DeckClickProc(CardRegion &stackobj, int NumDragCards) 00127 { 00128 CardStack temp, fakeDeck = pDeck->GetCardStack(); 00129 fGameStarted = true; 00130 00131 if (fakeDeck.NumCards() != 0 && deck.NumCards() != 0) 00132 { 00133 int i, facedown, faceup; 00134 /* Add one card to every stack */ 00135 for (i = 0; i < NUM_STACKS; i++) 00136 { 00137 temp = pStack[i]->GetCardStack(); 00138 temp.Push(deck.Pop()); 00139 00140 /* Check if we accidentally finished a row */ 00141 pStack[i]->GetFaceDirection(&facedown); 00142 faceup = temp.NumCards() - facedown; 00143 if (faceup >= NUM_ONECOLOR_CARDS) 00144 { 00145 /* Check stack finished, remove cards if so */ 00146 if (stackLookingGood(temp, NUM_ONECOLOR_CARDS - 1)) 00147 { 00148 int j; 00149 for (j = 0; j < NUM_ONECOLOR_CARDS; j++) 00150 { 00151 temp.RemoveCard(0); 00152 } 00153 cardsFinished += NUM_ONECOLOR_CARDS; 00154 pStack[i]->SetCardStack(temp); 00155 /* Turn now topmost card */ 00156 TurnStackCard(*pStack[i]); 00157 } 00158 } 00159 pStack[i]->SetCardStack(temp); 00160 } 00161 /* Remove one card from the fake ones */ 00162 pDeck->SetCardStack(fakeDeck.Pop(fakeDeck.NumCards() - 1)); 00163 } 00164 pDeck->Update(); 00165 SpiderWnd.Redraw(); 00166 } 00167 00168 /* Cards dragged from a stack */ 00169 bool CARDLIBPROC StackDragProc(CardRegion &stackobj, int numDragCards) 00170 { 00171 int numfacedown, numcards; 00172 00173 stackobj.GetFaceDirection(&numfacedown); 00174 numcards = stackobj.NumCards(); 00175 00176 /* Only cards facing up */ 00177 if (numDragCards <= numcards - numfacedown) 00178 { 00179 const CardStack &mystack = stackobj.GetCardStack(); 00180 /* Don't allow to drag unsuited cards */ 00181 if (!stackLookingGood(mystack, numDragCards - 1)) 00182 { 00183 return false; 00184 } 00185 /* Remember where the cards come from */ 00186 from = &stackobj; 00187 return true; 00188 } 00189 else 00190 { 00191 return false; 00192 } 00193 } 00194 00195 /* Game finished successfully */ 00196 void GameFinished() 00197 { 00198 SpiderWnd.EmptyStacks(); 00199 00200 MessageBox(SpiderWnd, MsgWin, szAppName, MB_OK | MB_ICONINFORMATION); 00201 if( IDYES == MessageBox(SpiderWnd, MsgDeal, szAppName, MB_YESNO | MB_ICONQUESTION) ) 00202 { 00203 NewGame(); 00204 } 00205 else 00206 { 00207 fGameStarted = false; 00208 } 00209 } 00210 00211 /* Card added, check for win situation */ 00212 void CARDLIBPROC StackAddProc(CardRegion &stackobj, const CardStack &added) 00213 { 00214 if (cardsFinished == NUM_SPIDER_CARDS) 00215 { 00216 GameFinished(); 00217 } 00218 } 00219 00220 /* Cards dropped to a stack */ 00221 bool CARDLIBPROC StackDropProc(CardRegion &stackobj, CardStack &dragcards) 00222 { 00223 Card dragcard = dragcards[dragcards.NumCards() - 1]; 00224 int faceup, facedown; 00225 00226 /* Only drop our cards on other stacks */ 00227 if (stackobj.Id() == from->Id()) 00228 { 00229 return false; 00230 } 00231 00232 /* If stack is empty, everything can be dropped */ 00233 if (stackobj.NumCards() != 0) 00234 { 00235 const CardStack &mystack = stackobj.GetCardStack(); 00236 00237 /* Can only drop if card is 1 less */ 00238 if (mystack[0].LoVal() != dragcard.LoVal() + 1) 00239 { 00240 return false; 00241 } 00242 00243 /* Check if stack complete */ 00244 stackobj.GetFaceDirection(&facedown); 00245 faceup = stackobj.NumCards() - facedown; 00246 00247 if (faceup + dragcards.NumCards() >= NUM_ONECOLOR_CARDS) 00248 { 00249 int i, max = NUM_ONECOLOR_CARDS - dragcards.NumCards() - 1; 00250 00251 /* Dragged cards have been checked to be in order, check stack cards */ 00252 if (stackLookingGood(mystack, max)) 00253 { 00254 CardStack s = stackobj.GetCardStack(); 00255 CardStack f; 00256 00257 /* Remove from card stack */ 00258 for (i = 0; i < max + 1; i++) 00259 { 00260 s.RemoveCard(0); 00261 } 00262 /* Remove dragged cards */ 00263 dragcards = f; 00264 stackobj.SetCardStack(s); 00265 cardsFinished += NUM_ONECOLOR_CARDS; 00266 /* Flip top card of the dest stack */ 00267 TurnStackCard(stackobj); 00268 } 00269 } 00270 } 00271 /* Flip the top card of the source stack */ 00272 TurnStackCard(*from); 00273 fGameStarted = true; 00274 return true; 00275 } 00276 00277 /* Create card regions */ 00278 void CreateSpider() 00279 { 00280 int i, pos; 00281 00282 /* Compute the value for yRowStackCardOffset based on the height of the card, so the card number isn't hidden on larger cards */ 00283 yRowStackCardOffset = (int)(__cardheight / 6.7); 00284 00285 pDeck = SpiderWnd.CreateRegion(0, true, 0, 0, -15, 0); 00286 pDeck->SetFaceDirection(CS_FACE_DOWN, 0); 00287 pDeck->SetEmptyImage(CS_EI_CIRC); 00288 pDeck->SetPlacement(CS_XJUST_RIGHT, CS_YJUST_BOTTOM, - X_BORDER, - Y_BORDER); 00289 pDeck->SetDragRule(CS_DRAG_NONE, 0); 00290 pDeck->SetDropRule(CS_DROP_NONE, 0); 00291 pDeck->SetClickReleaseProc(DeckClickProc); 00292 00293 /* Create the row stacks */ 00294 for (i = 0; i < NUM_STACKS; i++) 00295 { 00296 pStack[i] = SpiderWnd.CreateRegion(NUM_STACKS+i, true, 0, Y_BORDER, 0, yRowStackCardOffset); 00297 pStack[i]->SetFaceDirection(CS_FACE_DOWN, 0); 00298 pos = i - NUM_STACKS/2; 00299 pStack[i]->SetPlacement(CS_XJUST_CENTER, 0, 00300 pos * (__cardwidth + X_BORDER) + 6 * X_BORDER, 0); 00301 pStack[i]->SetEmptyImage(CS_EI_SUNK); 00302 pStack[i]->SetDragRule(CS_DRAG_CALLBACK, StackDragProc); 00303 pStack[i]->SetDropRule(CS_DROP_CALLBACK, StackDropProc); 00304 pStack[i]->SetAddCardProc(StackAddProc); 00305 } 00306 } 00307 Generated on Sun May 27 2012 04:16:36 for ReactOS by
1.7.6.1
|