00001 #ifndef MMANN_FCS_COMPACT_STATE_WITH_LOCATIONS_H
00002 #define MMANN_FCS_COMPACT_STATE_WITH_LOCATIONS_H
00003
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "FCCompactState.h"
00034
00035
00037 #define FCS_FREECELLS_OFFSET (MAX_NUM_STACKS*(MAX_NUM_CARDS_IN_A_STACK+1))
00038
00039 #define FCS_FOUNDATIONS_OFFSET (MAX_NUM_STACKS*(MAX_NUM_CARDS_IN_A_STACK+1)+MAX_NUM_FREECELLS)
00040
00043 template <class StateWithLocation>
00044 class FCSCompactStateWithLocations : public StateWithLocation
00045 {
00046 public:
00047
00049 FCSCompactStateWithLocations();
00050
00052 virtual ~FCSCompactStateWithLocations();
00053
00057 virtual void Copy(FCSStateWithLocations* State);
00058
00064 int Compare(const FCSStateWithLocations* State);
00065
00071 FCSCard* GetStackCard(int Stack, int Card);
00072
00078 char GetStackCardSuit(int Stack, int Card);
00079
00085 char GetStackCardNumber(int Stack, int Card);
00086
00091 int GetStackLength(int Stack);
00092
00097 FCSCard* GetFreecellCard(int Freecell);
00098
00103 char GetFreecellCardSuit(int Freecell);
00104
00109 char GetFreecellCardNumber(int Freecell);
00110
00115 char GetFoundation(int Foundation);
00116
00120 void IncrementFoundation(int Foundation);
00121
00126 void SetFoundation(int Foundation, char Value);
00127
00132 void PopStackCard(int Stack, FCSCard* Card);
00133
00139 void PushStackCardIntoStack(int DestStack, int SrcStack, int SrcCard);
00140
00145 void PushCardIntoStack(int DestStack, FCSCard* Card);
00146
00151 void PutCardInFreecell(int Freecell, FCSCard* Card);
00152
00156 void EmptyFreecell(int Freecell);
00157
00162 void FlipStackCard(int Stack, int Card);
00163
00168 void CanonizeState(int NumberOfFreecells, int NumberOfStacks);
00169
00173 FCSState* GetState();
00174
00175 protected:
00177 FCSCompactState m_State;
00178 };
00179
00180 template <class StateWithLocation>
00181 FCSCompactStateWithLocations<StateWithLocation>::FCSCompactStateWithLocations() : StateWithLocation()
00182 {
00183 }
00184
00185 template <class StateWithLocation>
00186 FCSCompactStateWithLocations<StateWithLocation>::~FCSCompactStateWithLocations()
00187 {
00188 }
00189
00190 template <class StateWithLocation>
00191 void FCSCompactStateWithLocations<StateWithLocation>::Copy(FCSStateWithLocations* State)
00192 {
00193 StateWithLocation::Copy(State);
00194
00195 m_State = ((FCSCompactStateWithLocations*)State)->m_State;
00196 }
00197
00198 template <class StateWithLocation>
00199 int FCSCompactStateWithLocations<StateWithLocation>::Compare(const FCSStateWithLocations *State)
00200 {
00201 return m_State.Compare(&(((FCSCompactStateWithLocations*)State)->m_State));
00202 }
00203
00204 template <class StateWithLocation>
00205 FCSCard* FCSCompactStateWithLocations<StateWithLocation>::GetStackCard(int Stack, int Card)
00206 {
00207 return &(m_State.m_Data[Stack*(MAX_NUM_CARDS_IN_A_STACK+1)+Card+1]);
00208 }
00209
00210 template <class StateWithLocation>
00211 char FCSCompactStateWithLocations<StateWithLocation>::GetStackCardSuit(int Stack, int Card)
00212 {
00213 return m_State.m_Data[Stack*(MAX_NUM_CARDS_IN_A_STACK+1)+Card+1].GetSuit();
00214 }
00215
00216 template <class StateWithLocation>
00217 char FCSCompactStateWithLocations<StateWithLocation>::GetStackCardNumber(int Stack, int Card)
00218 {
00219 return m_State.m_Data[Stack*(MAX_NUM_CARDS_IN_A_STACK+1)+Card+1].GetCardNumber();
00220 }
00221
00222 template <class StateWithLocation>
00223 int FCSCompactStateWithLocations<StateWithLocation>::GetStackLength(int Stack)
00224 {
00225 return (int)m_State.m_Data[Stack*(MAX_NUM_CARDS_IN_A_STACK+1)].m_Card;
00226 }
00227
00228 template <class StateWithLocation>
00229 FCSCard* FCSCompactStateWithLocations<StateWithLocation>::GetFreecellCard(int Freecell)
00230 {
00231 return &(m_State.m_Data[FCS_FREECELLS_OFFSET + Freecell]);
00232 }
00233
00234 template <class StateWithLocation>
00235 char FCSCompactStateWithLocations<StateWithLocation>::GetFreecellCardSuit(int Freecell)
00236 {
00237 return m_State.m_Data[FCS_FREECELLS_OFFSET + Freecell].GetSuit();
00238 }
00239
00240 template <class StateWithLocation>
00241 char FCSCompactStateWithLocations<StateWithLocation>::GetFreecellCardNumber(int Freecell)
00242 {
00243 return m_State.m_Data[FCS_FREECELLS_OFFSET + Freecell].GetCardNumber();
00244 }
00245
00246 template <class StateWithLocation>
00247 char FCSCompactStateWithLocations<StateWithLocation>::GetFoundation(int Foundation)
00248 {
00249 return m_State.m_Data[FCS_FOUNDATIONS_OFFSET + Foundation].m_Card;
00250 }
00251
00252 template <class StateWithLocation>
00253 void FCSCompactStateWithLocations<StateWithLocation>::IncrementFoundation(int Foundation)
00254 {
00255 m_State.m_Data[FCS_FOUNDATIONS_OFFSET + Foundation].m_Card++;
00256 }
00257
00258 template <class StateWithLocation>
00259 void FCSCompactStateWithLocations<StateWithLocation>::SetFoundation(int Foundation, char Value)
00260 {
00261 m_State.m_Data[FCS_FOUNDATIONS_OFFSET + Foundation].m_Card = Value;
00262 }
00263
00264 template <class StateWithLocation>
00265 void FCSCompactStateWithLocations<StateWithLocation>::PopStackCard(int Stack, FCSCard* Card)
00266 {
00267 FCSCard* Temp = GetStackCard(Stack, GetStackLength(Stack)-1);
00268 Card->Copy(Temp);
00269
00270 m_State.m_Data[Stack*(MAX_NUM_CARDS_IN_A_STACK+1)+1+GetStackLength(Stack)-1].EmptyCard();
00271 m_State.m_Data[Stack*(MAX_NUM_CARDS_IN_A_STACK+1)].m_Card--;
00272 }
00273
00274 template <class StateWithLocation>
00275 void FCSCompactStateWithLocations<StateWithLocation>::PushStackCardIntoStack(int DestStack, int SrcStack, int SrcCard)
00276 {
00277 FCSCard* Temp = GetStackCard(SrcStack, SrcCard);
00278 PushCardIntoStack(DestStack, Temp);
00279 }
00280
00281 template <class StateWithLocation>
00282 void FCSCompactStateWithLocations<StateWithLocation>::PushCardIntoStack(int DestStack, FCSCard* Card)
00283 {
00284 m_State.m_Data[DestStack*(MAX_NUM_CARDS_IN_A_STACK+1)+1+GetStackLength(DestStack)].Copy(Card);
00285 m_State.m_Data[DestStack*(MAX_NUM_CARDS_IN_A_STACK+1)].m_Card++;
00286 }
00287
00288 template <class StateWithLocation>
00289 void FCSCompactStateWithLocations<StateWithLocation>::PutCardInFreecell(int Freecell, FCSCard* Card)
00290 {
00291 m_State.m_Data[FCS_FREECELLS_OFFSET+Freecell].Copy(Card);
00292 }
00293
00294 template <class StateWithLocation>
00295 void FCSCompactStateWithLocations<StateWithLocation>::EmptyFreecell(int Freecell)
00296 {
00297 m_State.m_Data[FCS_FREECELLS_OFFSET+Freecell].m_Card = 0;
00298 }
00299
00300 template <class StateWithLocation>
00301 void FCSCompactStateWithLocations<StateWithLocation>::FlipStackCard(int Stack, int Card)
00302 {
00303 m_State.m_Data[Stack*(MAX_NUM_CARDS_IN_A_STACK+1)+Card+1].SetFlipped(0);
00304 }
00305
00306 template <class StateWithLocation>
00307 void FCSCompactStateWithLocations<StateWithLocation>::CanonizeState(int NumberOfFreecells, int NumberOfStacks)
00308 {
00309 int b,c;
00310
00311 FCSCompactCard TempStack[(MAX_NUM_CARDS_IN_A_STACK+1)];
00312 FCSCompactCard TempFeecell;
00313 char TempLocation;
00314
00315
00316
00317 for(b=1;b<NumberOfStacks;b++)
00318 {
00319 c = b;
00320 while((c>0) && (m_State.StackCompare(c*(MAX_NUM_CARDS_IN_A_STACK+1), (c-1)*(MAX_NUM_CARDS_IN_A_STACK+1)) < 0))
00321 {
00322 for (int a1 = 0;a1<(MAX_NUM_CARDS_IN_A_STACK+1);a1++)
00323 TempStack[a1] = m_State.m_Data[a1+c*(MAX_NUM_CARDS_IN_A_STACK+1)];
00324
00325 for (int a2 = 0;a2<(MAX_NUM_CARDS_IN_A_STACK+1);a2++)
00326 m_State.m_Data[a2+c*(MAX_NUM_CARDS_IN_A_STACK+1)] = m_State.m_Data[a2+ (c-1)*(MAX_NUM_CARDS_IN_A_STACK+1)];
00327
00328 for (int a3 = 0;a3<(MAX_NUM_CARDS_IN_A_STACK+1);a3++)
00329 m_State.m_Data[a3+(c-1)*(MAX_NUM_CARDS_IN_A_STACK+1)] = TempStack[a3];
00330
00331 TempLocation = m_StackLocations[c];
00332 m_StackLocations[c] = m_StackLocations[c-1];
00333 m_StackLocations[c-1] = TempLocation;
00334
00335 c--;
00336 }
00337 }
00338
00339
00340 for(b=1;b<NumberOfFreecells;b++)
00341 {
00342 c = b;
00343
00344 while( (c>0) && (m_State.m_Data[FCS_FREECELLS_OFFSET+c].Compare(&(m_State.m_Data[FCS_FREECELLS_OFFSET+c-1])) < 0) )
00345 {
00346 TempFeecell = m_State.m_Data[FCS_FREECELLS_OFFSET+c];
00347 m_State.m_Data[FCS_FREECELLS_OFFSET+c] = m_State.m_Data[FCS_FREECELLS_OFFSET+c-1];
00348 m_State.m_Data[FCS_FREECELLS_OFFSET+c-1] = TempFeecell;
00349
00350 TempLocation = m_FreecellLocations[c];
00351 m_FreecellLocations[c] = m_FreecellLocations[c-1];
00352 m_FreecellLocations[c-1] = TempLocation;
00353
00354 c--;
00355 }
00356 }
00357 }
00358
00359 template <class StateWithLocation>
00360 FCSState* FCSCompactStateWithLocations<StateWithLocation>::GetState()
00361 {
00362 return &m_State;
00363 }
00364
00365 #endif