更新配置文件读取

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

View File

@ -1,4 +1,4 @@
#include "Game.h" #include "Game.h"
#include <iostream> #include <iostream>
@ -25,14 +25,17 @@ void Game::run()
void Game::printConfig() void Game::printConfig()
{ {
auto sym = m_config.getSymbols(); 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; std::cout << "怪物AI难度: " << m_config.getDifficulty() << std::endl;
} }

View File

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

View File

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