#include "DxLib.h" #include #include void CreateRoom(std::vector>& t_Vec, int t_BaseX, int t_BaseY, int t_Width, int t_Height, int t_MinLength) { for(int i = t_BaseY, count = t_BaseY + t_Height; i < count; i++) { for(int j = t_BaseX, count2 = t_BaseX + t_Width; j < count2; j++) { if( ((i - t_BaseY) % (t_Height - 1) == 0) || ((j - t_BaseX) % (t_Width - 1) == 0) ){ if(t_Vec[i][j] == 0){ t_Vec[i][j] = 1; } }else{ t_Vec[i][j] = 2; } } } int t_SplitX = t_Width/(t_MinLength - 1); int t_SplitY = t_Height/(t_MinLength - 1); int t_RoomWidth = 2 * (t_MinLength - 1) + 1; int t_RoomHeight = 2 * (t_MinLength - 1) + 1; int t_RoomBaseX = 0; int t_RoomBaseY = 0; if(t_SplitX > 2 && t_SplitY > 2){ t_RoomWidth = (t_SplitX - 1) * (t_MinLength - 1) + 1; t_RoomHeight = (t_SplitY - 1) * (t_MinLength - 1) + 1; t_RoomBaseX = (rand() % 2) * (t_MinLength - 1); t_RoomBaseY = (rand() % 2) * (t_MinLength - 1); } for(int i = t_BaseY + t_RoomBaseY, count = t_BaseY + t_RoomBaseY + t_RoomHeight; i < count; i++) { for(int j = t_BaseX + t_RoomBaseX, count2 = t_BaseX + t_RoomBaseX + t_RoomWidth; j < count2; j++) { if( (i - t_BaseY - t_RoomBaseY) % (t_RoomHeight - 1) == 0 || (j - t_BaseX - t_RoomBaseX) % (t_RoomWidth - 1) == 0){ t_Vec[i][j] = 3; }else{ t_Vec[i][j] = 4; } } } } int CreateDoor(std::vector>& t_Vec, int t_BaseX, int t_BaseY, int t_Width, int t_Height, int t_MinLength) { int result = -1; for(int i = t_MinLength - 1; i < t_Height - 1; i += (t_MinLength - 1)) { for(int j = 0; j < t_Width - 1; j += (t_MinLength - 1)) { if(t_Vec[i][j] == 3 ){ int t_DoorNum = 0; int t_Num = j; for(int k = j + (t_MinLength - 1); k < t_Width - 1; ) { if((t_Vec[i][j] == 3 && t_Vec[i - 1][j] == 2 && t_Vec[i + 1][j] == 4) || (t_Vec[i][j] == 3 && t_Vec[i - 1][j] == 4 && t_Vec[i + 1][j] == 2)){ j += (t_MinLength - 1); k += (t_MinLength - 1); t_DoorNum += 1; }else{ break; } } for(int k = 0, count = rand() % (t_DoorNum + 1) + 1; k < count && k < 1; k++) { int t_DoorX = t_Num + (((rand() % count) + 1)*(t_MinLength - 1)>>1); if((t_Vec[i][t_DoorX] == 3 && t_Vec[i - 1][t_DoorX] == 2 && t_Vec[i + 1][t_DoorX] == 4) || (t_Vec[i][t_DoorX] == 3 && t_Vec[i - 1][t_DoorX] == 4 && t_Vec[i + 1][t_DoorX] == 2)){ t_Vec[i][t_DoorX] = 5; } } } } } return(result); } int DeleteWall(std::vector>& t_Vec, int t_BaseX, int t_BaseY, int t_Width, int t_Height, int t_MinLength) { int result = -1; for(int i = 0; i < t_Height - 1; i += (t_MinLength - 1)) { for(int j = 0; j < t_Width - 1; j += (t_MinLength - 1)) { if( j > 0){ for(int k = i + 1; t_Vec[k][j] == 3 && k < t_Height - 1; k++) { if(t_Vec[k][j] == 3 && ((t_Vec[k][j - 1] == 4 && t_Vec[k][j + 1] == 4) || (t_Vec[k + 1][j - 1] == 4 && t_Vec[k + 1][j + 1] == 4))){ t_Vec[k][j] = 4; }else{ break; } } } if( i > 0){ for(int k = j + 1; t_Vec[i][k] == 3 && k < t_Width - 1; k++) { if(t_Vec[i][k] == 3 && ((t_Vec[i - 1][k] == 4 && t_Vec[i + 1][k] == 4 || t_Vec[i - 1][k + 1] == 4 && t_Vec[i + 1][k + 1] == 4))){ t_Vec[i][k] = 4; }else{ break; } } } } } return(result); } int Split(std::vector>& t_Vec, int t_BaseX, int t_BaseY, int t_Width, int t_Height, int t_MinLength) { int result = -1; int t_Flag = 0; if(rand() % 2 == 0){ t_Flag = 1; } if(t_Flag == 0){ int t_SplitX = t_Width/(t_MinLength - 1); int t_RectWidth = t_Width; if(t_SplitX >= 5){ result = 0; t_RectWidth = (2 + (rand() % (t_SplitX - 4 + 1)))*(t_MinLength - 1) + 1; int t = Split( t_Vec, t_BaseX, t_BaseY, t_RectWidth, t_Height, t_MinLength); int tt = Split( t_Vec, t_BaseX + t_RectWidth - 1, t_BaseY, t_Width - t_RectWidth + 1, t_Height, t_MinLength); } }else if(t_Flag == 1){ int t_SplitY = t_Height/(t_MinLength - 1); int t_RectHeight = t_Height; if(t_SplitY >= 5){ result = 0; t_RectHeight = (2 + (rand() % (t_SplitY - 4 + 1)))*(t_MinLength - 1) + 1; int t = Split( t_Vec, t_BaseX, t_BaseY, t_Width, t_RectHeight, t_MinLength); int tt = Split( t_Vec, t_BaseX, t_BaseY + t_RectHeight - 1, t_Width, t_Height - t_RectHeight + 1, t_MinLength); } } if(result == -1){ CreateRoom(t_Vec, t_BaseX, t_BaseY, t_Width, t_Height, t_MinLength); } return(result); } void Create( std::vector>& t_Vec, int t_Width, int t_Height, int t_MinLength) { if(t_Width > t_MinLength && t_Height > t_MinLength && t_MinLength > 3){ //以下の様に、丁度 (固定幅 - 1)×N個 + 1 //の幅になるように、調整している。 //□●●□●●□●●□ t_Width = (t_Width/(t_MinLength - 1))*(t_MinLength - 1) + 1; t_Height = (t_Height/(t_MinLength - 1))*(t_MinLength - 1) + 1; t_Vec.resize(t_Height); for(int i = 0; i < t_Height; i++) { t_Vec[i].resize(t_Width); for(int j = 0; j < t_Width; j++) { t_Vec[i][j] = 0; } } Split( t_Vec, 0, 0, t_Width, t_Height, t_MinLength ); DeleteWall( t_Vec, 0, 0, t_Width, t_Height, t_MinLength); CreateDoor( t_Vec, 0, 0, t_Width, t_Height, t_MinLength); } } void Create2( std::vector>& t_Vec, int t_MinLength) { int t_Height = t_Vec.size(); int t_Width = t_Vec[0].size(); for(int i = 0; i < t_Height; i++) { for(int j = 0; j < t_Width; j++) { t_Vec[i][j] = 0; } } Split( t_Vec, 0, 0, t_Width, t_Height, t_MinLength ); DeleteWall( t_Vec, 0, 0, t_Width, t_Height, t_MinLength); CreateDoor( t_Vec, 0, 0, t_Width, t_Height, t_MinLength); } int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow ){ ChangeWindowMode( TRUE ); if(DxLib_Init() == -1 ) return -1; SetDrawScreen( DX_SCREEN_BACK ); SetGraphMode(1024, 768, 32); SetWindowPosition( 100, 32 ); srand((unsigned int)time(NULL)); std::vector> t_MapVec; //Vectorをresizeして、マップを作成する関数 Create(t_MapVec, 60, 40, 4); int t_Green = GetColor(0, 180, 0); int t_Cyan = GetColor(0, 100, 255); int t_White = GetColor(255, 255, 255); int t_Yellow = GetColor(255, 255, 0); //間隔を空けて、Create2関数を実行させる為の変数。 int t_Count = 0; while(ProcessMessage()==0 && ClearDrawScreen()==0){ for(int i = 0, count = (int)t_MapVec.size(); i < count; i++) { for(int j = 0, count2 = (int)t_MapVec[i].size(); j < count2; j++) { switch(t_MapVec[i][j]){ case 0: DrawString(j<<4, i<<4, " ", t_Green);break; case 1: DrawString(j<<4, i<<4, "■", t_Green);break; case 2: DrawString(j<<4, i<<4, "×", t_Green);break; case 3: DrawString(j<<4, i<<4, "■", t_White);break; case 4: DrawString(j<<4, i<<4, "×", t_Cyan);break; case 5: DrawString(j<<4, i<<4, "□", t_Yellow);break; default: break; } } } if((t_Count += 1) > 36){ //マップを書き換える関数。 Create2(t_MapVec, 4); t_Count = 0; } ScreenFlip(); } DxLib_End(); return 0; }