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 <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
00072 }
00073
00074
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
00166
00167
00168
00169
00170
00171
00172
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 }