examples/SFExamples/OandXViewArch/S60/src/oandxengine.cpp

00001 /*
00002 Copyright (c) 2002-2011 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:  
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         // empty.
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         // is there a row or column of matching tiles?
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         // is there a diagonal of matching tiles?
00162         TInt blTrSum = 0;               // bottom left to top right
00163         TInt tlBrSum = 0;               // top left to bottom right
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; // No winner
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         }

Generated by  doxygen 1.6.2