?? oandxcontroller.cpp
字號:
// oandxcontroller.cpp
//
// Copyright (c) 2006 Symbian Ltd. All rights reserved.
//
#include <eikenv.h>
#include <eiklabel.h>
#include <s32mem.h>
#include "oandxcontroller.h"
#include "oandxengine.h"
#include "oandxappui.h"
#include "oandxdefs.h"
COandXController* COandXController::NewL()
/**
Factory function allocates new instance of COandXController.
@return New, initialized instance of COandXController.
This object is owned by the caller.
*/
{
COandXController* self=new(ELeave) COandXController;
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop();
return self;
}
void COandXController::ConstructL()
/**
Run secondary initialization, clearing the board and
initializing the controller's state.
*/
{
Engine().Reset();
iState = ENewGame;
iCrossTurn = EFalse;
iLastGameResult = ETileDraw;
}
COandXController::~COandXController()
/**
Destructor is defined here to ensure only one
instance is generated.
*/
{
// empty.
}
void COandXController::SetStateL(TState aState)
{
iState = aState;
OandXAppUi()->UpdateCommandsL(IsNewGame(), IsCrossTurn());
}
// persistence
void COandXController::ExternalizeL(RWriteStream& aStream) const
/**
Persist the controller's state to the supplied stream.
Specifically, writes out the state, whose turn it is,
and which symbol the local player is using.
@param aStream Stream to which the controller's state will be
written.
@see InternalizeL
*/
{
aStream.WriteUint8L(iState);
aStream.WriteInt8L(iCrossTurn);
aStream.WriteInt8L(iLastGameResult);
aStream.WriteUint8L(iNumGames);
aStream.WriteUint8L(iNumNoughtWins);
aStream.WriteUint8L(iNumCrossWins);
for (TInt i=0; i<KNumHistoryRecords; i++)
{
aStream.WriteUint8L(iGameRecords[i]);
}
}
void COandXController::InternalizeL(RReadStream& aStream)
/**
Standard stream store internalization function, restores
state, whose turn, and which symbol the local player is using.
@param aStream Stream which contains externalized state.
@see ExternalizeL
*/
{
TState newState = static_cast<TState>(aStream.ReadUint8L());
iCrossTurn = static_cast<TBool>(aStream.ReadInt8L());
iLastGameResult = static_cast<TTileState>(aStream.ReadUint8L());
iNumGames = aStream.ReadUint8L();
iNumNoughtWins = aStream.ReadUint8L();
iNumCrossWins = aStream.ReadUint8L();
for (TInt i=0; i<KNumHistoryRecords; i++)
{
iGameRecords[i] = static_cast<TTileState>(aStream.ReadUint8L());
}
SetStateL(newState);
}
void COandXController::ResetL()
/**
Cancel the current game, clearing the board and setting
noughts as the current player.
Records the result of the last game and increments the
game counter.
Can not be used to initialize the controller, since
SetStateL() accesses the view, which does not exist
when the controller is constructed.
*/
{
Engine().Reset();
if (IsCrossTurn())
{
SwitchTurn();
}
switch (iLastGameResult)
{
case ETileNought:
iNumNoughtWins += 1;
break;
case ETileCross:
iNumCrossWins += 1;
break;
default:
break;
}
for (TInt i=KNumHistoryRecords-1; i>0; i--)
{
iGameRecords[i] = iGameRecords[i-1];
}
iGameRecords[0]=iLastGameResult;
iLastGameResult = ETileDraw;
iNumGames += 1;
SetStateL(ENewGame);
}
void COandXController::ResetStats()
{
iNumGames=0;
iNumNoughtWins=0;
iNumCrossWins=0;
for (TInt i=0; i<KNumHistoryRecords;i++)
{
iGameRecords[i]=ETileBlank;
}
}
TBool COandXController::HitSquareL(TInt aIndex)
{
// For Comms, replace this with another function, called
// when a tile is selected. It should refuse to accept
// the hit if it is not my move.
// Add another function, called when the opponent makes a
// move, and both can call this funtion (renamed from
// HitSquareL) The logic will need to be modified to handle
// the additional comms states and to report which of the
// two players wins the game (or when the game is drawn).
if (iState == EFinished)
{
return EFalse;
}
if (iState == ENewGame)
{
SetStateL(EPlaying);
}
if (Engine().TryMakeMove(aIndex,IsCrossTurn()))
{
SwitchTurn();
TTileState winner = Engine().GameWonBy();
if (winner)
{
iLastGameResult = winner;
SetStateL(EFinished);
OandXAppUi()->ReportWinnerL(winner);
}
return ETrue;
}
return EFalse;
}
void COandXController::SwitchTurn()
{
iCrossTurn = !iCrossTurn;
OandXAppUi()->ReportWhoseTurn();
}
TTileState COandXController::GameRecord(TInt aIndex)
{
return iGameRecords[aIndex];
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -