⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bosseng.cpp

📁 Symbian C++ scmp.zip
💻 CPP
字号:
// bosseng.cpp
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

#include "bosseng.h"

#include <s32std.h>

// panic wrapper

enum
	{
	ERowOutOfRange,
	EColOutOfRange,
	ETileOutOfRange,
	EBadArrangement,
	EBadMove,
	};

void Panic(TInt aPanic)
	{
	_LIT(KBossEnginePanic,"BossEngine");// constant declaration
	User::Panic(KBossEnginePanic,aPanic);
	}

// construct

EXPORT_C TBossPuzzle::TBossPuzzle()
	{
	SetFullyOrdered();
	}

// initialize state

EXPORT_C void TBossPuzzle::SetFullyOrdered()
	{
	for (TInt i=0; i<15; i++)
		iTiles[i]=i+1;
	iTiles[15]=0;
	}

EXPORT_C void TBossPuzzle::SetBossOrdered()
	{
	SetFullyOrdered();
	iTiles[13]=15;
	iTiles[14]=14;
	}

// inquiry

EXPORT_C TBool TBossPuzzle::IsFullyOrdered() const
	{
	for (TInt i=0; i<15; i++)
		if (iTiles[i]!=i+1)
			return EFalse;
	if (iTiles[15]!=0)
		return EFalse;
	return ETrue;
	}

EXPORT_C TBool TBossPuzzle::IsBlank(TInt aRow, TInt aCol) const
	{
	return Tile(aRow, aCol)==0;
	}

EXPORT_C const TInt& TBossPuzzle::Tile(TInt aRow, TInt aCol) const
	{
	__ASSERT_ALWAYS(aRow>=0 && aRow<4, Panic(ERowOutOfRange));
	__ASSERT_ALWAYS(aCol>=0 && aCol<4, Panic(EColOutOfRange));
	return iTiles[aRow*4 + aCol];
	}

EXPORT_C TInt& TBossPuzzle::Tile(TInt aRow, TInt aCol)
	{
	__ASSERT_ALWAYS(aRow>=0 && aRow<4, Panic(ERowOutOfRange));
	__ASSERT_ALWAYS(aCol>=0 && aCol<4, Panic(EColOutOfRange));
	return iTiles[aRow*4 + aCol];
	}

EXPORT_C void TBossPuzzle::LocateTile(TInt aTileNumber, TInt& aRow, TInt& aCol) const
	{
	__ASSERT_ALWAYS(aTileNumber>=1 && aTileNumber<=15, Panic(ETileOutOfRange));
	for (TInt row=0; row<4; row++)
		for (TInt col=0; col<4; col++)
			if (Tile(row, col)==aTileNumber)
				{
				aRow=row;
				aCol=col;
				return;
				}
	Panic(EBadArrangement);
	}

EXPORT_C void TBossPuzzle::LocateBlank(TInt& aRow, TInt& aCol) const
	{
	for (TInt row=0; row<4; row++)
		for (TInt col=0; col<4; col++)
			if (Tile(row, col)==0)
				{
				aRow=row;
				aCol=col;
				return;
				}
	Panic(EBadArrangement);
	}

// whether various moves are possible

EXPORT_C TBool TBossPuzzle::CanMove(TMoveType aMoveType) const
	{
	TInt blankRow, blankCol;
	LocateBlank(blankRow, blankCol);
	blankRow += aMoveType==EUp ? 1 : aMoveType==EDown ? -1 : 0;
	blankCol += aMoveType==ELeft ? 1 : aMoveType==ERight ? -1 : 0;
	return blankRow>=0 && blankRow<4 && blankCol>=0 && blankCol<4;
	}

EXPORT_C TBool TBossPuzzle::CanMove(TInt aRow, TInt aCol) const
	{
	TInt blankRow, blankCol;
	LocateBlank(blankRow, blankCol);
	return
		aRow==blankRow && aCol==blankCol+1 ||
		aRow==blankRow && aCol==blankCol-1 ||
		aCol==blankCol && aRow==blankRow+1 ||
		aCol==blankCol && aRow==blankRow-1;
	}

EXPORT_C TBossPuzzle::TMoveType TBossPuzzle::CanMoveType(TInt aRow, TInt aCol) const
	{
	__ASSERT_ALWAYS(CanMove(aRow, aCol), Panic(EBadMove));
	TInt blankRow, blankCol;
	LocateBlank(blankRow, blankCol);
	return
		aRow==blankRow && aCol==blankCol+1 ? ELeft :
		aRow==blankRow && aCol==blankCol-1 ? ERight :
		aCol==blankCol && aRow==blankRow+1 ? EUp :
		EDown;
	}

// moving - these functions panic if impossible

EXPORT_C void TBossPuzzle::Move(TMoveType aMoveType)
	{
	// check it's ok
	__ASSERT_ALWAYS(CanMove(aMoveType), Panic(EBadMove));
	// get current blank position and calculate new one
	TInt blankRow, blankCol;
	LocateBlank(blankRow, blankCol);
	TInt newBlankRow = blankRow + (aMoveType==EUp ? 1 : aMoveType==EDown ? -1 : 0);
	TInt newBlankCol = blankCol + (aMoveType==ELeft ? 1 : aMoveType==ERight ? -1 : 0);
	// do the move
	Tile(blankRow, blankCol)=Tile(newBlankRow,newBlankCol); // move tile to present blank
	Tile(newBlankRow, newBlankCol)=0; // move blank to new position
	}

EXPORT_C void TBossPuzzle::Move(TInt aRow, TInt aCol)
	{
	Move(CanMoveType(aRow, aCol));
	}

// stream persistence

EXPORT_C void TBossPuzzle::ExternalizeL(RWriteStream& aStream) const
	{
	for (TInt i=0; i<16; i++)
		aStream.WriteInt8L(iTiles[i]);
	}

EXPORT_C void TBossPuzzle::InternalizeL(RReadStream& aStream)
	{
	for (TInt i=0; i<16; i++)
		iTiles[i]=aStream.ReadInt8L();
	}

// store and restore

EXPORT_C TStreamId TBossPuzzle::StoreL(CStreamStore& aStore) const
	{
	RStoreWriteStream stream;
	TStreamId id = stream.CreateLC(aStore);
	ExternalizeL(stream);
	stream.CommitL();
	CleanupStack::PopAndDestroy(); // stream
	return id;
	}

EXPORT_C void TBossPuzzle::RestoreL(const CStreamStore& aStore, TStreamId aStreamId)
	{
	RStoreReadStream stream;
	stream.OpenLC(aStore,aStreamId);
	InternalizeL(stream);
	CleanupStack::PopAndDestroy(); // stream
	}

// requirement for E32 DLLs

EXPORT_C TInt E32Dll(TDllReason)
	{
	return 0;
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -