00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <e32math.h>
00031
00032 #include "oandxdefs.h"
00033 #include "oandxengine.h"
00034
00035
00036 COandXEngine* COandXEngine::NewL()
00046 {
00047 return new(ELeave) COandXEngine;
00048 }
00049
00050 COandXEngine::COandXEngine()
00055 {
00056 Reset();
00057 }
00058
00059 COandXEngine::~COandXEngine()
00064 {
00065
00066 }
00067
00068 void COandXEngine::Reset()
00073 {
00074 for (TInt i=0; i<KNumberOfTiles; i++)
00075 {
00076 iTileStates[i] = ETileBlank;
00077 }
00078 }
00079
00080 TTileState COandXEngine::TileStatus(TInt aIndex) const
00088 {
00089 return iTileStates[aIndex];
00090 }
00091
00092 TBool COandXEngine::TryMakeMove(TInt aIndex, TBool aCrossTurn)
00103 {
00104 if (iTileStates[aIndex] == ETileBlank)
00105 {
00106 iTileStates[aIndex] = aCrossTurn ? ETileCross : ETileNought;
00107 return ETrue;
00108 }
00109 return EFalse;
00110 }
00111
00112 TInt COandXEngine::TileState(TInt aX, TInt aY) const
00120 {
00121 ASSERT(aX >= 0 && aX < KTilesPerSide);
00122 ASSERT(aY >= 0 && aY < KTilesPerSide);
00123
00124 return iTileStates[aY * KTilesPerSide + aX];
00125 }
00126
00127 TTileState COandXEngine::GameWonBy() const
00136 {
00137 const TInt KNoughtWinSum = KTilesPerSide * ETileNought;
00138 const TInt KCrossWinSum = KTilesPerSide * ETileCross;
00139
00140
00141 for (TInt i = 0; i < KTilesPerSide; ++i)
00142 {
00143 TInt rowSum = 0;
00144 TInt colSum = 0;
00145 for (TInt j = 0; j < KTilesPerSide; ++j)
00146 {
00147 rowSum += TileState(j, i);
00148 colSum += TileState(i, j);
00149 }
00150
00151 if (rowSum == KNoughtWinSum || colSum == KNoughtWinSum)
00152 {
00153 return ETileNought;
00154 }
00155 if (rowSum == KCrossWinSum || colSum == KCrossWinSum)
00156 {
00157 return ETileCross;
00158 }
00159 }
00160
00161
00162 TInt blTrSum = 0;
00163 TInt tlBrSum = 0;
00164 for (TInt i = 0; i < KTilesPerSide; ++i)
00165 {
00166 tlBrSum += TileState(i,i);
00167 blTrSum += TileState(i,KTilesPerSide - 1 - i);
00168 }
00169
00170 if (blTrSum == KNoughtWinSum || tlBrSum == KNoughtWinSum)
00171 {
00172 return ETileNought;
00173 }
00174
00175 if (blTrSum == KCrossWinSum || tlBrSum == KCrossWinSum)
00176 {
00177 return ETileCross;
00178 }
00179
00180 return ETileBlank;
00181 }
00182
00183
00184 void COandXEngine::ExternalizeL(RWriteStream& aStream) const
00192 {
00193 for (TInt i = 0; i<KNumberOfTiles; i++)
00194 {
00195 aStream.WriteInt8L(iTileStates[i]);
00196 }
00197 }
00198
00199 void COandXEngine::InternalizeL(RReadStream& aStream)
00207 {
00208 for (TInt i = 0; i<KNumberOfTiles; i++)
00209 {
00210 iTileStates[i] = static_cast<TTileState>(aStream.ReadInt8L());
00211 }
00212 }