00001 #ifndef MMANN_FCS_INDIRECT_STATE_WITH_LOCATIONS_H
00002 #define MMANN_FCS_INDIRECT_STATE_WITH_LOCATIONS_H
00003
00011
00012 #include <malloc.h>
00013 #include "FCSStackStorage.h"
00014 #include "FCHelpingAlgorithms.h"
00015
00016 class FCSIndirectState;
00017
00020 template <class StateWithLocation>
00021 class FCSIndirectStateWithLocations : public StateWithLocation
00022 {
00023 public:
00025 FCSIndirectStateWithLocations();
00026
00028 virtual ~FCSIndirectStateWithLocations();
00029
00033 virtual void Copy(FCSStateWithLocations* State);
00034
00040 int Compare(const FCSStateWithLocations* State);
00041
00047 FCSCard* GetStackCard(int Stack, int Card);
00048
00054 char GetStackCardSuit(int Stack, int Card);
00055
00061 char GetStackCardNumber(int Stack, int Card);
00062
00067 int GetStackLength(int Stack);
00068
00073 FCSCard* GetFreecellCard(int Freecell);
00074
00079 char GetFreecellCardSuit(int Freecell);
00080
00085 char GetFreecellCardNumber(int Freecell);
00086
00091 char GetFoundation(int Foundation);
00092
00096 void IncrementFoundation(int Foundation);
00097
00102 void SetFoundation(int Foundation, char Value);
00103
00108 void PopStackCard(int Stack, FCSCard* Card);
00109
00115 void PushStackCardIntoStack(int DestStack, int SrcStack, int SrcCard);
00116
00121 void PushCardIntoStack(int DestStack, FCSCard* Card);
00122
00127 void PutCardInFreecell(int Freecell, FCSCard* Card);
00128
00132 void EmptyFreecell(int Freecell);
00133
00138 void FlipStackCard(int Stack, int Card);
00139
00141 void Initialize(int NumberOfStacks);
00142
00144 void CleanState();
00145
00150 void CacheStacks(AFCSGenericStackStorage* Storage, int StackNumber);
00151
00156 void CanonizeState(int NumberOfFreecells, int NumberOfStacks);
00157
00161 FCSState* GetState();
00162
00163 public:
00165 FCSIndirectState m_State;
00166 };
00167
00168 template <class StateWithLocation>
00169 FCSIndirectStateWithLocations<StateWithLocation>::FCSIndirectStateWithLocations() : StateWithLocation()
00170 {
00171 }
00172
00173 template <class StateWithLocation>
00174 FCSIndirectStateWithLocations<StateWithLocation>::~FCSIndirectStateWithLocations()
00175 {
00176 }
00177
00178 template <class StateWithLocation>
00179 void FCSIndirectStateWithLocations<StateWithLocation>::Copy(FCSStateWithLocations* State)
00180 {
00181 StateWithLocation::Copy(State);
00182 int StackLength;
00183
00184 m_State.Copy(&(((FCSIndirectStateWithLocations*)State)->m_State));
00185
00186 for(int a=0;a<MAX_NUM_STACKS;a++)
00187 {
00188 if (((FCSIndirectStateWithLocations*)State)->m_State.m_Stacks[a] != NULL)
00189 {
00190 StackLength = State->GetStackLength(a);
00191
00192 m_State.m_Stacks[a] = new FCSIndirectCard[StackLength+53];
00193 for (int b = 0;b<StackLength+1;b++)
00194 m_State.m_Stacks[a][b].m_Card = ((FCSIndirectStateWithLocations*)State)->m_State.m_Stacks[a][b].m_Card;
00195 }
00196 }
00197 }
00198
00199 template <class StateWithLocation>
00200 int FCSIndirectStateWithLocations<StateWithLocation>::Compare(const FCSStateWithLocations *State)
00201 {
00202 return m_State.Compare(&(((FCSIndirectStateWithLocations*)State)->m_State));
00203 }
00204
00205 template <class StateWithLocation>
00206 FCSCard* FCSIndirectStateWithLocations<StateWithLocation>::GetStackCard(int Stack, int Card)
00207 {
00208 return &(m_State.m_Stacks[Stack][Card+1]);
00209 }
00210
00211 template <class StateWithLocation>
00212 char FCSIndirectStateWithLocations<StateWithLocation>::GetStackCardSuit(int Stack, int Card)
00213 {
00214 return m_State.m_Stacks[Stack][Card+1].GetSuit();
00215 }
00216
00217 template <class StateWithLocation>
00218 char FCSIndirectStateWithLocations<StateWithLocation>::GetStackCardNumber(int Stack, int Card)
00219 {
00220 return m_State.m_Stacks[Stack][Card+1].GetCardNumber();
00221 }
00222
00223 template <class StateWithLocation>
00224 int FCSIndirectStateWithLocations<StateWithLocation>::GetStackLength(int Stack)
00225 {
00226 return (int)m_State.m_Stacks[Stack][0].m_Card;
00227 }
00228
00229 template <class StateWithLocation>
00230 FCSCard* FCSIndirectStateWithLocations<StateWithLocation>::GetFreecellCard(int Freecell)
00231 {
00232 return &(m_State.m_Freecells[Freecell]);
00233 }
00234
00235 template <class StateWithLocation>
00236 char FCSIndirectStateWithLocations<StateWithLocation>::GetFreecellCardSuit(int Freecell)
00237 {
00238 return m_State.m_Freecells[Freecell].GetSuit();
00239 }
00240
00241 template <class StateWithLocation>
00242 char FCSIndirectStateWithLocations<StateWithLocation>::GetFreecellCardNumber(int Freecell)
00243 {
00244 return m_State.m_Freecells[Freecell].GetCardNumber();
00245 }
00246
00247 template <class StateWithLocation>
00248 char FCSIndirectStateWithLocations<StateWithLocation>::GetFoundation(int Foundation)
00249 {
00250 return m_State.m_Foundations[Foundation];
00251 }
00252
00253 template <class StateWithLocation>
00254 void FCSIndirectStateWithLocations<StateWithLocation>::IncrementFoundation(int Foundation)
00255 {
00256 m_State.m_Foundations[Foundation]++;
00257 }
00258
00259 template <class StateWithLocation>
00260 void FCSIndirectStateWithLocations<StateWithLocation>::SetFoundation(int Foundation, char Value)
00261 {
00262 m_State.m_Foundations[Foundation] = Value;
00263 }
00264
00265 template <class StateWithLocation>
00266 void FCSIndirectStateWithLocations<StateWithLocation>::PopStackCard(int Stack, FCSCard* Card)
00267 {
00268 FCSCard* Temp = GetStackCard(Stack, GetStackLength(Stack)-1);
00269 Card->Copy(Temp);
00270
00271 m_State.m_Stacks[Stack][GetStackLength(Stack)].EmptyCard();
00272 m_State.m_Stacks[Stack][0].m_Card--;
00273 }
00274
00275 template <class StateWithLocation>
00276 void FCSIndirectStateWithLocations<StateWithLocation>::PushStackCardIntoStack(int DestStack, int SrcStack, int SrcCard)
00277 {
00278 FCSCard* Temp = GetStackCard(SrcStack, SrcCard);
00279 PushCardIntoStack(DestStack, Temp);
00280 }
00281
00282 template <class StateWithLocation>
00283 void FCSIndirectStateWithLocations<StateWithLocation>::PushCardIntoStack(int DestStack, FCSCard* Card)
00284 {
00285 m_State.m_Stacks[DestStack][GetStackLength(DestStack)+1].Copy(Card);
00286 m_State.m_Stacks[DestStack][0].m_Card++;
00287 }
00288
00289 template <class StateWithLocation>
00290 void FCSIndirectStateWithLocations<StateWithLocation>::PutCardInFreecell(int Freecell, FCSCard* Card)
00291 {
00292 m_State.m_Freecells[Freecell].Copy(Card);
00293 }
00294
00295 template <class StateWithLocation>
00296 void FCSIndirectStateWithLocations<StateWithLocation>::EmptyFreecell(int Freecell)
00297 {
00298 m_State.m_Freecells[Freecell].m_Card = 0;
00299 }
00300
00301 template <class StateWithLocation>
00302 void FCSIndirectStateWithLocations<StateWithLocation>::FlipStackCard(int Stack, int Card)
00303 {
00304 m_State.m_Stacks[Stack][Card+1].SetFlipped(0);
00305 }
00306
00307 template <class StateWithLocation>
00308 void FCSIndirectStateWithLocations<StateWithLocation>::Initialize(int NumberOfStacks)
00309 {
00310 for(int a=0;a<NumberOfStacks;a++)
00311 m_State.m_Stacks[a] = new FCSIndirectCard[MAX_NUM_DECKS*52+1];
00312 }
00313
00314
00315 template <class StateWithLocation>
00316 void FCSIndirectStateWithLocations<StateWithLocation>::CleanState()
00317 {
00318 for(int s=0;s<MAX_NUM_STACKS;s++)
00319 if (m_State.m_Stacks[s] != NULL)
00320 delete [] m_State.m_Stacks[s];
00321 }
00322
00323 template <class StateWithLocation>
00324 void FCSIndirectStateWithLocations<StateWithLocation>::CacheStacks(AFCSGenericStackStorage* Storage, int StackNumber)
00325 {
00326 FCSIndirectCard *CacheStack;
00327
00328 for (int a = 0;a<StackNumber;a++)
00329 {
00330 Realloc<FCSIndirectCard>(&m_State.m_Stacks[a], GetStackLength(a)+1, GetStackLength(a)+1);
00331
00332 CacheStack = Storage->Insert(m_State.m_Stacks[a]);
00333 if (CacheStack != NULL)
00334 {
00335 delete [] m_State.m_Stacks[a];
00336 m_State.m_Stacks[a] = CacheStack;
00337 }
00338 }
00339 }
00340
00341 template <class StateWithLocation>
00342 void FCSIndirectStateWithLocations<StateWithLocation>::CanonizeState(int NumberOfFreecells, int NumberOfStacks)
00343 {
00344 int b,c;
00345 FCSIndirectCard *TempStack,
00346 TempFreecell;
00347 char TempLocation;
00348
00349
00350 for(b=1;b<NumberOfStacks;b++)
00351 {
00352 c = b;
00353 while(
00354 (c>0) &&
00355 (
00356 #if MAX_NUM_DECKS > 1
00357 m_State.StackCompare
00358 #else
00359 m_State.StackSortCompare
00360 #endif
00361 (
00362 c,
00363 c-1
00364 )
00365 < 0
00366 )
00367 )
00368 {
00369 TempStack = m_State.m_Stacks[c];
00370 m_State.m_Stacks[c] = m_State.m_Stacks[c-1];
00371 m_State.m_Stacks[c-1] = TempStack;
00372
00373 TempLocation = m_StackLocations[c];
00374 m_StackLocations[c] = m_StackLocations[c-1];
00375 m_StackLocations[c-1] = TempLocation;
00376
00377 c--;
00378 }
00379 }
00380
00381
00382 for(b=1;b<NumberOfFreecells;b++)
00383 {
00384 c = b;
00385 while((c>0) && (m_State.m_Freecells[c].Compare(&(m_State.m_Freecells[c-1])) < 0) )
00386 {
00387 TempFreecell.m_Card = m_State.m_Freecells[c].m_Card;
00388 m_State.m_Freecells[c].m_Card = m_State.m_Freecells[c-1].m_Card;
00389 m_State.m_Freecells[c-1].m_Card = TempFreecell.m_Card;
00390
00391 TempLocation = m_FreecellLocations[c];
00392 m_FreecellLocations[c] = m_FreecellLocations[c-1];
00393 m_FreecellLocations[c-1] = TempLocation;
00394
00395 c--;
00396 }
00397 }
00398 }
00399
00400 template <class StateWithLocation>
00401 FCSState* FCSIndirectStateWithLocations<StateWithLocation>::GetState()
00402 {
00403 return &m_State;
00404 }
00405
00406 #endif