00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <e32math.h>
00018
00019 #include "oandxdefs.h"
00020 #include "oandxengine.h"
00021
00022
00023 COandXEngine* COandXEngine::NewL()
00033 {
00034 return new(ELeave) COandXEngine;
00035 }
00036
00037 COandXEngine::COandXEngine()
00042 {
00043 Reset();
00044 }
00045
00046 COandXEngine::~COandXEngine()
00051 {
00052
00053 }
00054
00055 void COandXEngine::Reset()
00060 {
00061 for (TInt i=0; i<KNumberOfTiles; i++)
00062 {
00063 iTileStates[i] = ETileBlank;
00064 }
00065 }
00066
00067 TTileState COandXEngine::TileStatus(TInt aIndex) const
00075 {
00076 return iTileStates[aIndex];
00077 }
00078
00079 TBool COandXEngine::TryMakeMove(TInt aIndex, TBool aCrossTurn)
00090 {
00091 if (iTileStates[aIndex] == ETileBlank)
00092 {
00093 iTileStates[aIndex] = aCrossTurn ? ETileCross : ETileNought;
00094 return ETrue;
00095 }
00096 return EFalse;
00097 }
00098
00099 TInt COandXEngine::TileState(TInt aX, TInt aY) const
00107 {
00108 ASSERT(aX >= 0 && aX < KTilesPerSide);
00109 ASSERT(aY >= 0 && aY < KTilesPerSide);
00110
00111 return iTileStates[aY * KTilesPerSide + aX];
00112 }
00113
00114 TTileState COandXEngine::GameWonBy() const
00123 {
00124 const TInt KNoughtWinSum = KTilesPerSide * ETileNought;
00125 const TInt KCrossWinSum = KTilesPerSide * ETileCross;
00126
00127
00128 for (TInt i = 0; i < KTilesPerSide; ++i)
00129 {
00130 TInt rowSum = 0;
00131 TInt colSum = 0;
00132 for (TInt j = 0; j < KTilesPerSide; ++j)
00133 {
00134 rowSum += TileState(j, i);
00135 colSum += TileState(i, j);
00136 }
00137
00138 if (rowSum == KNoughtWinSum || colSum == KNoughtWinSum)
00139 {
00140 return ETileNought;
00141 }
00142 if (rowSum == KCrossWinSum || colSum == KCrossWinSum)
00143 {
00144 return ETileCross;
00145 }
00146 }
00147
00148
00149 TInt blTrSum = 0;
00150 TInt tlBrSum = 0;
00151 for (TInt i = 0; i < KTilesPerSide; ++i)
00152 {
00153 tlBrSum += TileState(i,i);
00154 blTrSum += TileState(i,KTilesPerSide - 1 - i);
00155 }
00156
00157 if (blTrSum == KNoughtWinSum || tlBrSum == KNoughtWinSum)
00158 {
00159 return ETileNought;
00160 }
00161
00162 if (blTrSum == KCrossWinSum || tlBrSum == KCrossWinSum)
00163 {
00164 return ETileCross;
00165 }
00166
00167 return ETileBlank;
00168 }
00169
00170
00171 void COandXEngine::ExternalizeL(RWriteStream& aStream) const
00179 {
00180 for (TInt i = 0; i<KNumberOfTiles; i++)
00181 {
00182 aStream.WriteInt8L(iTileStates[i]);
00183 }
00184 }
00185
00186 void COandXEngine::InternalizeL(RReadStream& aStream)
00194 {
00195 for (TInt i = 0; i<KNumberOfTiles; i++)
00196 {
00197 iTileStates[i] = static_cast<TTileState>(aStream.ReadInt8L());
00198 }
00199 }