# Really need help with a simple dungeon generator!

**URL:** <https://discourse.processing.org/t/really-need-help-with-a-simple-dungeon-generator/27795>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [February 13, 2021, 3:21pm UTC](https://discourse.processing.org/t/really-need-help-with-a-simple-dungeon-generator/27795 "2021-02-13T15:21:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ekay](https://avatars.discourse-cdn.com/v4/letter/e/9e8a1a/32.png) [@ekay](https://discourse.processing.org/u/ekay)\
**Post date:** [February 13, 2021, 3:21pm UTC](https://discourse.processing.org/t/really-need-help-with-a-simple-dungeon-generator/27795/1 "2021-02-13T15:21:25Z")

</div>

Hi everyone! I am trying to create a random dungeon generator. I am at a very basic stage - I have set up an array filled with 0s called ‘activities’ and I am trying to randomly generate a square/rectangle of 1s randomly on it using object orientated coding. However, it isn’t working and I was wondering if there was an extra pair of eyes which could help me fix my mistakes! (Also I am new here so let me know if I am submitting anything incorrectly) Thank you so much - here is my code:

int activities[][] = {  
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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0,0,0,0},  
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
};

int xcor;  
int ycor;  
int mysize;  
int activitiesWidth;  
int activitiesHeight;  
Rooms dungeon;

public void settings() {  
size(1000, 4000);  
}

void setup(){  
mysize=20;  
activitiesWidth=35;  
activitiesHeight=25;  
dungeon= new Rooms();

}

void draw(){  
dungeon.generate2();  
xcor = 5;  
ycor = 5;  
for (int i = 0; i \< activitiesHeight; i++){  
//height of gameboard  
for (int j = 0; j \< activitiesWidth; j++){  
//width of gameboard

```
    if(activities[i][j]==1){
       fill(0,0,0);
       rect(xcor, ycor, mysize, mysize);
       xcor = xcor+mysize;
    }
      
    else if(activities[i][j]==0){
       fill(255,255,255);
       rect(xcor,ycor,mysize,mysize);
       xcor=xcor+mysize;
      
    }
    
  }

```

ycor=ycor+mysize;  
xcor=5;

}

}

class Rooms{

int roomWidth;  
int roomHeight;  
int xPos;  
int yPos;  
int mysize=20;  
int originalxPos;  
int originalyPos;

Rooms(){

roomWidth=int(random(2, 5));  
roomHeight=int(random(2, 5));

xPos=int(random(0,activitiesWidth));  
yPos=int(random(0,activitiesHeight));

originalxPos=xPos;  
originalyPos=yPos;

}

}

void generate2(){

for (int i = 0; i \< roomHeight; i++) {  
for (int j = 0; j \< roomWidth; j++) {  
activities[yPos][xPos]=1;  
xPos=xPos+1;  
}

yPos=yPos+1;

}

}

}

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [February 13, 2021, 4:38pm UTC](https://discourse.processing.org/t/really-need-help-with-a-simple-dungeon-generator/27795/2 "2021-02-13T16:38:56Z")

</div>

First problem, `void generate2()` needs to be inside your `Rooms` class. Remove a `}` in the right place.

Second problem, `dungeon.generate2();` needs to be in `setup()`, not `draw()`.

Third problem, you’re not setting `xPos` back to its original value properly in `generate2()`'s loops.

Fixed code:

```auto
int activities[][] = {
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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=20;
  activitiesWidth=35;
  activitiesHeight=25;
  dungeon= new Rooms();
  dungeon.generate2();
}

void draw() {
  
  xcor = 5;
  ycor = 5;
  for (int i = 0; i < activitiesHeight; i++) {
    //height of gameboard
    for (int j = 0; j < activitiesWidth; j++) {
      //width of gameboard

      if (activities[i][j]==1) {
        fill(0, 0, 0);
        rect(xcor, ycor, mysize, mysize);
        xcor = xcor+mysize;
      } else if (activities[i][j]==0) {
        fill(255, 255, 255);
        rect(xcor, ycor, mysize, mysize);
        xcor=xcor+mysize;
      }
    }
    ycor=ycor+mysize;
    xcor=5;
  }
}

class Rooms {

  int roomWidth;
  int roomHeight;
  int xPos;
  int yPos;
  int mysize=20;
  int originalxPos;
  int originalyPos;

  Rooms() {

    roomWidth=int(random(2, 5));
    roomHeight=int(random(2, 5));

    xPos=int(random(0, activitiesWidth));
    yPos=int(random(0, activitiesHeight));

    originalxPos=xPos;
    originalyPos=yPos;
  }

  void generate2() {

    for (int i = 0; i < roomHeight; i++) {
      for (int j = 0; j < roomWidth; j++) {
        activities[yPos][xPos]=1;
        xPos=xPos+1;
      }
      xPos = originalxPos;
      yPos=yPos+1;
    }
  }
}

```

There are other things that can be tidied up here too, but at least this works and makes a square black rooms.

---

<div class="post-metadata">

**Author:** ![ekay](https://avatars.discourse-cdn.com/v4/letter/e/9e8a1a/32.png) [@ekay](https://discourse.processing.org/u/ekay)\
**Post date:** [February 13, 2021, 4:50pm UTC](https://discourse.processing.org/t/really-need-help-with-a-simple-dungeon-generator/27795/3 "2021-02-13T16:50:33Z")

</div>

Oh my gosh thank you so much!! It was honestly so kind of you to fix up my code for me - I really appreciate it! I am still quite new to Processing and Java as a whole so sorry if my mistakes were really stupid haha. Hopefully will this knowledge I can move onto the next of my project without any worries!

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [February 13, 2021, 5:03pm UTC](https://discourse.processing.org/t/really-need-help-with-a-simple-dungeon-generator/27795/4 "2021-02-13T17:03:08Z")

</div>

An online sketch as another dungeon generator example: 👼

> **[Random Tiled Terrain Gen - OpenProcessing](https://openprocessing.org/sketch/436477)**
>
> Tiled Rogue Map Generator

---

<div class="post-metadata">

**Author:** ![ekay](https://avatars.discourse-cdn.com/v4/letter/e/9e8a1a/32.png) [@ekay](https://discourse.processing.org/u/ekay)\
**Post date:** [February 13, 2021, 9:23pm UTC](https://discourse.processing.org/t/really-need-help-with-a-simple-dungeon-generator/27795/5 "2021-02-13T21:23:17Z")

</div>

Awesome! Thank you so much - I will defo take a look 🙂
