📄 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.
*/
{
Engine().Reset();
iState = ENewGame;
iCrossTurn = EFalse;
}
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);
}
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());
SetStateL(newState);
}
void COandXController::ResetL()
/**
Cancel the current game, clearing the board and setting
noughts as the current player.
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();
}
SetStateL(ENewGame);
}
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();
TInt winner = Engine().GameWonBy();
if (winner)
{
SetStateL(EFinished);
OandXAppUi()->ReportWinnerL(winner);
}
return ETrue;
}
return EFalse;
}
void COandXController::SwitchTurn()
{
iCrossTurn = !iCrossTurn;
OandXAppUi()->ReportWhoseTurn();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -