00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <string.h>
00010 #include <stdlib.h>
00011 #include "FCIndirectState.h"
00012 #include "FCHelpingAlgorithms.h"
00013
00014 FCSIndirectCard::FCSIndirectCard()
00015 {
00016 EmptyCard();
00017 }
00018
00019 FCSIndirectCard::~FCSIndirectCard()
00020 {
00021 }
00022
00023 void FCSIndirectCard::Copy(FCSCard* Card)
00024 {
00025 m_Card = ((FCSIndirectCard*)Card)->m_Card;
00026 }
00027
00028 void FCSIndirectCard::EmptyCard()
00029 {
00030 m_Card = 0;
00031 }
00032
00033 bool FCSIndirectCard::IsEmptyCard()
00034 {
00035 return (m_Card == 0);
00036 }
00037
00038 char FCSIndirectCard::GetCardNumber()
00039 {
00040 return (m_Card & 0x0F);
00041 }
00042
00043 char FCSIndirectCard::GetSuit()
00044 {
00045 return ((m_Card >> 4) & 0x03);
00046 }
00047
00048 char FCSIndirectCard::GetFlipped()
00049 {
00050 return (m_Card >> 6);
00051 }
00052
00053 void FCSIndirectCard::SetSuit(char Suit)
00054 {
00055 m_Card = ((m_Card&0x4F)|(Suit<<4));
00056 }
00057
00058 void FCSIndirectCard::SetCardNumber(char CardNumber)
00059 {
00060 m_Card = ((m_Card&0xF0)|CardNumber);
00061 }
00062
00063 void FCSIndirectCard::SetFlipped(char Flipped)
00064 {
00065 m_Card = ((m_Card&0x3F)|(Flipped<<6));
00066 }
00067
00068 int FCSIndirectCard::GetClassSize()
00069 {
00070 return sizeof(FCSIndirectCard);
00071 }
00072
00073 FCSIndirectState::FCSIndirectState()
00074 {
00075 for(char a=0;a<MAX_NUM_STACKS;a++)
00076 {
00077 m_Stacks[a] = NULL;
00078 }
00079 memset(m_Foundations, 0, MAX_NUM_DECKS*4);
00080 }
00081
00082 FCSIndirectState::~FCSIndirectState()
00083 {
00084 }
00085
00086 int FCSIndirectState::StackCompare(int StackPosition1, int StackPosition2)
00087 {
00088 FCSIndirectCard* Card1 = m_Stacks[StackPosition1];
00089 FCSIndirectCard* Card2 = m_Stacks[StackPosition2];
00090
00091 int MinimumLength = min(Card1->m_Card, Card2->m_Card),
00092 ReturnValue;
00093
00094 for(int a=0;a<MinimumLength;a++)
00095 {
00096 Card1 = &m_Stacks[StackPosition1][a+1];
00097 Card2 = &m_Stacks[StackPosition2][a+1];
00098
00099 ReturnValue = Card1->Compare(Card2);
00100 if (ReturnValue != 0)
00101 return ReturnValue;
00102 }
00103
00104
00105
00106
00107
00108
00109
00110 Card1 = m_Stacks[StackPosition1];
00111 Card2 = m_Stacks[StackPosition2];
00112
00113 return Card1->Compare(Card2);
00114 }
00115
00116 int FCSIndirectState::GetClassSize()
00117 {
00118 return sizeof(FCSIndirectState);
00119 }
00120
00121 int FCSIndirectState::StackSortCompare(int StackPosition1, int StackPosition2)
00122 {
00123 FCSIndirectCard* Card1 = &m_Stacks[StackPosition1][1];
00124 FCSIndirectCard* Card2 = &m_Stacks[StackPosition2][1];
00125
00126 return Card1->Compare(Card2);
00127 }
00128
00129 int FCSIndirectState::Compare(FCSIndirectState* State)
00130 {
00131 int CompareValue, a;
00132
00133 for (a=0;a<MAX_NUM_STACKS;a++)
00134 {
00135 if (m_Stacks[a] == State->m_Stacks[a])
00136 continue;
00137
00138 if ((m_Stacks[a] != NULL) && (State->m_Stacks[a] == NULL))
00139 return 1;
00140
00141 if ((m_Stacks[a] == NULL) && (State->m_Stacks[a] != NULL))
00142 return -1;
00143
00144
00145 if (m_Stacks[a][0].m_Card > State->m_Stacks[a][0].m_Card)
00146 return 1;
00147
00148 if (m_Stacks[a][0].m_Card < State->m_Stacks[a][0].m_Card)
00149 return -1;
00150
00151
00152 for (int i = 1; i<=m_Stacks[a][0].m_Card;i++)
00153 if ( (CompareValue = m_Stacks[a][i].Compare(&State->m_Stacks[a][i])) != 0)
00154 return CompareValue;
00155 }
00156
00157 for (a=0;a<MAX_NUM_FREECELLS;a++)
00158 {
00159 if ( (CompareValue = m_Freecells[a].Compare(&(State->m_Freecells[a]))) != 0)
00160 return CompareValue;
00161 }
00162
00163 return memcmp(m_Foundations, State->m_Foundations, MAX_NUM_DECKS*4);
00164 }
00165
00166 void FCSIndirectState::Copy(FCSIndirectState* State)
00167 {
00168 for (char a = 0;a<MAX_NUM_FREECELLS;a++)
00169 m_Freecells[a].Copy(&State->m_Freecells[a]);
00170
00171 memcpy(m_Foundations, State->m_Foundations, MAX_NUM_DECKS*4);
00172 }
00173
00174