From 9cf9534657296462bffd048bae3892c5939f7eb5 Mon Sep 17 00:00:00 2001 From: InkSoul Date: Thu, 30 Oct 2025 20:42:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E8=AF=BB=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 5 +++++ mazzGame/.gitignore | 7 +++++-- mazzGame/CMakeLists.txt | 6 +++++- mazzGame/src/Config.cpp | 24 +++++++++++++++++++++++- mazzGame/src/Config.h | 4 ++++ mazzGame/src/Game.cpp | 21 +++++++++++++++++++-- mazzGame/src/Game.h | 4 +++- mazzGame/src/{cofig.ini => config.ini} | 0 mazzGame/src/main.cpp | 7 ++++++- 9 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 .gitignore rename mazzGame/src/{cofig.ini => config.ini} (100%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a3b1b56 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +out/ +.vs/ +.vscode/ +build/ +.cache/ \ No newline at end of file diff --git a/mazzGame/.gitignore b/mazzGame/.gitignore index 329817a..5b8dd9e 100644 --- a/mazzGame/.gitignore +++ b/mazzGame/.gitignore @@ -1,2 +1,5 @@ -out/ -.vs/ \ No newline at end of file +mazzGame/out/ +.vs/ +.vscode/ +build/ +.cache/ \ No newline at end of file diff --git a/mazzGame/CMakeLists.txt b/mazzGame/CMakeLists.txt index a00aa7d..253e83b 100644 --- a/mazzGame/CMakeLists.txt +++ b/mazzGame/CMakeLists.txt @@ -13,8 +13,12 @@ project ("mazzGame") aux_source_directory(${PROJECT_SOURCE_DIR}/src sourceCodeList) + + # 将源代码添加到此项目的可执行文件。 -add_executable (mazzGame ${sourceCodeList} "src/Maze.cpp") +add_executable (mazzGame ${sourceCodeList}) + +install(FILES ${CMAKE_SOURCE_DIR}/src/config.ini DESTINATION ${CMAKE_BINARY_DIR}/x64-debug/config.ini ) if (CMAKE_VERSION VERSION_GREATER 3.12) set_property(TARGET mazzGame PROPERTY CXX_STANDARD 20) diff --git a/mazzGame/src/Config.cpp b/mazzGame/src/Config.cpp index a546e81..50854fb 100644 --- a/mazzGame/src/Config.cpp +++ b/mazzGame/src/Config.cpp @@ -12,6 +12,15 @@ ConfigReader::~ConfigReader() { } +MazeData ConfigReader::getSymbols() const +{ + return symbols; +} +void ConfigReader::setSymbols(MazeData &value) +{ + symbols = value; +} + std::string ConfigReader::trim(const std::string &s) { auto start = s.find_first_not_of(" \t\r\n"); @@ -21,7 +30,7 @@ std::string ConfigReader::trim(const std::string &s) return s.substr(start, end - start + 1); } -bool ConfigReader::load(std::string &path) +bool ConfigReader::load(const std::string &path) { std::ifstream file(path); if (!file.is_open()) @@ -87,3 +96,16 @@ bool ConfigReader::load(std::string &path) file.close(); return true; } + +int ConfigReader::getDifficulty() +{ + return difficulty; +} +int ConfigReader::getMazeHeight() +{ + return mazeHeight; +} +int ConfigReader::getMazeWidth() +{ + return mazeWidth; +} \ No newline at end of file diff --git a/mazzGame/src/Config.h b/mazzGame/src/Config.h index 4669828..42ea1b2 100644 --- a/mazzGame/src/Config.h +++ b/mazzGame/src/Config.h @@ -14,6 +14,10 @@ public: MazeData getSymbols() const; void setSymbols(MazeData &value); + int getDifficulty(); + int getMazeHeight(); + int getMazeWidth(); + private: std::string trim(const std::string &s); diff --git a/mazzGame/src/Game.cpp b/mazzGame/src/Game.cpp index 04e9599..062eac7 100644 --- a/mazzGame/src/Game.cpp +++ b/mazzGame/src/Game.cpp @@ -1,5 +1,7 @@ #include "Game.h" +#include + Game::Game() { } @@ -8,9 +10,9 @@ Game::~Game() { } -void Game::loadConfig(const std::string &filePath, MazeData &data) +void Game::loadConfig(const std::string &filePath) { - m_config.setSymbols(data); + m_config.setSymbols(m_mazeData); m_config.load(filePath); } void Game::initMaze() @@ -19,3 +21,18 @@ void Game::initMaze() void Game::run() { } + +void Game::printConfig() +{ + auto sym = m_config.getSymbols(); + std::cout << "符号配置:\n"; + std::cout << "墙壁: " << sym.getWall() << "\n"; + std::cout << "空地: " << sym.getEmpty() << "\n"; + std::cout << "玩家: " << sym.getPlayer() << "\n"; + std::cout << "怪物: " << sym.getMonster() << "\n"; + std::cout << "陷阱: " << sym.getTrap() << "\n"; + std::cout << "出口: " << sym.getExit() << "\n"; + + std::cout << "\n迷宫大小: " << m_config.getMazeWidth() << "x" << m_config.getMazeHeight() << "\n"; + std::cout << "怪物AI难度: " << m_config.getDifficulty() << std::endl; +} \ No newline at end of file diff --git a/mazzGame/src/Game.h b/mazzGame/src/Game.h index efdc0f0..de48176 100644 --- a/mazzGame/src/Game.h +++ b/mazzGame/src/Game.h @@ -3,6 +3,7 @@ #include "Config.h" #include "MazeData.h" + #include class Game @@ -17,7 +18,8 @@ public: Game(); ~Game(); - void loadConfig(const std::string &filePath, MazeData &data); + void loadConfig(const std::string &filePath); + void printConfig(); void initMaze(); void run(); }; diff --git a/mazzGame/src/cofig.ini b/mazzGame/src/config.ini similarity index 100% rename from mazzGame/src/cofig.ini rename to mazzGame/src/config.ini diff --git a/mazzGame/src/main.cpp b/mazzGame/src/main.cpp index 969849d..e658917 100644 --- a/mazzGame/src/main.cpp +++ b/mazzGame/src/main.cpp @@ -1,11 +1,16 @@ #include "main.h" +#include + int main() { + + SetConsoleOutputCP(CP_UTF8); + SetConsoleCP(CP_UTF8); std::cout << "Hello CMake." << std::endl; Game game; - game.loadConfig("config.txt"); + game.loadConfig("F:\\mazzGame\\mazzGame\\src\\config.ini"); game.initMaze(); game.run(); return 0;