Compare commits
1 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
666cee6c62 |
|
|
@ -1,5 +0,0 @@
|
||||||
out/
|
|
||||||
.vs/
|
|
||||||
.vscode/
|
|
||||||
build/
|
|
||||||
.cache/
|
|
||||||
|
|
@ -1,5 +1,2 @@
|
||||||
mazzGame/out/
|
out/
|
||||||
.vs/
|
.vs/
|
||||||
.vscode/
|
|
||||||
build/
|
|
||||||
.cache/
|
|
||||||
|
|
@ -13,13 +13,9 @@ project ("mazzGame")
|
||||||
|
|
||||||
aux_source_directory(${PROJECT_SOURCE_DIR}/src sourceCodeList)
|
aux_source_directory(${PROJECT_SOURCE_DIR}/src sourceCodeList)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# 将源代码添加到此项目的可执行文件。
|
# 将源代码添加到此项目的可执行文件。
|
||||||
add_executable (mazzGame ${sourceCodeList})
|
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)
|
if (CMAKE_VERSION VERSION_GREATER 3.12)
|
||||||
set_property(TARGET mazzGame PROPERTY CXX_STANDARD 20)
|
set_property(TARGET mazzGame PROPERTY CXX_STANDARD 20)
|
||||||
endif()
|
endif()
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,7 @@
|
||||||
#include "Config.h"
|
#include "Config.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <iostream>
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <string>
|
#include <iostream>
|
||||||
#include <locale>
|
|
||||||
#include <codecvt>
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
ConfigReader::ConfigReader()
|
ConfigReader::ConfigReader()
|
||||||
|
|
@ -15,15 +12,6 @@ ConfigReader::~ConfigReader()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
MazeData ConfigReader::getSymbols() const
|
|
||||||
{
|
|
||||||
return symbols;
|
|
||||||
}
|
|
||||||
void ConfigReader::setSymbols(MazeData value)
|
|
||||||
{
|
|
||||||
symbols = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string ConfigReader::trim(const std::string &s)
|
std::string ConfigReader::trim(const std::string &s)
|
||||||
{
|
{
|
||||||
auto start = s.find_first_not_of(" \t\r\n");
|
auto start = s.find_first_not_of(" \t\r\n");
|
||||||
|
|
@ -33,11 +21,9 @@ std::string ConfigReader::trim(const std::string &s)
|
||||||
return s.substr(start, end - start + 1);
|
return s.substr(start, end - start + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ConfigReader::load(const std::string &path)
|
bool ConfigReader::load(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;
|
||||||
|
|
@ -72,7 +58,7 @@ bool ConfigReader::load(const std::string &path)
|
||||||
if (section == "Symbols")
|
if (section == "Symbols")
|
||||||
{
|
{
|
||||||
if (key == "Wall")
|
if (key == "Wall")
|
||||||
symbols.setWall(value[0]);
|
m_data.setWall(value[0]);
|
||||||
else if (key == "Empty")
|
else if (key == "Empty")
|
||||||
symbols.setEmpty(value[0]);
|
symbols.setEmpty(value[0]);
|
||||||
else if (key == "Player")
|
else if (key == "Player")
|
||||||
|
|
@ -87,30 +73,17 @@ bool ConfigReader::load(const std::string &path)
|
||||||
else if (section == "Difficulty")
|
else if (section == "Difficulty")
|
||||||
{
|
{
|
||||||
if (key == "MonsterAI")
|
if (key == "MonsterAI")
|
||||||
symbols.setDifficulty(std::stoi(value));
|
m_difficulty = std::stoi(value);
|
||||||
}
|
}
|
||||||
else if (section == "Maze")
|
else if (section == "Maze")
|
||||||
{
|
{
|
||||||
if (key == "Width")
|
if (key == "Width")
|
||||||
symbols.setMazeWidth(std::stoi(value));
|
m_mazeWidth = std::stoi(value);
|
||||||
else if (key == "Height")
|
else if (key == "Height")
|
||||||
symbols.setMazeHeight(std::stoi(value));
|
m_mazeHeight = std::stoi(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
file.close();
|
file.close();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ConfigReader::getDifficulty()
|
|
||||||
{
|
|
||||||
return difficulty;
|
|
||||||
}
|
|
||||||
int ConfigReader::getMazeHeight()
|
|
||||||
{
|
|
||||||
return mazeHeight;
|
|
||||||
}
|
|
||||||
int ConfigReader::getMazeWidth()
|
|
||||||
{
|
|
||||||
return mazeWidth;
|
|
||||||
}
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "MazeData.h"
|
#include "MazeData.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
@ -12,18 +12,12 @@ public:
|
||||||
bool load(const std::string &path);
|
bool load(const std::string &path);
|
||||||
|
|
||||||
MazeData getSymbols() const;
|
MazeData getSymbols() const;
|
||||||
void setSymbols(MazeData value);
|
void setSymbols(MazeData &value);
|
||||||
|
|
||||||
int getDifficulty();
|
|
||||||
int getMazeHeight();
|
|
||||||
int getMazeWidth();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string trim(const std::string &s);
|
std::string trim(const std::string &s);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
MazeData symbols; // 存放迷宫符号
|
MazeData m_data; // 存放迷宫符号
|
||||||
int difficulty = 0; // 怪物AI难度
|
|
||||||
int mazeWidth = 21;
|
|
||||||
int mazeHeight = 21;
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
#include "Game.h"
|
#include "Game.h"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
|
@ -10,38 +10,19 @@ Game::~Game()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::loadConfig(const std::string &filePath)
|
void Game::loadConfig(const std::string &filePath, MazeData &data)
|
||||||
{
|
{
|
||||||
m_config.setSymbols(m_mazeData);
|
m_config.setSymbols(data);
|
||||||
m_config.load(filePath);
|
m_config.load(filePath);
|
||||||
m_mazeData = m_config.getSymbols();
|
|
||||||
}
|
}
|
||||||
void Game::initMaze()
|
void Game::initMaze()
|
||||||
{
|
{
|
||||||
m_maze.setMazeData(m_mazeData);
|
|
||||||
m_maze.generate();
|
|
||||||
}
|
}
|
||||||
void Game::run()
|
void Game::run()
|
||||||
{
|
{
|
||||||
Position player(1, 1);
|
|
||||||
std::vector<Position> monster{Position(2, 2), Position(4, 4)};
|
|
||||||
m_maze.draw(player,monster);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::printConfig()
|
void Game::printConfig()
|
||||||
{
|
{
|
||||||
auto sym = m_config.getSymbols();
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,8 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Config.h"
|
#include "Config.h"
|
||||||
#include "MazeData.h"
|
#include "MazeData.h"
|
||||||
#include "Maze.h"
|
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
|
@ -11,7 +10,6 @@ class Game
|
||||||
private:
|
private:
|
||||||
ConfigReader m_config;
|
ConfigReader m_config;
|
||||||
MazeData m_mazeData;
|
MazeData m_mazeData;
|
||||||
Maze m_maze;
|
|
||||||
|
|
||||||
bool m_isRunning = true;
|
bool m_isRunning = true;
|
||||||
|
|
||||||
|
|
@ -19,8 +17,8 @@ public:
|
||||||
Game();
|
Game();
|
||||||
~Game();
|
~Game();
|
||||||
|
|
||||||
void loadConfig(const std::string &filePath);
|
void loadConfig(const std::string &filePath, MazeData &data);
|
||||||
void printConfig();
|
void printConfig(MazeData &data);
|
||||||
void initMaze();
|
void initMaze();
|
||||||
void run();
|
void run();
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
#include "Maze.h"
|
#include "Maze.h"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
@ -20,17 +20,15 @@ Maze::~Maze()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void Maze::setMazeData(const MazeData &data)
|
void Maze::setMazeData(MazeData &data)
|
||||||
{
|
{
|
||||||
m_symbols = data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Maze::generate()
|
void Maze::generate(int w, int h, const MazeData &sym)
|
||||||
{
|
{
|
||||||
int mazeWidth = m_symbols.getMazeWidth();
|
m_symbols = sym;
|
||||||
int mazeHeight = m_symbols.getMazeHeight();
|
m_width = (w % 2 == 0) ? w + 1 : w;
|
||||||
m_width = (mazeWidth % 2 == 0) ? mazeWidth + 1 : mazeWidth;
|
m_height = (h % 2 == 0) ? h + 1 : h;
|
||||||
m_height = (mazeHeight % 2 == 0) ? mazeHeight + 1 : mazeHeight;
|
|
||||||
m_grid.assign(m_height, std::string(m_width, m_symbols.getWall()));
|
m_grid.assign(m_height, std::string(m_width, m_symbols.getWall()));
|
||||||
|
|
||||||
std::vector<std::vector<Cell>> cells(m_height, std::vector<Cell>(m_width));
|
std::vector<std::vector<Cell>> cells(m_height, std::vector<Cell>(m_width));
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "MazeData.h"
|
#include "MazeData.h"
|
||||||
|
|
||||||
|
|
@ -20,9 +20,9 @@ public:
|
||||||
Maze();
|
Maze();
|
||||||
~Maze();
|
~Maze();
|
||||||
|
|
||||||
void setMazeData(const MazeData &data);
|
void setMazeData(MazeData &data);
|
||||||
|
|
||||||
void generate();
|
void generate(int w, int h, const MazeData &sym);
|
||||||
void draw(const Position &playerPos, const std::vector<Position> &monsterPos) const;
|
void draw(const Position &playerPos, const std::vector<Position> &monsterPos) const;
|
||||||
|
|
||||||
bool isWalkable(const Position &p) const;
|
bool isWalkable(const Position &p) const;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
#include "MazeData.h"
|
#include "MazeData.h"
|
||||||
|
|
||||||
MazeData::MazeData()
|
MazeData::MazeData()
|
||||||
{
|
{
|
||||||
|
|
@ -67,33 +67,3 @@ void MazeData::setExit(char value)
|
||||||
{
|
{
|
||||||
exit = value;
|
exit = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
int MazeData::getDifficulty() const
|
|
||||||
{
|
|
||||||
return m_difficulty;
|
|
||||||
}
|
|
||||||
|
|
||||||
void MazeData::setDifficulty(int value)
|
|
||||||
{
|
|
||||||
m_difficulty = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
int MazeData::getMazeWidth() const
|
|
||||||
{
|
|
||||||
return m_mazeWidth;
|
|
||||||
}
|
|
||||||
|
|
||||||
void MazeData::setMazeWidth(int value)
|
|
||||||
{
|
|
||||||
m_mazeWidth = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
int MazeData::getMazeHeight() const
|
|
||||||
{
|
|
||||||
return m_mazeHeight;
|
|
||||||
}
|
|
||||||
|
|
||||||
void MazeData::setMazeHeight(int value)
|
|
||||||
{
|
|
||||||
m_mazeHeight = value;
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
class MazeData
|
class MazeData
|
||||||
{
|
{
|
||||||
|
|
@ -30,14 +30,6 @@ public:
|
||||||
|
|
||||||
void setExit(char value);
|
void setExit(char value);
|
||||||
|
|
||||||
int getDifficulty() const;
|
|
||||||
void setDifficulty(int value);
|
|
||||||
|
|
||||||
int getMazeWidth() const;
|
|
||||||
void setMazeWidth(int value);
|
|
||||||
int getMazeHeight() const;
|
|
||||||
void setMazeHeight(int value);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
char wall;
|
char wall;
|
||||||
char empty;
|
char empty;
|
||||||
|
|
@ -45,9 +37,4 @@ private:
|
||||||
char monster;
|
char monster;
|
||||||
char trap;
|
char trap;
|
||||||
char exit;
|
char exit;
|
||||||
|
|
||||||
int m_difficulty = 0; // 怪物AI难度
|
|
||||||
int m_mazeWidth = 21;
|
|
||||||
int m_mazeHeight = 21;
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -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*精确追踪
|
||||||
|
|
@ -1,19 +1,12 @@
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
|
|
||||||
#include <windows.h>
|
|
||||||
#include <consoleapi2.h>
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
std::cout << "Hello CMake." << std::endl;
|
||||||
SetConsoleOutputCP(CP_UTF8);
|
|
||||||
SetConsoleCP(CP_UTF8);
|
|
||||||
|
|
||||||
Game game;
|
Game game;
|
||||||
game.loadConfig("config.ini");
|
game.loadConfig("config.txt");
|
||||||
//game.printConfig();
|
|
||||||
game.initMaze();
|
game.initMaze();
|
||||||
game.run();
|
game.run();
|
||||||
system("pause");
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue