examples/SFExamples/games_on_symbian_os_code/oandx/src/oandxcontroller.cpp

00001 // 
00002 // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
00003 // All rights reserved.
00004 // This component and the accompanying materials are made available
00005 // under the terms of the License "Eclipse Public License v1.0"
00006 // which accompanies this distribution, and is available
00007 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00008 // 
00009 // Initial Contributors:
00010 // Nokia Corporation - initial contribution.
00011 // 
00012 // Contributors:
00013 // 
00014 // Description:
00015 // 
00016 
00017 #include <eikenv.h>
00018 #include <eiklabel.h>
00019 #include <s32mem.h>
00020 
00021 #include "oandxcontroller.h"
00022 #include "oandxengine.h"
00023 #include "oandxappui.h"
00024 #include "oandxdefs.h"
00025 
00026 COandXController* COandXController::NewL()
00033         {
00034         COandXController* self=new(ELeave) COandXController;
00035         CleanupStack::PushL(self);
00036         self->ConstructL();
00037         CleanupStack::Pop();
00038         return self;
00039         }
00040 
00041 void COandXController::ConstructL()
00045         {
00046         Reset();
00047         }
00048 
00049 COandXController::~COandXController()
00054         {
00055         // empty.
00056         }
00057 
00058 // persistence
00059 
00060 void COandXController::ExternalizeL(RWriteStream& aStream) const
00070         {
00071         aStream.WriteUint8L(iState);
00072         aStream.WriteInt8L(iCrossTurn);
00073         }
00074 
00075 void COandXController::InternalizeL(RReadStream& aStream)
00083         {
00084         iState = static_cast<TState>(aStream.ReadUint8L());
00085         iCrossTurn = static_cast<TBool>(aStream.ReadInt8L());
00086         }
00087 
00088 void COandXController::Reset()
00093         {
00094         Engine().Reset();
00095         iState = ENewGame;
00096         if (IsCrossTurn())
00097                 {
00098                 SwitchTurn();
00099                 }
00100         }
00101 
00102 TBool COandXController::HitSquareL(TInt aIndex)
00103         {
00104         // For Comms, replace this with another function, called
00105         // when a tile is selected. It should refuse to accept
00106         // the hit if it is not my move.
00107         // Add another function, called when the opponent makes a
00108         // move, and both can call this funtion (renamed from
00109         // HitSquareL) The logic will need to be modified to handle 
00110         // the additional comms states and to report which of the
00111         // two players wins the game (or when the game is drawn).
00112         if (iState == EFinished)
00113                 {
00114                 return EFalse;
00115                 }
00116         if (iState == ENewGame)
00117                 {
00118                 iState = EPlaying;
00119                 }
00120         if (Engine().TryMakeMove(aIndex,IsCrossTurn()))
00121                 {
00122                 SwitchTurn();
00123                 TInt winner = Engine().GameWonBy();
00124                 if (winner)
00125                         {
00126                         iState = EFinished;
00127                         OandXAppUi()->ReportWinnerL(winner);
00128                         }
00129                 return ETrue;
00130                 }
00131         return EFalse;
00132         }
00133 
00134 void COandXController::SwitchTurn()
00135         {
00136         iCrossTurn = !iCrossTurn;
00137         OandXAppUi()->ReportWhoseTurn();
00138         }
00139 

Generated by  doxygen 1.6.2