修复配置文件读取

main
InkSoul 2025-10-30 20:42:24 +08:00
parent fbbc6e20af
commit 9cf9534657
9 changed files with 70 additions and 8 deletions

5
.gitignore vendored 100644
View File

@ -0,0 +1,5 @@
out/
.vs/
.vscode/
build/
.cache/

7
mazzGame/.gitignore vendored
View File

@ -1,2 +1,5 @@
out/
.vs/
mazzGame/out/
.vs/
.vscode/
build/
.cache/

View File

@ -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)

View File

@ -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;
}

View File

@ -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);

View File

@ -1,5 +1,7 @@
#include "Game.h"
#include <iostream>
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;
}

View File

@ -3,6 +3,7 @@
#include "Config.h"
#include "MazeData.h"
#include <string>
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();
};

View File

@ -1,11 +1,16 @@
#include "main.h"
#include <windows.h>
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;