C++完成隨機生成迷宮地牢。本站提示廣大學習愛好者:(C++完成隨機生成迷宮地牢)文章只能為提供參考,不一定能成為您想要的結果。以下是C++完成隨機生成迷宮地牢正文
可以用這個地圖焦點做成一個無窮迷宮類的游戲
main.cpp
// Author: FreeKnight 2014-09-02 #include "stdafx.h" #include <iostream> #include <string> #include <random> #include <cassert> /* 簡略邏輯流程描寫: 將全部地圖填滿土 在地圖中央挖一個房間出來 選中某一房間(假如有多個的話)的牆壁 肯定要建築某種新元素 檢查從選中的牆延長出去能否有足夠的空間承載新的元素 假如有的話持續,否則就前往第 3 步 從選中的牆處增長新的元素 前往第 3 步,直到地牢扶植完成 在地圖的隨機點上支配上樓和下樓的樓梯 最初,放出來怪獸和物品 */ //------------------------------------------------------------------------------- // 臨時支撐的最年夜的地圖塊個數 #define MAX_TILES_NUM 10000 // 房間的年夜小 #define MAX_ROOM_WIDTH 8 #define MAX_ROOM_HEIGHT 8 #define MIN_ROOM_WIDTH 4 #define MIN_ROOM_HEIGHT 4 // 房間和走廊的算計最年夜個數 #define DEFAULT_FEATURE_NUM 1000 // 測驗考試生成房間和走廊的測試次數(即步長) #define MAX_TRY_TIMES 1000 // 默許創立房間的幾率(100-該值則為創立走廊的幾率) #define DEFAULT_CREATE_ROOM_CHANCE 70 // 走廊長度 #define MIN_CORRIDOR_LEN 3 #define MAX_CORRIDOR_LEN 6 //------------------------------------------------------------------------------- // 格子塊 enum class Tile { Unused, // 沒用的格子(土塊) DirtWall, // 牆壁 DirtFloor, // 房間地板 Corridor, // 走廊 Door, // 房門 UpStairs, // 進口 DownStairs // 出口 }; //------------------------------------------------------------------------------- // 朝向 enum class Direction { North, // 北 South, // 南 East, // 東 West, // 西 }; //------------------------------------------------------------------------------- class Map { public: Map(): xSize(0), ySize(0), data() { } // 結構函數,全屏填土 Map(int x, int y, Tile value = Tile::Unused): xSize(x), ySize(y), data(x * y, value) { } // 填充某塊類型 void SetCell(int x, int y, Tile celltype) { assert(IsXInBounds(x)); assert(IsYInBounds(y)); data[x + xSize * y] = celltype; } // 獲得某塊類型 Tile GetCell(int x, int y) const { assert(IsXInBounds(x)); assert(IsYInBounds(y)); return data[x + xSize * y]; } // 設置一塊區域為指定類型塊 void SetCells(int xStart, int yStart, int xEnd, int yEnd, Tile cellType) { assert(IsXInBounds(xStart) && IsXInBounds(xEnd)); assert(IsYInBounds(yStart) && IsYInBounds(yEnd)); assert(xStart <= xEnd); assert(yStart <= yEnd); for (auto y = yStart; y != yEnd + 1; ++y) { for (auto x = xStart; x != xEnd + 1; ++x) { SetCell(x, y, cellType); } } } // 斷定一塊能否在有用規模內 bool IsXInBounds(int x) const { return x >= 0 && x < xSize; } // 斷定一塊能否在有用規模內 bool IsYInBounds(int y) const { return y >= 0 && y < ySize; } // 斷定一個區域能否已被應用過 bool IsAreaUnused(int xStart, int yStart, int xEnd, int yEnd) { assert(IsXInBounds(xStart) && IsXInBounds(xEnd)); assert(IsYInBounds(yStart) && IsYInBounds(yEnd)); assert(xStart <= xEnd); assert(yStart <= yEnd); for (auto y = yStart; y != yEnd + 1; ++y) { for (auto x = xStart; x != xEnd + 1; ++x) { if (GetCell(x, y) != Tile::Unused) { return false; } } } return true; } // 斷定一個地圖塊四周能否臨接某種地圖塊 bool IsAdjacent(int x, int y, Tile tile) { assert(IsXInBounds(x - 1) && IsXInBounds(x + 1)); assert(IsYInBounds(y - 1) && IsYInBounds(y + 1)); return (GetCell(x - 1, y) == tile || GetCell(x + 1, y) == tile || GetCell(x, y - 1) == tile || GetCell(x, y + 1) == tile); } // 輸入地圖 void Print() const { for (auto y = 0; y != ySize; y++) { for (auto x = 0; x != xSize; x++) { switch(GetCell(x, y)) { case Tile::Unused: std::cout << " "; break; case Tile::DirtWall: std::cout << "#"; break; case Tile::DirtFloor: std::cout << "_"; break; case Tile::Corridor: std::cout << "."; break; case Tile::Door: std::cout << "+"; break; case Tile::UpStairs: std::cout << "<"; break; case Tile::DownStairs: std::cout << ">"; break; }; } std::cout << std::endl; } std::cout << std::endl; } private: // 地圖總寬高 int xSize, ySize; // 全體地圖塊數據 std::vector<Tile> data; }; //------------------------------------------------------------------------------- class DungeonGenerator { public: int m_nSeed; // 隨機數種子 int m_nXSize, m_nYSize; // 地圖最年夜寬高 int m_nMaxFeatures; // 房間和走廊的最年夜個數 int m_nChanceRoom; // 創立房間的幾率【0,100】 int m_nChanceCorridor; // 創立走廊的幾率【0,100】 該幾率+創立房間的幾率應該 = 100 typedef std::mt19937 RngT; public: DungeonGenerator( int XSize, int YSize ): m_nSeed(std::random_device()()), m_nXSize( XSize ), m_nYSize( YSize ), m_nMaxFeatures( DEFAULT_FEATURE_NUM ), m_nChanceRoom( DEFAULT_CREATE_ROOM_CHANCE ) { m_nChanceCorridor = 100 - m_nChanceRoom; } Map Generate() { assert( m_nMaxFeatures > 0 && m_nMaxFeatures <= DEFAULT_FEATURE_NUM); assert( m_nXSize > 3 ); assert( m_nYSize > 3 ); auto rng = RngT(m_nSeed); // step1: 滿地圖填土 auto map = Map(m_nXSize, m_nYSize, Tile::Unused); MakeDungeon(map, rng); return map; } private: // 獲得隨機int int GetRandomInt(RngT& rng, int min, int max) const { return std::uniform_int_distribution<int>(min, max)(rng); } // 獲得隨機偏向 Direction GetRandomDirection(RngT& rng) const { return Direction(std::uniform_int_distribution<int>( static_cast<int>(Direction::North), static_cast<int>(Direction::West) )(rng)); } // 創立走廊 bool MakeCorridor(Map& map, RngT& rng, int x, int y, int maxLength, Direction direction) const { assert(x >= 0 && x < m_nXSize); assert(y >= 0 && y < m_nYSize); assert(maxLength > 0 && maxLength <= std::max(m_nXSize, m_nYSize)); // 設置走廊長度 auto length = GetRandomInt(rng, MIN_CORRIDOR_LEN, maxLength); auto xStart = x; auto yStart = y; auto xEnd = x; auto yEnd = y; if (direction == Direction::North) yStart = y - length; else if (direction == Direction::East) xEnd = x + length; else if (direction == Direction::South) yEnd = y + length; else if (direction == Direction::West) xStart = x - length; // 檢討全部走廊能否在地圖內 if (!map.IsXInBounds(xStart) || !map.IsXInBounds(xEnd) || !map.IsYInBounds(yStart) || !map.IsYInBounds(yEnd)) return false; // 檢討走廊區域能否有被占用 if (!map.IsAreaUnused(xStart, yStart, xEnd, yEnd)) return false; map.SetCells(xStart, yStart, xEnd, yEnd, Tile::Corridor); return true; } // 發明房間 bool MakeRoom(Map& map, RngT& rng, int x, int y, int xMaxLength, int yMaxLength, Direction direction) const { assert( xMaxLength >= MIN_ROOM_WIDTH ); assert( yMaxLength >= MIN_ROOM_HEIGHT ); // 創立的房間最小是4 * 4,隨機出房間年夜小 auto xLength = GetRandomInt(rng, MIN_ROOM_WIDTH, xMaxLength); auto yLength = GetRandomInt(rng, MIN_ROOM_HEIGHT, yMaxLength); auto xStart = x; auto yStart = y; auto xEnd = x; auto yEnd = y; // 依據房間朝向隨機出房間肇端和終結地位 if (direction == Direction::North) { yStart = y - yLength; xStart = x - xLength / 2; xEnd = x + (xLength + 1) / 2; } else if (direction == Direction::East) { yStart = y - yLength / 2; yEnd = y + (yLength + 1) / 2; xEnd = x + xLength; } else if (direction == Direction::South) { yEnd = y + yLength; xStart = x - xLength / 2; xEnd = x + (xLength + 1) / 2; } else if (direction == Direction::West) { yStart = y - yLength / 2; yEnd = y + (yLength + 1) / 2; xStart = x - xLength; } // 要包管生成的房間必定四個點都在地圖中 if (!map.IsXInBounds(xStart) || !map.IsXInBounds(xEnd) || !map.IsYInBounds(yStart) || !map.IsYInBounds(yEnd)) return false; // 要包管房間所占用地盤未被其他地占用 if (!map.IsAreaUnused(xStart, yStart, xEnd, yEnd)) return false; // 四周種牆 map.SetCells(xStart, yStart, xEnd, yEnd, Tile::DirtWall); // 房間內鋪地板 map.SetCells(xStart + 1, yStart + 1, xEnd - 1, yEnd - 1, Tile::DirtFloor); return true; } // 創立一個房間或許走廊 bool MakeRoomOrCorridor(Map& map, RngT& rng, int x, int y, int xmod, int ymod, Direction direction) const { // 隨機選擇創立類型(房間或許走廊) auto chance = GetRandomInt(rng, 0, 100); if (chance <= m_nChanceRoom) { // 創立房間 if (MakeRoom(map, rng, x + xmod, y + ymod, MAX_ROOM_WIDTH, MAX_ROOM_HEIGHT, direction)) { // 以後地位設置門 map.SetCell(x, y, Tile::Door); // 刪除門旁邊的牆壁,改建為牆壁 map.SetCell(x + xmod, y + ymod, Tile::DirtFloor); return true; } return false; } else { // 創立走廊 if (MakeCorridor(map, rng, x + xmod, y + ymod, MAX_CORRIDOR_LEN, direction)) { // 以後地位設置門 map.SetCell(x, y, Tile::Door); return true; } return false; } } // 對全地圖停止隨機處置生成房間和走廊 bool MakeRandomFeature(Map& map, RngT& rng) const { for( auto tries = 0 ; tries != MAX_TRY_TIMES; ++tries) { // 獲得一個成心義的地形格 int x = GetRandomInt(rng, 1, m_nXSize - 2); int y = GetRandomInt(rng, 1, m_nYSize - 2); // 獲得一個隨機牆壁 或許 走廊 if (map.GetCell(x, y) != Tile::DirtWall && map.GetCell(x, y) != Tile::Corridor) continue; // 包管該牆壁和走廊不臨接門 if (map.IsAdjacent(x, y, Tile::Door)) continue; // 找個臨接牆壁或許走廊的格子 創立新居間或許走廊 if (map.GetCell(x, y+1) == Tile::DirtFloor || map.GetCell(x, y+1) == Tile::Corridor) { if (MakeRoomOrCorridor(map, rng, x, y, 0, -1, Direction::North)) return true; } else if (map.GetCell(x-1, y) == Tile::DirtFloor || map.GetCell(x-1, y) == Tile::Corridor) { if (MakeRoomOrCorridor(map, rng, x, y, 1, 0, Direction::East)) return true; } else if (map.GetCell(x, y-1) == Tile::DirtFloor || map.GetCell(x, y-1) == Tile::Corridor) { if (MakeRoomOrCorridor(map, rng, x, y, 0, 1, Direction::South)) return true; } else if (map.GetCell(x+1, y) == Tile::DirtFloor || map.GetCell(x+1, y) == Tile::Corridor) { if (MakeRoomOrCorridor(map, rng, x, y, -1, 0, Direction::West)) return true; } } return false; } // 隨機制造收支口 bool MakeRandomStairs(Map& map, RngT& rng, Tile tile) const { auto tries = 0; auto maxTries = MAX_TILES_NUM; for ( ; tries != maxTries; ++tries) { // 隨機獲得一個非邊沿的點 int x = GetRandomInt(rng, 1, m_nXSize - 2); int y = GetRandomInt(rng, 1, m_nYSize - 2); // 假如四周沒有地板而且沒有走廊(通路)的話,直接廢棄 if (!map.IsAdjacent(x, y, Tile::DirtFloor) && !map.IsAdjacent(x, y, Tile::Corridor)) continue; // 四周不許可有門 if (map.IsAdjacent(x, y, Tile::Door)) continue; map.SetCell(x, y, tile); return true; } return false; } // 隨機生成地牢 bool MakeDungeon(Map& map, RngT& rng) const { // step2 : 在正中央創立一個房間 MakeRoom(map, rng, m_nXSize / 2, m_nYSize / 2, MAX_ROOM_WIDTH, MAX_ROOM_HEIGHT, GetRandomDirection(rng)); for (auto features = 1; features != m_nMaxFeatures; ++features) { if (!MakeRandomFeature(map, rng)) { std::cout << "生成地牢已滿。(以後房間和走廊個數為: " << features << ")." << std::endl; break; } } // 創立隨機進口點 if (!MakeRandomStairs(map, rng, Tile::UpStairs)) std::cout << "創立進口點掉敗!" << std::endl; // 創立隨機出口點 if (!MakeRandomStairs(map, rng, Tile::DownStairs)) std::cout << "創立出口點掉敗!" << std::endl; return true; } }; #include <windows.h> void ResetConsoleSize() { HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); // 獲得尺度輸入裝備句柄 CONSOLE_SCREEN_BUFFER_INFO bInfo; // 窗口緩沖區信息 GetConsoleScreenBufferInfo(hOut, &bInfo ); COORD size = {1000, 800}; SetConsoleScreenBufferSize(hOut,size); // 從新設置緩沖區年夜小 SMALL_RECT rc = {0,0, 1000-1, 800-1}; // 重置窗口地位和年夜小 SetConsoleWindowInfo(hOut,true ,&rc); } void FlushReadme() { std::cout<< "=================================" << std::endl; std::cout<< " < 表現進口 " << std::endl; std::cout<< " > 表現出口 " << std::endl; std::cout<< " _ 下劃線表現地板 " << std::endl; std::cout<< " # 表現牆壁 " << std::endl; std::cout<< " . 點表現走廊 " << std::endl; std::cout<< " + 表現門 " << std::endl; std::cout<< "純黑表現啥都沒有,是妨礙" << std::endl; std::cout<< "=================================" << std::endl; } int main() { ResetConsoleSize(); FlushReadme(); DungeonGenerator* pGenerator = new DungeonGenerator( 150, 50 ); if( pGenerator == NULL ) return -1; auto map = pGenerator->Generate(); map.Print(); int n; std::cin >> n; }
演示圖:
以上所述就是本文的全體內容了,願望年夜家可以或許愛好。