完成迷宫生成
							parent
							
								
									c397684ef8
								
							
						
					
					
						commit
						fba5e3b2ab
					
				| 
						 | 
				
			
			@ -19,7 +19,7 @@ MazeData ConfigReader::getSymbols() const
 | 
			
		|||
{
 | 
			
		||||
    return symbols;
 | 
			
		||||
}
 | 
			
		||||
void ConfigReader::setSymbols(MazeData &value)
 | 
			
		||||
void ConfigReader::setSymbols(MazeData value)
 | 
			
		||||
{
 | 
			
		||||
    symbols = value;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -87,14 +87,14 @@ bool ConfigReader::load(const std::string &path)
 | 
			
		|||
        else if (section == "Difficulty")
 | 
			
		||||
        {
 | 
			
		||||
            if (key == "MonsterAI")
 | 
			
		||||
                difficulty = std::stoi(value);
 | 
			
		||||
                symbols.setDifficulty(std::stoi(value));
 | 
			
		||||
        }
 | 
			
		||||
        else if (section == "Maze")
 | 
			
		||||
        {
 | 
			
		||||
            if (key == "Width")
 | 
			
		||||
                mazeWidth = std::stoi(value);
 | 
			
		||||
                symbols.setMazeWidth(std::stoi(value));
 | 
			
		||||
            else if (key == "Height")
 | 
			
		||||
                mazeHeight = std::stoi(value);
 | 
			
		||||
                symbols.setMazeHeight(std::stoi(value));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,4 +1,4 @@
 | 
			
		|||
#pragma once
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "MazeData.h"
 | 
			
		||||
#include <string>
 | 
			
		||||
| 
						 | 
				
			
			@ -12,7 +12,7 @@ public:
 | 
			
		|||
    bool load(const std::string &path);
 | 
			
		||||
 | 
			
		||||
    MazeData getSymbols() const;
 | 
			
		||||
    void setSymbols(MazeData &value);
 | 
			
		||||
    void setSymbols(MazeData value);
 | 
			
		||||
 | 
			
		||||
    int getDifficulty();
 | 
			
		||||
    int getMazeHeight();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -14,12 +14,18 @@ void Game::loadConfig(const std::string &filePath)
 | 
			
		|||
{
 | 
			
		||||
    m_config.setSymbols(m_mazeData);
 | 
			
		||||
    m_config.load(filePath);
 | 
			
		||||
    m_mazeData = m_config.getSymbols();
 | 
			
		||||
}
 | 
			
		||||
void Game::initMaze()
 | 
			
		||||
{
 | 
			
		||||
    m_maze.setMazeData(m_mazeData);
 | 
			
		||||
    m_maze.generate();
 | 
			
		||||
}
 | 
			
		||||
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()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,8 +1,8 @@
 | 
			
		|||
#pragma once
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "Config.h"
 | 
			
		||||
#include "MazeData.h"
 | 
			
		||||
 | 
			
		||||
#include "Maze.h"
 | 
			
		||||
 | 
			
		||||
#include <string>
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -11,6 +11,7 @@ class Game
 | 
			
		|||
private:
 | 
			
		||||
    ConfigReader m_config;
 | 
			
		||||
    MazeData m_mazeData;
 | 
			
		||||
    Maze m_maze;
 | 
			
		||||
 | 
			
		||||
    bool m_isRunning = true;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,4 +1,4 @@
 | 
			
		|||
#include "Maze.h"
 | 
			
		||||
#include "Maze.h"
 | 
			
		||||
 | 
			
		||||
#include <algorithm>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
| 
						 | 
				
			
			@ -20,15 +20,17 @@ Maze::~Maze()
 | 
			
		|||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Maze::setMazeData(MazeData &data)
 | 
			
		||||
void Maze::setMazeData(const MazeData &data)
 | 
			
		||||
{
 | 
			
		||||
    m_symbols = data;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Maze::generate(int w, int h, const MazeData &sym)
 | 
			
		||||
void Maze::generate()
 | 
			
		||||
{
 | 
			
		||||
    m_symbols = sym;
 | 
			
		||||
    m_width = (w % 2 == 0) ? w + 1 : w;
 | 
			
		||||
    m_height = (h % 2 == 0) ? h + 1 : h;
 | 
			
		||||
    int mazeWidth = m_symbols.getMazeWidth();
 | 
			
		||||
    int mazeHeight = m_symbols.getMazeHeight();
 | 
			
		||||
    m_width = (mazeWidth % 2 == 0) ? mazeWidth + 1 : mazeWidth;
 | 
			
		||||
    m_height = (mazeHeight % 2 == 0) ? mazeHeight + 1 : mazeHeight;
 | 
			
		||||
    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));
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,4 +1,4 @@
 | 
			
		|||
#pragma once
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "MazeData.h"
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -20,9 +20,9 @@ public:
 | 
			
		|||
    Maze();
 | 
			
		||||
    ~Maze();
 | 
			
		||||
 | 
			
		||||
    void setMazeData(MazeData &data);
 | 
			
		||||
    void setMazeData(const MazeData &data);
 | 
			
		||||
 | 
			
		||||
    void generate(int w, int h, const MazeData &sym);
 | 
			
		||||
    void generate();
 | 
			
		||||
    void draw(const Position &playerPos, const std::vector<Position> &monsterPos) const;
 | 
			
		||||
 | 
			
		||||
    bool isWalkable(const Position &p) const;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,4 +1,4 @@
 | 
			
		|||
#include "MazeData.h"
 | 
			
		||||
#include "MazeData.h"
 | 
			
		||||
 | 
			
		||||
MazeData::MazeData()
 | 
			
		||||
{
 | 
			
		||||
| 
						 | 
				
			
			@ -66,4 +66,34 @@ char MazeData::getExit() const
 | 
			
		|||
void MazeData::setExit(char 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
 | 
			
		||||
{
 | 
			
		||||
| 
						 | 
				
			
			@ -30,6 +30,14 @@ public:
 | 
			
		|||
 | 
			
		||||
    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:
 | 
			
		||||
    char wall;
 | 
			
		||||
    char empty;
 | 
			
		||||
| 
						 | 
				
			
			@ -37,4 +45,9 @@ private:
 | 
			
		|||
    char monster;
 | 
			
		||||
    char trap;
 | 
			
		||||
    char exit;
 | 
			
		||||
 | 
			
		||||
    int m_difficulty = 0; // 怪物AI难度
 | 
			
		||||
    int m_mazeWidth = 21;
 | 
			
		||||
    int m_mazeHeight = 21;
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -11,7 +11,7 @@ int main()
 | 
			
		|||
 | 
			
		||||
    Game game;
 | 
			
		||||
    game.loadConfig("config.ini");
 | 
			
		||||
    game.printConfig();
 | 
			
		||||
    //game.printConfig();
 | 
			
		||||
    game.initMaze();
 | 
			
		||||
    game.run();
 | 
			
		||||
    system("pause");
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue