📄 oandxengine.cpp
字号:
// engine.cpp
//
// Copyright (c) 2000 - 2006 Symbian Ltd. All rights reserved.
#include <e32math.h>
#include "oandxdefs.h"
#include "oandxengine.h"
COandXEngine* COandXEngine::NewL()
/**
Factory function allocates new instance of COandXEngine.
@return New game engine, which is owned
by the caller. On return every
tile is marked as blank, which is
useful for rendering, although
a game has not been started yet.
*/
{
return new(ELeave) COandXEngine;
}
COandXEngine::COandXEngine()
/**
This constructor is only defined to ensure there is exactly
one definition.
*/
{
Reset();
}
COandXEngine::~COandXEngine()
/**
This destructor is only defined to ensure there is exactly
one definition.
*/
{
// empty.
}
void COandXEngine::Reset()
/**
Mark all tiles as blank. This is an engine-only operation;
it does not affect any comms connections or the UI.
*/
{
for (TInt i=0; i<KNumberOfTiles; i++)
{
iTileStates[i] = ETileBlank;
}
}
TTileState COandXEngine::TileStatus(TInt aIndex) const
/**
Return the indexed tile's state.
@param aIndex Zero-based tile index.
@return Tile state, i.e. ETileBlank,
ETileNought, or ETileCross.
*/
{
return iTileStates[aIndex];
}
TBool COandXEngine::TryMakeMove(TInt aIndex, TBool aCrossTurn)
/**
This function is called by the controller when the local or
remote player wants to select a tile. If the tile is blank,
it allows the move. Note this function does not know whose
turn it should be - that is the controller's responsibility.
@param aIndex Index of tile to update.
@param aCrossTurn Whether it is cross' or nought's turn.
@return Whether the board was updated.
*/
{
if (iTileStates[aIndex] == ETileBlank)
{
iTileStates[aIndex] = aCrossTurn ? ETileCross : ETileNought;
return ETrue;
}
return EFalse;
}
TInt COandXEngine::TileState(TInt aX, TInt aY) const
/**
Get the state of the tile at the supplied coordinates.
@param aX Tile X coordinate.
@param aY Tile Y coordinate.
@return Value of tile at (aX, aY).
*/
{
ASSERT(aX >= 0 && aX < KTilesPerSide);
ASSERT(aY >= 0 && aY < KTilesPerSide);
return iTileStates[aY * KTilesPerSide + aX];
}
TTileState COandXEngine::GameWonBy() const
/**
Check if there is a full line of noughts or crosses.
The line can be horizontal, vertical, or diagonal.
@return ETileNought if there is a line of noughts;
ETileCross if there is a line of crosses;
Zero if there is no complete line.
*/
{
const TInt KNoughtWinSum = KTilesPerSide * ETileNought;
const TInt KCrossWinSum = KTilesPerSide * ETileCross;
// is there a row or column of matching tiles?
for (TInt i = 0; i < KTilesPerSide; ++i)
{
TInt rowSum = 0;
TInt colSum = 0;
for (TInt j = 0; j < KTilesPerSide; ++j)
{
rowSum += TileState(j, i);
colSum += TileState(i, j);
}
if (rowSum == KNoughtWinSum || colSum == KNoughtWinSum)
{
return ETileNought;
}
if (rowSum == KCrossWinSum || colSum == KCrossWinSum)
{
return ETileCross;
}
}
// is there a diagonal of matching tiles?
TInt blTrSum = 0; // bottom left to top right
TInt tlBrSum = 0; // top left to bottom right
for (TInt i = 0; i < KTilesPerSide; ++i)
{
tlBrSum += TileState(i,i);
blTrSum += TileState(i,KTilesPerSide - 1 - i);
}
if (blTrSum == KNoughtWinSum || tlBrSum == KNoughtWinSum)
{
return ETileNought;
}
if (blTrSum == KCrossWinSum || tlBrSum == KCrossWinSum)
{
return ETileCross;
}
return ETileBlank; // No winner
}
void COandXEngine::ExternalizeL(RWriteStream& aStream) const
/**
Externalize the board to the supplied stream.
@param aStream Stream to which the board's state
will be written.
@see InternalizeL
*/
{
for (TInt i = 0; i<KNumberOfTiles; i++)
{
aStream.WriteInt8L(iTileStates[i]);
}
}
void COandXEngine::InternalizeL(RReadStream& aStream)
/**
Internalize the board from the supplied stream.
@param aStream Stream which contains externalized
board state.
@see ExternalizeL
*/
{
for (TInt i = 0; i<KNumberOfTiles; i++)
{
iTileStates[i] = static_cast<TTileState>(aStream.ReadInt8L());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -