📄 bosstui.cpp
字号:
// bosstui.cpp
//
// Copyright (c) 1998 Symbian Ltd. All rights reserved.
//
#include "bosseng.h"
#include "tuiedit.h"
#include <s32file.h>
/*
class CBossTextUi - defines text UI
*/
class CBossTextUi : public CBase
{
public:
// execute UI until exit command processed
void ExecuteL();
private:
// destructor
~CBossTextUi();
// other functions
void Draw();
void DoCommandL();
// command handlers
void Help();
void NewGameFullyOrdered();
void NewGameBossOrdered();
void Move(TBossPuzzle::TMoveType aMoveType);
void SetPlayerNameL();
void SaveGameL();
void LoadGameL();
private:
TBossPuzzle iPuzzle;
CConsoleBase* iConsole;
CLineEdit* iCommandLine;
TBuf<80> iCommand;
HBufC* iPlayerName;
};
/*
ok, real stuff starts here
*/
void CBossTextUi::ExecuteL()
{
//text constant declarations
_LIT(KBossPuzzleTextUI,"Boss Puzzle Text UI");
_LIT(KPrompt,">");
iConsole=Console::NewL(KBossPuzzleTextUI,
TSize(KConsFullScreen,KConsFullScreen));
iCommandLine=CLineEdit::NewL(iConsole,10);
iPuzzle.SetFullyOrdered();
for (;;)
{
iCommandLine->Edit(KPrompt, &iCommand);
DoCommandL();
}
}
CBossTextUi::~CBossTextUi()
{
delete iPlayerName;
delete iCommandLine;
delete iConsole;
}
void CBossTextUi::Draw()
{
// constant declarations
_LIT(KAppendFormat,"%2d");
_LIT(KRowFormat,"%S\n");
_LIT(KBlankZapper,"--");
// start by formatting the rows with tile number, or 00 for blank
TBuf<11> rows[4];
TInt row;
TInt col;
for (row=0; row<4; row++)
{
rows[row].Zero();
for (col=0; col<4; col++)
{
if (col>0) rows[row].Append(' ');
rows[row].AppendFormat(KAppendFormat, (TInt) iPuzzle.Tile(row,col));
}
}
// find blank position, and replace number with symbol
TInt blankRow, blankCol;
iPuzzle.LocateBlank(blankRow, blankCol);
TPtr blankZapper((TText*)(rows[blankRow].Ptr())+3*blankCol, 2);
blankZapper=KBlankZapper;
// write out the strings to the console
for (row=0; row<4; row++)
iConsole->Printf(KRowFormat, &(rows[row]));
}
void CBossTextUi::DoCommandL()
{
_LIT(KUnknownCmnd,"unknown command\n"); // constant declaration
if (iCommand.Length()==0); // nothing
else if (iCommand[0]=='q') User::Leave(KErrNone);
else if (iCommand[0]=='h') Help();
else if (iCommand[0]=='v') Draw();
else if (iCommand[0]=='u') Move(TBossPuzzle::EUp);
else if (iCommand[0]=='d') Move(TBossPuzzle::EDown);
else if (iCommand[0]=='l') Move(TBossPuzzle::ELeft);
else if (iCommand[0]=='r') Move(TBossPuzzle::ERight);
else if (iCommand[0]=='n') NewGameFullyOrdered();
else if (iCommand[0]=='b') NewGameBossOrdered();
else if (iCommand[0]=='p') SetPlayerNameL();
else if (iCommand[0]=='x') SaveGameL();
else if (iCommand[0]=='i') LoadGameL();
else
{
iConsole->Printf(KUnknownCmnd);
}
}
void CBossTextUi::Help()
{
//constant declarations
_LIT(KHelpMove,"u, d, l, r - up, down, left, right\n");
_LIT(KHelpView,"v - view board position\n");
_LIT(KHelpHelp,"h - help\n");
_LIT(KHelpNewFullOrder,"n - new, fully ordered\n");
_LIT(KHelpNewBossOrder,"b - new, Boss ordered\n");
_LIT(KHelpSetPlayer,"pname - set player name\n");
_LIT(KHelpSaveFile,"x - save to file identified by player name\n");
_LIT(KHelpLoadFile,"i - load from file identified by player name\n");
_LIT(KHelpQuit,"q - quit\n");
// print help information
iConsole->Printf(KHelpMove);
iConsole->Printf(KHelpView);
iConsole->Printf(KHelpHelp);
iConsole->Printf(KHelpNewFullOrder);
iConsole->Printf(KHelpNewBossOrder);
iConsole->Printf(KHelpSetPlayer);
iConsole->Printf(KHelpSaveFile);
iConsole->Printf(KHelpLoadFile);
iConsole->Printf(KHelpQuit);
}
void CBossTextUi::NewGameFullyOrdered()
{
_LIT(KNewGameFO,"new game - fully ordered\n"); //constant declaration
iConsole->Printf(KNewGameFO);
iPuzzle.SetFullyOrdered();
Draw();
}
void CBossTextUi::NewGameBossOrdered()
{
_LIT(KNewGameBO,"new game - Boss ordered\n");
iConsole->Printf(KNewGameBO);
iPuzzle.SetBossOrdered();
Draw();
}
void CBossTextUi::Move(TBossPuzzle::TMoveType aMoveType)
{
// constant declarations
_LIT(KTextUp,"up");
_LIT(KTextDown,"down");
_LIT(KTextLeft,"left");
_LIT(KTextRight,"right");
_LIT(KTextInDir,"in that direction");
_LIT(KTextNtMove,"cannot move %S\n");
_LIT(KTextGameOvr,"game over - congratulations!\n");
if (!iPuzzle.CanMove(aMoveType))
{
TPtrC direction=
aMoveType==TBossPuzzle::EUp ? TPtrC(KTextUp) :
aMoveType==TBossPuzzle::EDown ? TPtrC(KTextDown) :
aMoveType==TBossPuzzle::ELeft ? TPtrC(KTextLeft) :
aMoveType==TBossPuzzle::ERight ? TPtrC(KTextRight) :
TPtrC(KTextInDir);
iConsole->Printf(KTextNtMove, &direction);
return;
}
iPuzzle.Move(aMoveType);
Draw();
if (iPuzzle.IsFullyOrdered())
{
iConsole->Printf(KTextGameOvr);
}
}
// saving and loading
void CBossTextUi::SetPlayerNameL()
{
// constant declarations
_LIT(KNoPlayerSet,"no player name\n");
_LIT(KPlayerIs,"player name is %S\n");
_LIT(KPlayerSetTo,"player name set to %S\n");
// if no player name specified, say what's currently set
if (iCommand.Length()==1)
{
if (!iPlayerName)
iConsole->Printf(KNoPlayerSet);
else
iConsole->Printf(KPlayerIs, iPlayerName);
return;
}
// otherwise set name
TPtrC name(iCommand.Ptr()+1, iCommand.Length()-1); // rest of command is name
delete iPlayerName; // delete old name buffer
iPlayerName=0; // zero pointer, just in case
iPlayerName=name.AllocL(); // re-allocate buffer and copy in new name
iConsole->Printf(KPlayerSetTo, iPlayerName);
}
void CBossTextUi::SaveGameL()
{
//constant declarations
_LIT(KCannotSave,"cannot save - no player name set\n");
_LIT(KGameSavedTo,"game saved to %S\n");
if (!iPlayerName)
{
iConsole->Printf(KCannotSave);
return;
}
TParse fileStoreName;
RFs fsSession;
User::LeaveIfError(fsSession.Connect());
fsSession.Parse(*iPlayerName,fileStoreName);
CFileStore* store = CDirectFileStore::ReplaceLC(fsSession,fileStoreName.FullName(),EFileWrite);
store->SetTypeL(KDirectFileStoreLayoutUid);
TStreamId id=iPuzzle.StoreL(*store);
store->SetRootL(id);
store->CommitL();
CleanupStack::PopAndDestroy(); // store
iConsole->Printf(KGameSavedTo, iPlayerName);
}
void CBossTextUi::LoadGameL()
{
// constant declarations
_LIT(KCannotLoad,"cannot load - no player name set\n");
_LIT(KGameLoadedFrom,"game loaded from %S\n");
if (!iPlayerName)
{
iConsole->Printf(KCannotLoad);
return;
}
TParse fileStoreName;
RFs fsSession;
User::LeaveIfError(fsSession.Connect());
fsSession.Parse(*iPlayerName,fileStoreName);
CFileStore* store = CDirectFileStore::OpenLC(fsSession,fileStoreName.FullName(),EFileRead);
iPuzzle.RestoreL(*store, store->Root()); // restore from root stream
CleanupStack::PopAndDestroy(); // store
iConsole->Printf(KGameLoadedFrom, iPlayerName);
Draw();
}
/*
harness
*/
LOCAL_C void executeUiL()
{
CBossTextUi* textUi=new (ELeave) CBossTextUi;
CleanupStack::PushL(textUi);
textUi->ExecuteL();
CleanupStack::PopAndDestroy();
}
GLDEF_C TInt E32Main() // main function called by E32
{
__UHEAP_MARK; // mark heap state
_LIT(KBossPuzzlePanic,"BossPuzzle"); //constant declaration
CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack
TRAPD(error,executeUiL()); // do most stuff under cleanup stack
__ASSERT_ALWAYS(!error,User::Panic(KBossPuzzlePanic,error));
delete cleanup; // destroy clean-up stack
__UHEAP_MARKEND; // check no memory leak
return 0; // and return
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -