examples/SFExamples/OandXViewArch/S60/src/oandxcontroller.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 <eikenv.h>
00031 #include <eiklabel.h>
00032 #include <s32mem.h>
00033 
00034 #include "oandxcontroller.h"
00035 #include "oandxengine.h"
00036 #include "oandxappui.h"
00037 #include "oandxdefs.h"
00038 
00039 COandXController* COandXController::NewL()
00046         {
00047         COandXController* self=new(ELeave) COandXController;
00048         CleanupStack::PushL(self);
00049         self->ConstructL();
00050         CleanupStack::Pop();
00051         return self;
00052         }
00053 
00054 void COandXController::ConstructL()
00058         {
00059         Engine().Reset();
00060         iState = ENewGame;
00061         iCrossTurn = EFalse;
00062         iLastGameResult = ETileDraw;
00063         }
00064 
00065 COandXController::~COandXController()
00070         {
00071         // empty.
00072         }
00073 
00074 // persistence
00075 
00076 void COandXController::ExternalizeL(RWriteStream& aStream) const
00086         {
00087         aStream.WriteUint8L(iState);
00088         aStream.WriteInt8L(iCrossTurn);
00089         aStream.WriteInt8L(iLastGameResult);
00090         aStream.WriteUint8L(iNumGames);
00091         aStream.WriteUint8L(iNumNoughtWins);
00092         aStream.WriteUint8L(iNumCrossWins);
00093         for (TInt i=0; i<KNumHistoryRecords; i++)
00094                 {
00095                 aStream.WriteUint8L(iGameRecords[i]);
00096                 }
00097         }
00098 
00099 void COandXController::InternalizeL(RReadStream& aStream)
00107         {
00108         iState = static_cast<TState>(aStream.ReadUint8L());
00109         iCrossTurn = static_cast<TBool>(aStream.ReadInt8L());
00110         iLastGameResult = static_cast<TTileState>(aStream.ReadUint8L());
00111         iNumGames = aStream.ReadUint8L();
00112         iNumNoughtWins = aStream.ReadUint8L();
00113         iNumCrossWins = aStream.ReadUint8L();
00114         for (TInt i=0; i<KNumHistoryRecords; i++)
00115                 {
00116                 iGameRecords[i] = static_cast<TTileState>(aStream.ReadUint8L());
00117                 }
00118         }
00119 
00120 void COandXController::Reset()
00125         {
00126         Engine().Reset();
00127         iState = ENewGame;
00128         if (IsCrossTurn())
00129                 {
00130                 SwitchTurn();
00131                 }
00132         switch (iLastGameResult)
00133                 {
00134         case ETileNought:
00135                 iNumNoughtWins += 1;
00136                 break;
00137         case ETileCross:
00138                 iNumCrossWins += 1;
00139                 break;
00140         default:
00141                 break;
00142                 }
00143         for (TInt i=KNumHistoryRecords-1; i>0; i--)
00144                 {
00145                 iGameRecords[i] = iGameRecords[i-1];
00146                 }
00147         iGameRecords[0]=iLastGameResult;
00148         iLastGameResult = ETileDraw;
00149         iNumGames += 1;
00150         }
00151 
00152 void COandXController::ResetStats()
00153         {
00154         iNumGames=0;
00155         iNumNoughtWins=0;
00156         iNumCrossWins=0;
00157         for (TInt i=0; i<KNumHistoryRecords;i++)
00158                 {
00159                 iGameRecords[i]=ETileBlank;
00160                 }
00161         }
00162 
00163 TBool COandXController::HitSquareL(TInt aIndex)
00164         {
00165         // For Comms, replace this with another function, called
00166         // when a tile is selected. It should refuse to accept
00167         // the hit if it is not my move.
00168         // Add another function, called when the opponent makes a
00169         // move, and both can call this funtion (renamed from
00170         // HitSquareL) The logic will need to be modified to handle 
00171         // the additional comms states and to report which of the
00172         // two players wins the game (or when the game is drawn).
00173         if (iState == EFinished)
00174                 {
00175                 return EFalse;
00176                 }
00177         if (iState == ENewGame)
00178                 {
00179                 iState = EPlaying;
00180                 }
00181         if (Engine().TryMakeMove(aIndex,IsCrossTurn()))
00182                 {
00183                 SwitchTurn();
00184                 TTileState winner = Engine().GameWonBy();
00185                 if (winner)
00186                         {
00187                         iLastGameResult = winner;
00188                         iState = EFinished;
00189                         OandXAppUi()->ReportWinnerL(winner);
00190                         }
00191                 return ETrue;
00192                 }
00193         return EFalse;
00194         }
00195 
00196 void COandXController::SwitchTurn()
00197         {
00198         iCrossTurn = !iCrossTurn;
00199         OandXAppUi()->ReportWhoseTurn();
00200         }
00201 
00202 TTileState COandXController::GameRecord(TInt aIndex)
00203         {
00204         return iGameRecords[aIndex];
00205         }

Generated by  doxygen 1.6.2