更新配置文件读取

main v0.1
InkSoul 2025-11-01 14:28:47 +08:00
parent 9cf9534657
commit c397684ef8
4 changed files with 26 additions and 16 deletions

View File

@ -1,7 +1,10 @@
#include "Config.h"
#include "Config.h"
#include <algorithm>
#include <fstream>
#include <iostream>
#include <fstream>
#include <string>
#include <locale>
#include <codecvt>
#include <sstream>
ConfigReader::ConfigReader()
@ -32,7 +35,9 @@ std::string ConfigReader::trim(const std::string &s)
bool ConfigReader::load(const std::string &path)
{
std::ifstream file(path);
file.imbue(std::locale(std::locale(), new std::codecvt_utf8<wchar_t>));
if (!file.is_open())
{
std::cerr << "无法打开配置文件: " << path << std::endl;

View File

@ -1,4 +1,4 @@
#include "Game.h"
#include "Game.h"
#include <iostream>
@ -25,14 +25,17 @@ 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 << "符号配置:" << std::endl;
std::cout << "墙壁: " << sym.getWall() << std::endl;
std::cout << "空地: " << sym.getEmpty() << std::endl;
std::cout << "玩家: " << sym.getPlayer() << std::endl;
std::cout << "怪物: " << sym.getMonster() << std::endl;
std::cout << "陷阱: " << sym.getTrap() << std::endl;
std::cout << "出口: " << sym.getExit() << std::endl;
std::cout << std::endl;
std::cout << "迷宫大小: " << m_config.getMazeWidth() << "x" << m_config.getMazeHeight() << std::endl;
std::cout << "怪物AI难度: " << m_config.getDifficulty() << std::endl;
}

View File

@ -1,10 +1,10 @@
[Symbols]
[Symbols]
Wall = #
Player = @
Monster = M
Trap = X
Exit = E
Empty =
Empty = .
[Difficulty]
# 0: 随机移动1: BFS偏向2: A*精确追踪

View File

@ -1,17 +1,19 @@
#include "main.h"
#include "main.h"
#include <windows.h>
#include <consoleapi2.h>
int main()
{
SetConsoleOutputCP(CP_UTF8);
SetConsoleCP(CP_UTF8);
std::cout << "Hello CMake." << std::endl;
Game game;
game.loadConfig("F:\\mazzGame\\mazzGame\\src\\config.ini");
game.loadConfig("config.ini");
game.printConfig();
game.initMaze();
game.run();
system("pause");
return 0;
}