diff --git a/mazzGame/src/Config.cpp b/mazzGame/src/Config.cpp index dd882d4..a546e81 100644 --- a/mazzGame/src/Config.cpp +++ b/mazzGame/src/Config.cpp @@ -1,4 +1,8 @@ #include "Config.h" +#include +#include +#include +#include ConfigReader::ConfigReader() { @@ -8,22 +12,78 @@ ConfigReader::~ConfigReader() { } -void ConfigReader::load(std::string& path) +std::string ConfigReader::trim(const std::string &s) { + auto start = s.find_first_not_of(" \t\r\n"); + auto end = s.find_last_not_of(" \t\r\n"); + if (start == std::string::npos) + return ""; + return s.substr(start, end - start + 1); } -ConfigData::ConfigData() +bool ConfigReader::load(std::string &path) { -} + std::ifstream file(path); + if (!file.is_open()) + { + std::cerr << "无法打开配置文件: " << path << std::endl; + return false; + } -ConfigData::~ConfigData() -{ -} + std::string line, section; -void ConfigData::setSymbol(char wall, char empty, char player, char monster, char trap, char exit) -{ -} + while (std::getline(file, line)) + { + line = trim(line); -void ConfigData::getSymbol(char& wall, char& empty, char& player, char& monster, char& trap, char& exit) -{ + if (line.empty() || line[0] == ';' || line[0] == '#') + continue; + + // 识别 [Section] + if (line.front() == '[' && line.back() == ']') + { + section = line.substr(1, line.size() - 2); + continue; + } + + // 解析 Key = Value + size_t eq = line.find('='); + if (eq == std::string::npos) + continue; + + std::string key = trim(line.substr(0, eq)); + std::string value = trim(line.substr(eq + 1)); + + // 根据 section 分发 + if (section == "Symbols") + { + if (key == "Wall") + symbols.setWall(value[0]); + else if (key == "Empty") + symbols.setEmpty(value[0]); + else if (key == "Player") + symbols.setPlayer(value[0]); + else if (key == "Monster") + symbols.setMonster(value[0]); + else if (key == "Trap") + symbols.setTrap(value[0]); + else if (key == "Exit") + symbols.setExit(value[0]); + } + else if (section == "Difficulty") + { + if (key == "MonsterAI") + difficulty = std::stoi(value); + } + else if (section == "Maze") + { + if (key == "Width") + mazeWidth = std::stoi(value); + else if (key == "Height") + mazeHeight = std::stoi(value); + } + } + + file.close(); + return true; } diff --git a/mazzGame/src/Config.h b/mazzGame/src/Config.h index 2f2fe46..4669828 100644 --- a/mazzGame/src/Config.h +++ b/mazzGame/src/Config.h @@ -1,6 +1,6 @@ #pragma once -#include "MazeSymbol.h" +#include "MazeData.h" #include class ConfigReader @@ -9,29 +9,17 @@ public: ConfigReader(); ~ConfigReader(); - void load(std::string &path); + bool load(const std::string &path); + + MazeData getSymbols() const; + void setSymbols(MazeData &value); private: - MazeSymbol symbols; // 存放迷宫符号 + std::string trim(const std::string &s); + +private: + MazeData symbols; // 存放迷宫符号 int difficulty = 0; // 怪物AI难度 int mazeWidth = 21; int mazeHeight = 21; }; - -class ConfigData -{ -public: - ConfigData(); - ~ConfigData(); - - void setSymbol(char wall, char empty, char player, char monster, char trap, char exit); - void getSymbol(char &wall, char &empty, char &player, char &monster, char &trap, char &exit); - - void setDiffculty(int level); - int getDiffculty(); - -private: - char m_wall, m_empty, m_player, m_monster, m_trap, m_exit; - - int m_monsterAILevel; -}; diff --git a/mazzGame/src/Game.cpp b/mazzGame/src/Game.cpp index bac026f..04e9599 100644 --- a/mazzGame/src/Game.cpp +++ b/mazzGame/src/Game.cpp @@ -8,4 +8,14 @@ Game::~Game() { } - +void Game::loadConfig(const std::string &filePath, MazeData &data) +{ + m_config.setSymbols(data); + m_config.load(filePath); +} +void Game::initMaze() +{ +} +void Game::run() +{ +} diff --git a/mazzGame/src/Game.h b/mazzGame/src/Game.h index 896543c..efdc0f0 100644 --- a/mazzGame/src/Game.h +++ b/mazzGame/src/Game.h @@ -1,27 +1,23 @@ #pragma once #include "Config.h" +#include "MazeData.h" #include class Game { - private: + ConfigReader m_config; + MazeData m_mazeData; - ConfigReader m_config; - ConfigData m_configData; - - bool m_isRunning = true; - + bool m_isRunning = true; public: - Game(); - ~Game(); - - void loadConfig(const std::string& filePath,ConfigData& data); - void initMaze(ConfigData data); - void run(); + Game(); + ~Game(); + void loadConfig(const std::string &filePath, MazeData &data); + void initMaze(); + void run(); }; - diff --git a/mazzGame/src/Maze.cpp b/mazzGame/src/Maze.cpp index 2f3c5ef..493a59c 100644 --- a/mazzGame/src/Maze.cpp +++ b/mazzGame/src/Maze.cpp @@ -20,16 +20,16 @@ Maze::~Maze() { } -void Maze::setMazeSymbols(char wall, char empty, char player, char monster, char trap, char exit) +void Maze::setMazeData(MazeData &data) { } -void Maze::generate(int w, int h, const MazeSymbols &sym) +void Maze::generate(int w, int h, const MazeData &sym) { m_symbols = sym; m_width = (w % 2 == 0) ? w + 1 : w; m_height = (h % 2 == 0) ? h + 1 : h; - m_grid.assign(m_height, std::string(m_width, m_symbols.wall)); + m_grid.assign(m_height, std::string(m_width, m_symbols.getWall())); std::vector> cells(m_height, std::vector(m_width)); for (int y = 0; y < m_height; ++y) @@ -51,7 +51,7 @@ void Maze::generate(int w, int h, const MazeSymbols &sym) int sx = (startX(gen) / 2) * 2 + 1; int sy = (startY(gen) / 2) * 2 + 1; st.push({sx, sy, true}); - m_grid[sy][sx] = m_symbols.empty; + m_grid[sy][sx] = m_symbols.getEmpty(); // DFS生成 while (!st.empty()) @@ -67,8 +67,8 @@ void Maze::generate(int w, int h, const MazeSymbols &sym) if (inside(nx, ny) && !cells[ny][nx].visited) { cells[ny][nx].visited = true; - m_grid[ny][nx] = m_symbols.empty; - m_grid[c.y + dy / 2][c.x + dx / 2] = m_symbols.empty; + m_grid[ny][nx] = m_symbols.getEmpty(); + m_grid[c.y + dy / 2][c.x + dx / 2] = m_symbols.getEmpty(); st.push({nx, ny, true}); moved = true; break; @@ -80,16 +80,16 @@ void Maze::generate(int w, int h, const MazeSymbols &sym) } // 放置出口与陷阱 - m_grid[1][1] = m_symbols.empty; // 起点 - m_grid[m_height - 2][m_width - 2] = m_symbols.exit; // 出口 + m_grid[1][1] = m_symbols.getEmpty(); // 起点 + m_grid[m_height - 2][m_width - 2] = m_symbols.getExit(); // 出口 // 随机放置陷阱 std::uniform_int_distribution<> tx(1, m_width - 2), ty(1, m_height - 2); for (int i = 0; i < (m_width * m_height) / 80; ++i) { int x = tx(gen), y = ty(gen); - if (m_grid[y][x] == m_symbols.empty) - m_grid[y][x] = m_symbols.trap; + if (m_grid[y][x] == m_symbols.getEmpty()) + m_grid[y][x] = m_symbols.getTrap(); } } @@ -104,7 +104,7 @@ void Maze::draw(const Position &playerPos, const std::vector &monsterP // 绘制玩家 if (playerPos.x == x && playerPos.y == y) { - std::cout << m_symbols.player; + std::cout << m_symbols.getPlayer(); continue; } @@ -113,7 +113,7 @@ void Maze::draw(const Position &playerPos, const std::vector &monsterP { if (m.x == x && m.y == y) { - std::cout << m_symbols.monster; + std::cout << m_symbols.getMonster(); drawn = true; break; } @@ -132,15 +132,15 @@ bool Maze::isWalkable(const Position &p) const if (p.x < 0 || p.x >= m_width || p.y < 0 || p.y >= m_height) return false; char c = m_grid[p.y][p.x]; - return (c == m_symbols.empty || c == m_symbols.trap || c == m_symbols.exit); + return (c == m_symbols.getEmpty() || c == m_symbols.getTrap() || c == m_symbols.getExit()); } bool Maze::isExit(const Position &p) const { - return m_grid[p.y][p.x] == m_symbols.exit; + return m_grid[p.y][p.x] == m_symbols.getExit(); } bool Maze::checkTrap(const Position &p) const { - return m_grid[p.y][p.x] == m_symbols.trap; + return m_grid[p.y][p.x] == m_symbols.getTrap(); } diff --git a/mazzGame/src/Maze.h b/mazzGame/src/Maze.h index 766ac13..18eda92 100644 --- a/mazzGame/src/Maze.h +++ b/mazzGame/src/Maze.h @@ -1,38 +1,36 @@ #pragma once +#include "MazeData.h" + +#include #include -#include - -struct Position { - int x, y; - bool operator==(const Position& other) const { - return x == other.x && y == other.y; - } -}; - -// 用于存储字符符号 -struct MazeSymbols { - char wall, empty, player, monster, trap, exit; +struct Position +{ + int x, y; + bool operator==(const Position &other) const + { + return x == other.x && y == other.y; + } }; class Maze { public: - Maze(); - ~Maze(); + Maze(); + ~Maze(); - void setMazeSymbols(char wall, char empty, char player, char monster, char trap, char exit); + void setMazeData(MazeData &data); - void generate(int w, int h, const MazeSymbols& sym); - void draw(const Position& playerPos, const std::vector& monsterPos) const; + void generate(int w, int h, const MazeData &sym); + void draw(const Position &playerPos, const std::vector &monsterPos) const; - bool isWalkable(const Position& p) const; - bool isExit(const Position& p) const; - bool checkTrap(const Position& p) const; + bool isWalkable(const Position &p) const; + bool isExit(const Position &p) const; + bool checkTrap(const Position &p) const; private: - int m_width, m_height; - std::vector m_grid; - MazeSymbols m_symbols; + int m_width, m_height; + std::vector m_grid; + MazeData m_symbols; }; diff --git a/mazzGame/src/MazeData.cpp b/mazzGame/src/MazeData.cpp new file mode 100644 index 0000000..858844d --- /dev/null +++ b/mazzGame/src/MazeData.cpp @@ -0,0 +1,69 @@ +#include "MazeData.h" + +MazeData::MazeData() +{ +} + +MazeData::~MazeData() +{ +} + +char MazeData::getWall() const +{ + return wall; +} + +void MazeData::setWall(char value) +{ + wall = value; +} + +char MazeData::getEmpty() const +{ + return empty; +} + +void MazeData::setEmpty(char value) +{ + empty = value; +} + +char MazeData::getPlayer() const +{ + return player; +} + +void MazeData::setPlayer(char value) +{ + player = value; +} + +char MazeData::getMonster() const +{ + return monster; +} + +void MazeData::setMonster(char value) +{ + monster = value; +} + +char MazeData::getTrap() const +{ + return trap; +} + +void MazeData::setTrap(char value) +{ + trap = value; +} + +char MazeData::getExit() const +{ + return exit; +} + +void MazeData::setExit(char value) +{ + exit = value; +} \ No newline at end of file diff --git a/mazzGame/src/MazeData.h b/mazzGame/src/MazeData.h new file mode 100644 index 0000000..a3d6274 --- /dev/null +++ b/mazzGame/src/MazeData.h @@ -0,0 +1,40 @@ +#pragma once + +class MazeData +{ +public: + MazeData(); + ~MazeData(); + + char getWall() const; + + void setWall(char value); + + char getEmpty() const; + + void setEmpty(char value); + + char getPlayer() const; + + void setPlayer(char value); + + char getMonster() const; + + void setMonster(char value); + + char getTrap() const; + + void setTrap(char value); + + char getExit() const; + + void setExit(char value); + +private: + char wall; + char empty; + char player; + char monster; + char trap; + char exit; +}; diff --git a/mazzGame/src/MazeSymbol.cpp b/mazzGame/src/MazeSymbol.cpp deleted file mode 100644 index dfe644d..0000000 --- a/mazzGame/src/MazeSymbol.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include "MazeSymbol.h" - -MazeSymbol::MazeSymbol() {} - -MazeSymbol::~MazeSymbol() {} - -char MazeSymbol::getWall() const -{ - return wall; -} - -void MazeSymbol::setWall(char value) -{ - wall = value; -} - -char MazeSymbol::getEmpty() const -{ - return empty; -} - -void MazeSymbol::setEmpty(char value) -{ - empty = value; -} - -char MazeSymbol::getPlayer() const -{ - return player; -} - -void MazeSymbol::setPlayer(char value) -{ - player = value; -} - -char MazeSymbol::getMonster() const -{ - return monster; -} - -void MazeSymbol::setMonster(char value) -{ - monster = value; -} - -char MazeSymbol::getStrap() const -{ - return strap; -} - -void MazeSymbol::setStrap(char value) -{ - strap = value; -} - -char MazeSymbol::getExit() const -{ - return exit; -} - -void MazeSymbol::setExit(char value) -{ - exit = value; -} \ No newline at end of file diff --git a/mazzGame/src/MazeSymbol.h b/mazzGame/src/MazeSymbol.h deleted file mode 100644 index ed4f030..0000000 --- a/mazzGame/src/MazeSymbol.h +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once - -class MazeSymbol -{ -public: - MazeSymbol(); - ~MazeSymbol(); - - char getWall() const; - - void setWall(char value); - - char getEmpty() const; - - void setEmpty(char value); - - char getPlayer() const; - - void setPlayer(char value); - - char getMonster() const; - - void setMonster(char value); - - char getStrap() const; - - void setStrap(char value); - - char getExit() const; - - void setExit(char value); - -private: - char wall; - char empty; - char player; - char monster; - char strap; - char exit; - - -}; - diff --git a/mazzGame/src/cofig.ini b/mazzGame/src/cofig.ini index 35718cd..bd5699d 100644 --- a/mazzGame/src/cofig.ini +++ b/mazzGame/src/cofig.ini @@ -8,4 +8,8 @@ Empty = [Difficulty] # 0: 随机移动,1: BFS偏向,2: A*精确追踪 -MonsterAI = 1 \ No newline at end of file +MonsterAI = 1 + +[Maze] +Width = 21 +height = 21 \ No newline at end of file diff --git a/mazzGame/src/main.cpp b/mazzGame/src/main.cpp index d896855..969849d 100644 --- a/mazzGame/src/main.cpp +++ b/mazzGame/src/main.cpp @@ -2,11 +2,11 @@ int main() { - std::cout << "Hello CMake." << std::endl; + std::cout << "Hello CMake." << std::endl; - Game game; - game.loadConfig("config.txt"); - game.initMaze(); - game.run(); - return 0; + Game game; + game.loadConfig("config.txt"); + game.initMaze(); + game.run(); + return 0; }