diff --git a/AI/KBlocksAIPlayer.cpp b/AI/KBlocksAIPlayer.cpp index 9d64c10..cbded9b 100644 --- a/AI/KBlocksAIPlayer.cpp +++ b/AI/KBlocksAIPlayer.cpp @@ -1,389 +1,389 @@ /*************************************************************************** * KBlocks, a falling blocks game by KDE * * Copyright (C) 2010 University Freiburg * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * ***************************************************************************/ #include "KBlocksAIPlayer.h" #include "KBlocksAIEvaluation.h" #include "KBlocksAIFeature.h" #include "KBlocksAIPlannerExtend.h" #include "KBlocksAILog.h" #include "KBlocksAITypeDefine.h" #include "kblocks_ai_debug.h" #include #include typedef std::pair Result; typedef std::vector Result_Sequence; #define MAX_UTILITY // choose the goal with the highest utility //# PLANNER ###################################### #define TWO_PIECE_PLANNER //# SITUATION TREE ################################ #define SITUATION_MULTI_NARROW #define SITUATION_CRITIC_HEIGHT #define SITUATION_TERIS //#define SITUATION_SECTION_ANALYSIS //#define SITUATION_DEEP_NARROW //#define SITUATION_TALL_BLOCKS //# EVALUATION FEATURE ############################ //#define DIFFERENCE_EVALUATION //#define SECOND_EVALUATION //# EXTRA EVALUATION ############################# //#define SPECIAL_EVALUATION #ifdef SPECIAL_EVALUATION #define NBS_EVALUATION #endif -KBlocksAIPlayer::KBlocksAIPlayer(string name) +KBlocksAIPlayer::KBlocksAIPlayer(const string &name) { mAIName = name; mAIStarted = false; mAIPaused = false; mpAIField = 0; mpCurPiece = 0; mpNextPiece = 0; mpPlanner = 0; mNextCount = 0; mpEvaluatorFinal = 0; mpEvaluatorFirst = 0; mpEvaluatorPre = 0; mBestPosition = 0; mBestRotation = 0; } KBlocksAIPlayer::~KBlocksAIPlayer() { if (mAIStarted) { delete mpPlanner; delete mpNextPiece; delete mpCurPiece; delete mpAIField; } } void KBlocksAIPlayer::startGame(SingleGameInterface *p) { if (mAIStarted) { return; } mAIStarted = true; mpGame = p; mpAIField = new KBlocksField(mpGame->getField()); mpCurPiece = new KBlocksPiece(mpGame->getPiece(0)); mpNextPiece = new KBlocksPiece(mpGame->getPiece(1)); mpPlanner = new KBlocksAIPlannerExtend(mpAIField); } void KBlocksAIPlayer::stopGame() { if (!mAIStarted) { return; } delete mpPlanner; mpPlanner = 0; delete mpNextPiece; mpNextPiece = 0; delete mpCurPiece; mpCurPiece = 0; delete mpAIField; mpAIField = 0; mpGame = 0; mAIStarted = false; } void KBlocksAIPlayer::pauseGame(bool flag) { mAIPaused = flag; } void KBlocksAIPlayer::think(GamePlayer_ActionList *actionList) { if ((!mAIStarted) || (mAIPaused)) { return; } // Phase I - State Update update(); // Phase II - Planning planning(); // Phase III - Situation Analysis situationAdaption(); // Phase IV - States Evaluation generateGoalAction(); // set up action list if (mBestRotation > 0) { for (int i = 0; i < mBestRotation; i++) { actionList->push_back(PlayerAction_Rotate_CW); } } else { mBestRotation = -mBestRotation; for (int i = 0; i < mBestRotation; i++) { actionList->push_back(PlayerAction_Rotate_CCW); } } if (mBestPosition > 0) { for (int i = 0; i < mBestPosition; i++) { actionList->push_back(PlayerAction_Move_Right); } } else { mBestPosition = -mBestPosition; for (int i = 0; i < mBestPosition; i++) { actionList->push_back(PlayerAction_Move_Left); } } actionList->push_back(PlayerAction_Push_Down); } string KBlocksAIPlayer::getName() { return mAIName; } /****************************************************************** * THINK - DECIDE - CATCH *****************************************************************/ /* Phase I - State Update ----------------------------------*/ void KBlocksAIPlayer::update() { mpAIField->copy(mpGame->getField()); mpCurPiece->copy(mpGame->getPiece(0)); mpNextPiece->copy(mpGame->getPiece(1)); } /* Phase II - Planning ----------------------------------*/ void KBlocksAIPlayer::planning() { if (!mpPlanner) { qCWarning(KBlocksAI) << "No planner set for AI evaluation!"; return; } #ifdef TWO_PIECE_PLANNER AIPlanner_PieceValue_Sequence mPieceSequence = AIPlanner_PieceValue_Sequence(0); mPieceSequence.push_back(KBlocks_PieceType_Detail(mpCurPiece->toValue())); mPieceSequence.push_back(KBlocks_PieceType_Detail(mpNextPiece->toValue())); KBlocksAIPlannerExtend *extendedPlanner = dynamic_cast(mpPlanner); if (extendedPlanner != nullptr) { mNextCount = extendedPlanner->process(mPieceSequence); } #else KBlocks_PieceType_Detail type = KBlocks_PieceType_Detail(mpCurPiece->toValue()); mNextCount = mpPlanner->process(type); #endif } /* Phase III - Situation Analysis -------------------*/ void KBlocksAIPlayer::situationAdaption() { #ifndef SPECIAL_EVALUATION if (false) { } #ifdef SITUATION_MULTI_NARROW else if (getDecisionFeature(DF_PEEKS_COUNT, mpAIField)) { mpEvaluatorFinal = WellsFillerEvaluation::instance(); } #endif #ifdef SITUATION_CRITIC_HEIGHT else if (getDecisionFeature(DF_HEIGHT_MAX, mpAIField)) { mpEvaluatorFinal = HeightKillerEvaluation::instance(); } #endif #ifdef SITUATION_SECTION_ANALYSIS else if (getDecisionFeature(DF_LAYER_SCAN, mpAIField)) { mpEvaluatorFinal = HalfBaseEvaluation::instance(); } #endif #ifdef SITUATION_DEEP_NARROW else if (getDecisionFeature(DF_DEEP_NARROW, mpAIField)) { mpEvaluatorFinal = DeepNarrowRemoverEvaluation::instance(); } #endif #ifdef SITUATION_TALL_BLOCKS else if (getDecisionFeature(DF_BLOCK_SCAN, mpAIField)) { mpEvaluatorFinal = BlockRemoverEvaluation::instance(); } #endif #ifdef SITUATION_TERIS else if (getDecisionFeature(DF_CREATING_TETRIS, mpAIField)) { if (getDecisionFeature(DF_REMOVE_TETRIS, mpAIField)) { mpEvaluatorFinal = TetrisEliminationEvaluation::instance(); } else { mpEvaluatorFinal = TetrisPreparingEvaluation::instance(); } } #endif else { mpEvaluatorFinal = BaseEvaluation::instance(); } #ifdef DIFFERENCE_EVALUATION_ mpEvaluatorPre = mpEvaluatorFinal; #endif #ifdef SECOND_EVALUATION mpEvaluatorFirst = BaseEvaluation::instance(); #endif #else #ifdef NBS_EVALUATION mpEvaluatorFirst = 0; mpEvaluatorPre = 0; mpEvaluatorFinal = NBSEvaluation::instance(); #endif #endif //println(mpEvaluatorFinal->evaluationName()); } /* Phase IV - States Evaluation -------------------*/ void KBlocksAIPlayer::generateGoalAction() { double current_value = 0; if (mpEvaluatorPre != 0) { current_value = mpEvaluatorPre->evaluate(mpAIField); } double best_value = 0; Result_Sequence result = Result_Sequence(0); std::vector best_goals = std::vector(0); mBestRotation = 0; mBestPosition = 0; // FIRST STEP - ANALYZE FINAL BOARD_STATE if (mpEvaluatorFinal != 0) { bool firstValue = true; for (int i = 0; i < mNextCount; i++) { KBlocksField *field = new KBlocksField(mpAIField->getWidth(), mpAIField->getHeight()); mpPlanner->getNextBoardStatus(i, field); if (!field) { continue; } KBlocksPiece *piece = new KBlocksPiece(); mpPlanner->getNextPieceState(i, piece); double value = 0; #ifndef SPECIAL_EVALUATION value = mpEvaluatorFinal->evaluate(field) - current_value; #else #ifdef NBS_EVALUATION SpecialEvaluationInterface *evaluator = dynamic_cast(mpEvaluatorFinal); evaluator->setCurrentPiece(piece); value = evaluator->evaluate(field); #endif #endif result.push_back(Result(KBlocksPiece(piece), value)); #ifdef MAX_UTILITY bool better = (best_value < value); #else bool better = (best_value > value); #endif if (better || firstValue) { best_goals.clear(); best_value = value; firstValue = false; } // alternative found if (best_value == value) { best_goals.push_back(i); } delete piece; if (field) { delete field; } } } // NO SOLUTION FOUND if (best_goals.empty()) { return ; } //SECOND STEP #ifdef TWO_PIECE_PLANNER #ifdef SECOND_EVALUATION if (mpEvaluatorFirst != 0) { std::vector _best_goals = std::vector(0); double _best_value = 0; double firstValue = true; KBlocksAIPlannerExtend *_planner = (dynamic_cast(mpPlanner)); // double _current_value = current_value; for (int i = 0; i < best_goals.size(); i++) { int index = best_goals[i]; KBlocksField *field = new KBlocksField(); _planner->getNextBoardStatus(index, field, true); if (!field) { continue; } double value = mpEvaluatorFirst->evaluate(field); #ifdef MAX_UTILITY bool better = (_best_value < value); #else bool better = (_best_value > value); #endif if (better || firstValue) { _best_goals.clear(); _best_value = value; firstValue = false; better = true; } // alternative found if (better || (_best_value == value)) { _best_goals.push_back(index); } if (field) { delete field; } } best_goals = std::vector(_best_goals); } #endif #endif #ifdef INITLIST int goal_index = best_goals[0]; #else int goal_index = best_goals[rand() % best_goals.size()]; #endif KBlocksPiece *goal_piece = &(result[goal_index].first); mBestRotation = goal_piece->getRotation() - mpCurPiece->getRotation(); mBestPosition = goal_piece->getPosX() - mpCurPiece->getPosX(); } diff --git a/AI/KBlocksAIPlayer.h b/AI/KBlocksAIPlayer.h index 4dd80c6..01bf63a 100644 --- a/AI/KBlocksAIPlayer.h +++ b/AI/KBlocksAIPlayer.h @@ -1,73 +1,73 @@ /*************************************************************************** * KBlocks, a falling blocks game by KDE * * Copyright (C) 2010 University Freiburg * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * ***************************************************************************/ #ifndef KBLOCKSAIPLAYER_H #define KBLOCKSAIPLAYER_H #include using namespace std; #include "KBlocksAITypeDefine.h" #include "../GamePlayerInterface.h" #include "../SingleGameInterface.h" #include "../KBlocksField.h" #include "../KBlocksPiece.h" #include "EvaluationInterface.h" #include "PlannerInterface.h" class KBlocksAIPlayer : public GamePlayerInterface { public: - explicit KBlocksAIPlayer(string name = ""); + explicit KBlocksAIPlayer(const string &name = ""); ~KBlocksAIPlayer(); public: void startGame(SingleGameInterface *p); void stopGame(); void pauseGame(bool flag); void think(GamePlayer_ActionList *actionList); string getName(); private: string mAIName; // Private Control Data bool mAIStarted; bool mAIPaused; // Phase I - State Update KBlocksField *mpAIField; KBlocksPiece *mpCurPiece; KBlocksPiece *mpNextPiece; void update(); // Phase II - Planning PlannerInterface *mpPlanner; int mNextCount; void planning(); // Phase III - Situation Analysis EvaluationInterface *mpEvaluatorFinal; EvaluationInterface *mpEvaluatorFirst; EvaluationInterface *mpEvaluatorPre; void situationAdaption(); // Phase IV - States Evaluation int mBestPosition; int mBestRotation; void generateGoalAction(); }; #endif diff --git a/KBlocksConfigManager.cpp b/KBlocksConfigManager.cpp index 184129a..075c138 100644 --- a/KBlocksConfigManager.cpp +++ b/KBlocksConfigManager.cpp @@ -1,400 +1,400 @@ /*************************************************************************** * KBlocks, a falling blocks game by KDE * * Copyright (C) 2010 Zhongjie Cai * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * ***************************************************************************/ #include "KBlocksConfigManager.h" #include #include #include #include #include #include #include KBlocksConfigManager::KBlocksConfigManager() { stConfigSectionList.clear(); stConfigKeyNameList.clear(); stConfigDataTable.clear(); isDebug = false; } KBlocksConfigManager::~KBlocksConfigManager() { stConfigSectionList.clear(); stConfigKeyNameList.clear(); stConfigDataTable.clear(); } int KBlocksConfigManager::SetDebugOutput(bool flag) { isDebug = flag; return 0; } -int KBlocksConfigManager::LoadConfigFile(string filename) +int KBlocksConfigManager::LoadConfigFile(const string &filename) { FILE *fp; int result; fp = fopen(filename.c_str(), "r"); if (fp == NULL) { return -1; } result = ParseConfigFile(fp); fclose(fp); return result; } -int KBlocksConfigManager::SaveConfigFile(string filename) +int KBlocksConfigManager::SaveConfigFile(const string &filename) { FILE *fp; int result; fp = fopen(filename.c_str(), "w+"); if (fp == NULL) { return -1; } result = ConstructConfigFile(fp); fclose(fp); return result; } int KBlocksConfigManager::GetSectionCount() { return stConfigSectionList.size(); } -int KBlocksConfigManager::GetKeyCount(string SectionName) +int KBlocksConfigManager::GetKeyCount(const string &SectionName) { map< string, map >::iterator it; it = stConfigDataTable.find(SectionName); if (it == stConfigDataTable.end()) { return -1; } map tmpMap = it->second; return tmpMap.size(); } -int KBlocksConfigManager::GetKeyString(string SectionName, string KeyName, string *KeyString, const string Default) +int KBlocksConfigManager::GetKeyString(const string &SectionName, const string &KeyName, string *KeyString, const string &Default) { map< string, map >::iterator it; it = stConfigDataTable.find(SectionName); if (it == stConfigDataTable.end()) { *KeyString = Default; return -1; } map tmpMap = it->second; if (tmpMap.find(KeyName) == tmpMap.end()) { *KeyString = Default; return -1; } *KeyString = tmpMap[KeyName]; return 0; } -int KBlocksConfigManager::GetKeyInt(string SectionName, string KeyName, int *KeyInt, const int Default) +int KBlocksConfigManager::GetKeyInt(const string &SectionName, const string &KeyName, int *KeyInt, const int Default) { map< string, map >::iterator it; it = stConfigDataTable.find(SectionName); if (it == stConfigDataTable.end()) { *KeyInt = Default; return -1; } map tmpMap = it->second; if (tmpMap.find(KeyName) == tmpMap.end()) { *KeyInt = Default; return -1; } string tmpString = tmpMap[KeyName]; char *endptr; if (tmpString.find("0x") == tmpString.npos) { *KeyInt = strtol(tmpString.c_str(), &endptr, 10); } else { *KeyInt = strtol(tmpString.c_str(), &endptr, 16); } return 0; } -int KBlocksConfigManager::GetKeyBool(string SectionName, string KeyName, bool *KeyBool, const bool Default) +int KBlocksConfigManager::GetKeyBool(const string &SectionName, const string &KeyName, bool *KeyBool, const bool Default) { string tmpValue; if (GetKeyString(SectionName, KeyName, &tmpValue, "") == -1) { *KeyBool = Default; return -1; } transform(tmpValue.begin(), tmpValue.end(), tmpValue.begin(), (int(*)(int))tolower); if (tmpValue.find("true") != tmpValue.npos) { *KeyBool = true; } else if (tmpValue.find("false") != tmpValue.npos) { *KeyBool = false; } return 0; } int KBlocksConfigManager::SetKeyString(string SectionName, string KeyName, string KeyString) { map tmpKeyName; map< string, map< int, string > >::iterator it_keyname; map tmpKeyMap; map< string, map >::iterator it_data; it_data = stConfigDataTable.find(SectionName); if (it_data == stConfigDataTable.end()) { stConfigDataTable.insert(pair< string, map >(SectionName, tmpKeyMap)); stConfigSectionList.insert(pair(stConfigSectionList.size() + 1, SectionName)); stConfigKeyNameList.insert(pair< string, map >(SectionName, tmpKeyName)); } it_data = stConfigDataTable.find(SectionName); tmpKeyMap = it_data->second; if (tmpKeyMap.find(KeyName) == tmpKeyMap.end()) { it_keyname = stConfigKeyNameList.find(SectionName); tmpKeyName = it_keyname->second; tmpKeyName.insert(pair(tmpKeyName.size() + 1, KeyName)); stConfigKeyNameList[SectionName] = tmpKeyName; tmpKeyMap.insert(pair(KeyName, KeyString)); } else { tmpKeyMap[KeyName] = KeyString; } stConfigDataTable[SectionName] = tmpKeyMap; return 0; } int KBlocksConfigManager::SetKeyInt(string SectionName, string KeyName, int KeyInt) { map tmpKeyName; map< string, map< int, string > >::iterator it_keyname; map tmpKeyMap; map< string, map >::iterator it_data; it_data = stConfigDataTable.find(SectionName); if (it_data == stConfigDataTable.end()) { stConfigDataTable.insert(pair< string, map >(SectionName, tmpKeyMap)); stConfigSectionList.insert(pair(stConfigSectionList.size() + 1, SectionName)); stConfigKeyNameList.insert(pair< string, map >(SectionName, tmpKeyName)); } it_data = stConfigDataTable.find(SectionName); tmpKeyMap = it_data->second; string tmpString = int16tostring(KeyInt); if (tmpKeyMap.find(KeyName) == tmpKeyMap.end()) { it_keyname = stConfigKeyNameList.find(SectionName); tmpKeyName = it_keyname->second; tmpKeyName.insert(pair(tmpKeyName.size() + 1, KeyName)); stConfigKeyNameList[SectionName] = tmpKeyName; tmpKeyMap.insert(pair(KeyName, tmpString)); } else { tmpKeyMap[KeyName] = tmpString; } stConfigDataTable[SectionName] = tmpKeyMap; return 0; } -int KBlocksConfigManager::SetKeyBool(string SectionName, string KeyName, bool KeyBool) +int KBlocksConfigManager::SetKeyBool(const string &SectionName, const string &KeyName, bool KeyBool) { if (KeyBool) { return SetKeyString(SectionName, KeyName, "true"); } else { return SetKeyString(SectionName, KeyName, "false"); } } int KBlocksConfigManager::ParseConfigFile(FILE *fp) { map< string, map< string, string > >::iterator it_section; map tmpKeyName; map< string, map< int, string > >::iterator it_keyname; map tmpKeyMap; map< string, map< string, string > >::iterator it_data; stConfigSectionList.clear(); stConfigKeyNameList.clear(); stConfigDataTable.clear(); string curSection = "DefaultSection"; string curKey = "DefaultKey"; string curValue = "DefaultValue"; int lenth; string tmpString; char tmpBuff[1024]; while (fgets(tmpBuff, 1024, fp)) { switch (tmpBuff[0]) { case '[': tmpString = string(tmpBuff); lenth = tmpString.find(']'); curSection = tmpString.substr(1, lenth - 1); it_section = stConfigDataTable.find(curSection); if (it_section == stConfigDataTable.end()) { map tmpKeyMap; stConfigDataTable.insert(pair< string, map >(curSection, tmpKeyMap)); map tmpKeyName; stConfigKeyNameList.insert(pair< string, map >(curSection, tmpKeyName)); stConfigSectionList.insert(pair(stConfigSectionList.size() + 1, curSection)); if (isDebug) { printf("New section loaded <%s>\n", curSection.c_str()); } } else { if (isDebug) { printf("Existing section updated <%s>\n", curSection.c_str()); } } break; case '#': // skip this line break; case '\n': // skip this line break; case '\0': // skip this line break; default: tmpString = string(tmpBuff); lenth = tmpString.find('='); curKey = tmpString.substr(0, lenth); if (tmpString[tmpString.size() - 1] == '\n') { tmpString[tmpString.size() - 1] = '\0'; } curValue = tmpString.substr(lenth + 1); transform(curValue.begin(), curValue.end(), curValue.begin(), (int(*)(int))tolower); it_data = stConfigDataTable.find(curSection); if (it_data == stConfigDataTable.end()) { stConfigDataTable.insert(pair< string, map >(curSection, tmpKeyMap)); stConfigSectionList.insert(pair(stConfigSectionList.size() + 1, curSection)); stConfigKeyNameList.insert(pair< string, map >(curSection, tmpKeyName)); if (isDebug) { printf("New section added <%s>\n", curSection.c_str()); } } else { tmpKeyMap = it_data->second; } if (tmpKeyMap.find(curKey) == tmpKeyMap.end()) { it_keyname = stConfigKeyNameList.find(curSection); tmpKeyName = it_keyname->second; tmpKeyName.insert(pair(tmpKeyName.size() + 1, curKey)); stConfigKeyNameList[curSection] = tmpKeyName; if (isDebug) { printf("New Key & Value loaded <%s=%s> for section <%s>\n", curKey.c_str(), curValue.c_str(), curSection.c_str()); } tmpKeyMap.insert(pair(curKey, curValue)); } else { if (isDebug) { printf("Existing Key & Value updated <%s=%s> for section <%s>\n", curKey.c_str(), curValue.c_str(), curSection.c_str()); } tmpKeyMap[curKey] = curValue; } stConfigDataTable[curSection] = tmpKeyMap; break; } } return 0; } int KBlocksConfigManager::ConstructConfigFile(FILE *fp) { for (unsigned int i = 1; i < stConfigSectionList.size() + 1; i++) { string tmpSectionName = stConfigSectionList[i]; fprintf(fp, "[%s]\n", tmpSectionName.c_str()); map tmpKeyName = stConfigKeyNameList[tmpSectionName]; map tmpKeyMap = stConfigDataTable[tmpSectionName]; for (unsigned int j = 1; j < tmpKeyName.size() + 1; j++) { string tmpKeyNameStr = tmpKeyName[j]; string tmpKeyValueStr = tmpKeyMap[tmpKeyName[j]]; fprintf(fp, "%s=%s\n", tmpKeyNameStr.c_str(), tmpKeyValueStr.c_str()); } } return 0; } string KBlocksConfigManager::int16tostring(int input) { string tmpString = ""; char tmpChar[2] = {0, 0}; do { tmpChar[0] = input & 0x0F; input >>= 4; if (tmpChar[0] < 10) { tmpChar[0] += '0'; } else { tmpChar[0] += 'a' - 10; } tmpString = string(tmpChar) + tmpString; } while (input != 0); tmpString = "0x" + tmpString; return tmpString; } diff --git a/KBlocksConfigManager.h b/KBlocksConfigManager.h index 744c34b..6f72c1e 100644 --- a/KBlocksConfigManager.h +++ b/KBlocksConfigManager.h @@ -1,55 +1,55 @@ /*************************************************************************** * KBlocks, a falling blocks game by KDE * * Copyright (C) 2010 Zhongjie Cai * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * ***************************************************************************/ #ifndef KBLOCKSCONFIGMANAGER_H_INCLUDED #define KBLOCKSCONFIGMANAGER_H_INCLUDED #include #include #include using namespace std; class KBlocksConfigManager { private: map< int, string > stConfigSectionList; map< string, map< int, string > > stConfigKeyNameList; map< string, map< string, string > > stConfigDataTable; bool isDebug; public: KBlocksConfigManager(); ~KBlocksConfigManager(); int SetDebugOutput(bool flag); - int LoadConfigFile(string filename); - int SaveConfigFile(string filename); + int LoadConfigFile(const string &filename); + int SaveConfigFile(const string &filename); int GetSectionCount(); - int GetKeyCount(string SectionName); + int GetKeyCount(const string &SectionName); - int GetKeyString(string SectionName, string KeyName, string *KeyString, const string Default); - int GetKeyInt(string SectionName, string KeyName, int *KeyInt, const int Default); - int GetKeyBool(string SectionName, string KeyName, bool *KeyBool, const bool Default); + int GetKeyString(const string &SectionName, const string &KeyName, string *KeyString, const string &Default); + int GetKeyInt(const string &SectionName, const string &KeyName, int *KeyInt, const int Default); + int GetKeyBool(const string &SectionName, const string &KeyName, bool *KeyBool, const bool Default); int SetKeyString(string SectionName, string KeyName, string KeyString); int SetKeyInt(string SectionName, string KeyName, int KeyInt); - int SetKeyBool(string SectionName, string KeyName, bool KeyBool); + int SetKeyBool(const string &SectionName, const string &KeyName, bool KeyBool); private: int ParseConfigFile(FILE *fp); int ConstructConfigFile(FILE *fp); string int16tostring(int input); }; #endif // KBLOCKSCONFIGMANAGER_H_INCLUDED diff --git a/KBlocksKeyboardPlayer.cpp b/KBlocksKeyboardPlayer.cpp index 00b7dca..183d0a5 100644 --- a/KBlocksKeyboardPlayer.cpp +++ b/KBlocksKeyboardPlayer.cpp @@ -1,192 +1,192 @@ /*************************************************************************** * KBlocks, a falling blocks game by KDE * * Copyright (C) 2010 Zhongjie Cai * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * ***************************************************************************/ #include "KBlocksKeyboardPlayer.h" #include -KBlocksKeyboardPlayer::KBlocksKeyboardPlayer(KXmlGuiWindow *parent, string name, bool netMode) +KBlocksKeyboardPlayer::KBlocksKeyboardPlayer(KXmlGuiWindow *parent, const string &name, bool netMode) { mpGame = 0; mPauseFlag = false; mpKeyWindow = 0; mPlayerName = name; mNetMode = netMode; if (parent) { mpKeyShortcuts = parent->actionCollection(); } else { mpKeyWindow = new KXmlGuiWindow(); mpKeyWindow->setupGUI(); mpKeyWindow->setFixedWidth(192); mpKeyWindow->setFixedHeight(32); mpKeyWindow->show(); mpKeyShortcuts = mpKeyWindow->actionCollection(); } bindKeys(); mActionList.clear(); } KBlocksKeyboardPlayer::~KBlocksKeyboardPlayer() { delete mpKeyWindow; } void KBlocksKeyboardPlayer::startGame(SingleGameInterface *p) { mpGame = p; mPauseFlag = false; mActionList.clear(); } void KBlocksKeyboardPlayer::stopGame() { mpGame = 0; mActionList.clear(); } void KBlocksKeyboardPlayer::pauseGame(bool flag) { mPauseFlag = flag; } void KBlocksKeyboardPlayer::think(GamePlayer_ActionList *actionList) { if (mNetMode) { *actionList = mActionList; mActionList.clear(); } } string KBlocksKeyboardPlayer::getName() { return mPlayerName; } void KBlocksKeyboardPlayer::bindKeys() { rotatecw = mpKeyShortcuts->addAction(QStringLiteral("rotate_cw")); rotatecw->setText(i18n("Rotate Piece Clockwise")); rotatecw->setIcon(QIcon::fromTheme(QStringLiteral("object-rotate-right"))); mpKeyShortcuts->setDefaultShortcut(rotatecw, Qt::Key_Z); connect(rotatecw, &QAction::triggered, this, &KBlocksKeyboardPlayer::rotateCW); rotateccw = mpKeyShortcuts->addAction(QStringLiteral("rotate_ccw")); rotateccw->setText(i18n("Rotate Piece Counter Clockwise")); rotateccw->setIcon(QIcon::fromTheme(QStringLiteral("object-rotate-left"))); mpKeyShortcuts->setDefaultShortcut(rotateccw, Qt::Key_Up); connect(rotateccw, &QAction::triggered, this, &KBlocksKeyboardPlayer::rotateCCW); moveleft = mpKeyShortcuts->addAction(QStringLiteral("move_left")); moveleft->setText(i18n("Move Piece Left")); moveleft->setIcon(QIcon::fromTheme(QStringLiteral("arrow-left"))); mpKeyShortcuts->setDefaultShortcut(moveleft, Qt::Key_Left); connect(moveleft, &QAction::triggered, this, &KBlocksKeyboardPlayer::moveLeft); moveright = mpKeyShortcuts->addAction(QStringLiteral("move_right")); moveright->setText(i18n("Move Piece Right")); moveright->setIcon(QIcon::fromTheme(QStringLiteral("arrow-right"))); mpKeyShortcuts->setDefaultShortcut(moveright, Qt::Key_Right); connect(moveright, &QAction::triggered, this, &KBlocksKeyboardPlayer::moveRight); movedown = mpKeyShortcuts->addAction(QStringLiteral("move_down")); movedown->setText(i18n("Move Piece Down")); movedown->setIcon(QIcon::fromTheme(QStringLiteral("arrow-down"))); mpKeyShortcuts->setDefaultShortcut(movedown, Qt::Key_Down); connect(movedown, &QAction::triggered, this, &KBlocksKeyboardPlayer::moveDown); pushdown = mpKeyShortcuts->addAction(QStringLiteral("push_down")); pushdown->setText(i18n("Drop the Piece")); pushdown->setIcon(QIcon::fromTheme(QStringLiteral("arrow-down"))); mpKeyShortcuts->setDefaultShortcut(pushdown, Qt::Key_Space); connect(pushdown, &QAction::triggered, this, &KBlocksKeyboardPlayer::pushDown); } void KBlocksKeyboardPlayer::moveLeft() { if ((!mpGame) || (mPauseFlag)) { return; } if (mNetMode) { mActionList.push_back(PlayerAction_Move_Left); } else { mpGame->setCurrentPiece(-1, 0, 0); } emit blockMoved(); } void KBlocksKeyboardPlayer::moveRight() { if ((!mpGame) || (mPauseFlag)) { return; } if (mNetMode) { mActionList.push_back(PlayerAction_Move_Right); } else { mpGame->setCurrentPiece(1, 0, 0); } emit blockMoved(); } void KBlocksKeyboardPlayer::moveDown() { if ((!mpGame) || (mPauseFlag)) { return; } if (mNetMode) { mActionList.push_back(PlayerAction_Move_Down); } else { mpGame->setCurrentPiece(0, 1, 0); } emit blockMoved(); } void KBlocksKeyboardPlayer::pushDown() { if ((!mpGame) || (mPauseFlag)) { return; } if (mNetMode) { mActionList.push_back(PlayerAction_Push_Down); } else { while (mpGame->setCurrentPiece(0, 1, 0)) ; mpGame->forceUpdateGame(); } emit blockDropped(); } void KBlocksKeyboardPlayer::rotateCW() { if ((!mpGame) || (mPauseFlag)) { return; } if (mNetMode) { mActionList.push_back(PlayerAction_Rotate_CW); } else { mpGame->setCurrentPiece(0, 0, -1); } emit blockMoved(); } void KBlocksKeyboardPlayer::rotateCCW() { if ((!mpGame) || (mPauseFlag)) { return; } if (mNetMode) { mActionList.push_back(PlayerAction_Rotate_CCW); } else { mpGame->setCurrentPiece(0, 0, 1); } emit blockMoved(); } diff --git a/KBlocksKeyboardPlayer.h b/KBlocksKeyboardPlayer.h index 3f0afbc..608a406 100644 --- a/KBlocksKeyboardPlayer.h +++ b/KBlocksKeyboardPlayer.h @@ -1,78 +1,78 @@ /*************************************************************************** * KBlocks, a falling blocks game by KDE * * Copyright (C) 2010 Zhongjie Cai * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * ***************************************************************************/ #ifndef KBLOCKSKEYBOARDPLAYER_H #define KBLOCKSKEYBOARDPLAYER_H #include using namespace std; #include #include #include #include #include #include "GamePlayerInterface.h" class KBlocksKeyboardPlayer : public QObject, public GamePlayerInterface { Q_OBJECT public: - explicit KBlocksKeyboardPlayer(KXmlGuiWindow *parent, string name = "", bool netMode = false); + explicit KBlocksKeyboardPlayer(KXmlGuiWindow *parent, const string &name = "", bool netMode = false); ~KBlocksKeyboardPlayer(); public: void startGame(SingleGameInterface *p); void stopGame(); void pauseGame(bool flag); void think(GamePlayer_ActionList *actionList); string getName(); private: void bindKeys(); signals: void blockMoved(); void blockDropped(); private slots: void moveLeft(); void moveRight(); void moveDown(); void pushDown(); void rotateCW(); void rotateCCW(); protected: SingleGameInterface *mpGame; bool mPauseFlag; QAction *rotatecw; QAction *rotateccw; QAction *moveleft; QAction *moveright; QAction *movedown; QAction *pushdown; private: bool mNetMode; string mPlayerName; GamePlayer_ActionList mActionList; KXmlGuiWindow *mpKeyWindow; KActionCollection *mpKeyShortcuts; }; #endif diff --git a/KBlocksSound.cpp b/KBlocksSound.cpp index 4dbb1ae..fb380bd 100644 --- a/KBlocksSound.cpp +++ b/KBlocksSound.cpp @@ -1,64 +1,64 @@ /*************************************************************************** * KBlocks, a falling blocks game by KDE * * Copyright (C) 2009 Mauricio Piacentini * * Zhongjie Cai * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * ***************************************************************************/ //Uses routines from Kapman sound manager (game.cpp) #include "KBlocksSound.h" #include #include #include "kblocks_sound_debug.h" #include "settings.h" KBlocksSound::KBlocksSound() { m_blockFallSound = new KgSound(QStandardPaths::locate( - QStandardPaths::AppDataLocation, "sounds/block-fall.ogg")); + QStandardPaths::AppDataLocation, QStringLiteral("sounds/block-fall.ogg"))); m_blockMoveSound = new KgSound(QStandardPaths::locate( - QStandardPaths::AppDataLocation, "sounds/block-move.ogg")); + QStandardPaths::AppDataLocation, QStringLiteral("sounds/block-move.ogg"))); m_blockRemoveSound = new KgSound(QStandardPaths::locate( - QStandardPaths::AppDataLocation, "sounds/block-remove.ogg")); + QStandardPaths::AppDataLocation, QStringLiteral("sounds/block-remove.ogg"))); setSoundsEnabled(Settings::sounds()); } KBlocksSound::~KBlocksSound() { delete m_blockFallSound; delete m_blockMoveSound; delete m_blockRemoveSound; } void KBlocksSound::setSoundsEnabled(bool p_enabled) { sndActive = p_enabled; } void KBlocksSound::playSound(Sound soundType) { if (sndActive) { switch (soundType) { case Sound::BlockFall: m_blockFallSound->start(); break; case Sound::BlockMove: m_blockMoveSound->start(); break; case Sound::BlockRemove: m_blockRemoveSound->start(); break; default: qCWarning(KBSound) << "Unknown Sound requested for playback."; break; } } }