2013-10-05 2 views
-3

나는이 작업을 수행하고있는 프로젝트의 지하 감옥 발전기를 만들려고합니다. algorithm. 모든 것을 다운 받았지만 배열 (그림 1)이 어떤 이유로지도 데이터를 제공하지 않는 것 같습니다. 3 가지 유형의 데이터를 사용하여지도의 셀이 비 었는지 (0), 문자가있는 공간 (1), 복도 (2) 또는 벽 (3)인지 확인합니다.C# 배열에 데이터가 없습니다.

나는이 부분에 조금 붙어있어 어떤 도움을 주시면 감사하겠습니다!

편집 : 문제는지도 객체가 그림 1에 표시된 루프에 데이터를 저장하지 않는다는 것입니다. 너무 모호하게 여겨서 죄송합니다.

(그림. 1)

 for (int i = 0; i < roomList.Count; i++) 
     { 
      for (int x = roomList[i].X; x < (roomList[i].X + roomList[i].W); x++) 
      { 
       for (int y = roomList[i].Y; y < (roomList[i].Y + roomList[i].H); y++) 
       { 
        map[x, y] = 1; 
       } 
      } 
     } 

(내 관련 모든 코드)를 for -loop에서

namespace Project 
{ 
    } 
    public class Room 
    { 
     int xValue, yValue, widthValue, heightValue; 

     public int X 
     { 
      get { return xValue; } 
      set { xValue = value; } 
     } 
     public int Y 
     { 
      get { return yValue; } 
      set { yValue = value; } 
     } 
     public int W 
     { 
      get { return widthValue; } 
      set { widthValue = value; } 
     } 
     public int H 
     { 
      get { return heightValue; } 
      set { heightValue = value; } 
     } 
    } 
public class DungeonGenerate 
{ 
    public int baseWidth = 513; 
    public int baseHeight = 513; 
    public int width = 64; 
    public int height = 64; 
    Color[,] arrayColor; 
    Random rand = new Random(); 
    Room room = new Room(); 
    Rectangle[,] rectMap; 

    public void Generate() 
    { 
     rectMap = new Rectangle[baseWidth, baseHeight]; 
     //Creates a 2-D Array/Grid for the Dungeon 
     int[,] map = new int[baseWidth, baseHeight]; 
     //Determines all the cells to be empty until otherwise stated 
     for (int x = 0; x < width; x++) 
     { 
      for (int y = 0; y < height; y++) 
      { 
       map[x, y] = 0; 
      } 
     } 

     //Determines the amount of rooms in the dungeon 
     int minRooms = (width * height)/300; 
     int maxRooms = (width * height)/150; 
     int amountOfRooms = rand.Next(minRooms, maxRooms); 

     //Room dimensions 
     int widthRoot = Convert.ToInt32(Math.Round(Math.Sqrt(width * 2))); 
     int heightRoot = Convert.ToInt32(Math.Round(Math.Sqrt(height * 2))); 
     int minWidth = Convert.ToInt32(Math.Round((width * .5)/widthRoot)); 
     int maxWidth = Convert.ToInt32((width * 2)/widthRoot); 
     int minHeight = Convert.ToInt32(Math.Round(height * .5)/heightRoot); 
     int maxHeight = Convert.ToInt32((height * 2)/heightRoot); 

     //Creates the rooms 
     List<Room> roomList = new List<Room>(amountOfRooms); 

     for (int i = 0; i < amountOfRooms; i++) 
     { 
      bool ok = false; 
      do 
      { 
       room.X = rand.Next(width); 
       room.Y = rand.Next(height); 
       room.W = (rand.Next(maxWidth)) + minWidth; 
       room.H = (rand.Next(maxHeight)) + minHeight; 

       if (room.X + room.W >= width && room.Y + room.H >= height) 
       { 
        continue; 
       } 
       for (int q = 0; q < roomList.Count; q++) 
       { 
        if (room.X > roomList[q].X && room.X < roomList[q].X + room.W && room.Y > roomList[q].Y && room.Y < roomList[q].Y + room.H) 
        { 
         ok = false; 
         break; 
        } 
       } 
       ok = true; 
       roomList.Add(room); 
      } while (!ok); 
     } 
     //This will create hallways that lead to and from the rooms 
     int connectionCount = roomList.Count; 
     List<Point> connectedCells = new List<Point>((width * height)); 
     for (int i = 0; i < connectionCount; i++) 
     { 
      Room roomA = roomList[i]; 
      int roomNum = i; 

      while (roomNum == i) 
      { 
       roomNum = rand.Next(roomList.Count); 
      } 

      Room roomB = roomList[roomNum]; 

      //Increasing this will make the hallway more straight, decreasing it will make the hallway more skewed 
      int sidestepChance = 10; 

      Point pointA = new Point(x: (rand.Next(roomA.W)) + roomA.X, y: (rand.Next(roomA.H)) + roomA.Y); 
      Point pointB = new Point(x: (rand.Next(roomB.W)) + roomB.X, y: (rand.Next(roomB.H)) + roomB.Y); 

      while (pointA != pointB) 
      { 
       int num = rand.Next() * 100; 

       if (num < sidestepChance) 
       { 
        if (pointB.X != pointA.X) 
        { 
         if (pointB.X > pointA.X) 
         { 
          pointB.X--; 
         } 
         else 
         { 
          pointB.X++; 
         } 
        } 
       } 
       else if(pointB.Y != pointA.Y) 
       { 
        if (pointB.Y > pointA.Y) 
        { 
         pointB.Y--; 
        } 
        else 
        { 
         pointB.Y++; 
        } 
       } 
      } 

      if (pointB.X < width && pointB.Y < height) 
      { 
       connectedCells.Add(pointB); 
      } 
     } 

     //Fills the room with data 
     for (int i = 0; i < roomList.Count; i++) 
     { 
      for (int x = roomList[i].X; x < (roomList[i].X + roomList[i].W); x++) 
      { 
       for (int y = roomList[i].Y; y < (roomList[i].Y + roomList[i].H); y++) 
       { 
        map[x, y] = 1; 
       } 
      } 
     } 

     for (int y = 0; y < height; y++) 
     { 
      for (int x = 0; x < width; x++) 
      { 
       if (map[x, y] == 0) 
       { 
        bool wall = false; 
        for (int yy = y - 2; yy < y + 2; yy++) 
        { 
         for (int xx = x - 2; xx < x + 2; xx++) 
         { 
          if (xx > 0 && yy > 0 && xx < width && yy < height) 
          { 
           if (map[xx, yy] == 1 || map[xx, yy] == 2) 
           { 
            map[x, y] = 3; 
            wall = true; 
           } 
          } 
         } 
         if (wall) 
         { 
          break; 
         } 
        } 
       } 
      } 
     } 

     //Rendering the Map and giving it some Color (Sort of)! 
     int scaler = baseWidth/width; 

     for (int x = 0; x < baseWidth; x++) 
     { 
      for (int y = 0; y < baseHeight; y++) 
      { 
       rectMap[x, y] = new Rectangle(x, y, 1, 1); 
       arrayColor = new Color[baseWidth, baseHeight]; 
       switch (map[x, y]) 
       { 
        case 0: 
         arrayColor[x, y] = new Color(0,0,0); 
         break; 
        case 1: 
         arrayColor[x, y] = new Color(0,0,0); 
         break; 
        case 2: 
         arrayColor[x, y] = new Color(0,0,0); 
         break; 
        case 3: 
         arrayColor[x, y] = new Color (0,0,0); 
         break; 
       } 
      } 
     } 
    } 
    public Rectangle[,] GetMap() 
    { 
     return rectMap; 
    } 
    public Color[,] GetColors() 
    { 
     return arrayColor; 
    } 
} 
+1

을 당신이 값을 보유하지 않습니다 무슨 뜻 이죠? 어느 부분이 정확히? – Szymon

+2

문제가 무엇인지 좀 더 자세하게 알아볼 수 있습니까? 가치가없는 코드에 대한 큰 목록과 가치없는 질문은 우리가 여러분을 도울 수 있도록 도와주지 않습니다! –

답변

2

당신이 roomList을 채우기하고, 새 Room을 인스턴스화하지 않는 매번. 단순히 동일한 Room 개체를 조작하여 목록에 다시 추가하기 때문에 roomList은 동일한 Room 개체에 대한 많은 참조를 포함하게됩니다. 당신의 DungeonGenerate 클래스에서 room 필드를 제거하는 시도하고 대신 로컬 변수를 사용

for (int i = 0; i < amountOfRooms; i++) 
{ 
    bool ok = false; 
    do 
    { 
     var room = new Room(); 
     ... 
     roomList.Add(room); 
    } while (!ok); 
} 
+0

좋은 캐치, +1. – Brian

관련 문제