### Author Topic: Anyone know what's wrong with this code?  (Read 12330 times)

#### Shaggy

• Rogueliker
• Posts: 65
• Karma: +0/-0
• (╯°□°）╯︵ ┻━┻ <( !@#\$ THIS, I'M OUT. )
##### Anyone know what's wrong with this code?
« on: October 04, 2009, 03:33:00 AM »
[vb.net 2008 express]

I'm working on my Random Dungeon Generation. I have this function that returns whether or not a given area of the map is clear or not so that I can add a room there.

Code: [Select]
` Public Function Clear(ByVal Top As Integer, ByVal Left As Integer, ByVal Height As Integer, ByVal Width As Integer) As Boolean        'Returns whether a rectangle is empty        'used to loop through a rectangle of the map array        Dim introw As Integer        Dim intcol As Integer        'set clear to true as default        Clear = True        'if the rectangle goes outside of the aray, then set clear to fals        If Top + Height > 79 Then Clear = False        If Left + Width > 79 Then Clear = False        'check the rectangle, if it has any numbers in it other than 0[blank] or        ' 9[buffer to keep rooms from touching] then set it to false        For introw = Top To (Top + Height)            For intcol = Left To (Left + Width)                If Not Map(introw, intcol) = 0 Or 9 Then                    Clear = False                End If            Next        Next    End Function`
and it always returns false for some reason. I've displayed the map, and even when it checks a chunk that is 100% empty, it returns false. Any reason?
Check out my blog at http://NotQuiteADoM.blogspot.com/ !

#### Scautura

• Rogueliker
• Posts: 55
• Karma: +0/-0
##### Re: Anyone know what's wrong with this code?
« Reply #1 on: October 04, 2009, 06:04:36 AM »
This line:
Code: [Select]
`If Not Map(introw, intcol) = 0 Or 9 Then`You are checking:
Code: [Select]
`If (Not Map(introw, intcol) = 0) Or (9) Then`Which is True (9 is true, isn't it? )
When you mean:
Code: [Select]
`If Not (Map(introw, intcol) = 0 Or Map(introw, intcol) = 9) Then`
Duct tape is like the Force - it has a Dark side, a Light side, and it holds the Universe together.
CyberRogue

#### Ex

• IRC Communications Delegate
• Rogueliker
• Posts: 313
• Karma: +0/-0
##### Re: Anyone know what's wrong with this code?
« Reply #2 on: October 04, 2009, 06:26:23 AM »
It looks valid to me, though I'm definitely not a vb.net expert The usage of "if not map = 0 or 9" seems a little odd, but this is probably correct syntax for vb. In C, it would be something like if(map!=0&&map!=9), where as it seems to me here to be if(!(map==0||map==9)), which is essentially the same thing. I suspect it might be receiving bad input, such as Top being too high or left+width being too high. Or perhaps top and left being accidentally swapped somewhere along the way. Just guesses, though. Good luck!

Edit; after reading the new post, I highly suspect that this if statement is bad. Try what Scautura suggested

#### Shaggy

• Rogueliker
• Posts: 65
• Karma: +0/-0
• (╯°□°）╯︵ ┻━┻ <( !@#\$ THIS, I'M OUT. )
##### Re: Anyone know what's wrong with this code?
« Reply #3 on: October 04, 2009, 07:20:15 AM »
I tried that, but its still failing the clear check 100% of the time
although I think that may have been part of the problem..

and the thing that bugs me most is that once this clear check is fixed my RDG is basically done, other than aesthetics.
Check out my blog at http://NotQuiteADoM.blogspot.com/ !

#### purestrain

• Rogueliker
• Posts: 172
• Karma: +0/-0
##### Re: Anyone know what's wrong with this code?
« Reply #4 on: October 04, 2009, 08:57:32 AM »
Maybe a simple thing like that a room of Height 1 checks two tiles? i mean (for left to left+height) should perhaps check only (for left to left+height-1) ?

Edit:
since it fails 100% you should use a debugger.
« Last Edit: October 04, 2009, 08:59:54 AM by purestrain »

#### Shaggy

• Rogueliker
• Posts: 65
• Karma: +0/-0
• (╯°□°）╯︵ ┻━┻ <( !@#\$ THIS, I'M OUT. )
##### Re: Anyone know what's wrong with this code?
« Reply #5 on: October 04, 2009, 09:12:20 AM »
well the room size is a minimum of 5, maximum of 12 [including walls]

let me try that and see if it works...
^scuttles off to poke and prod at VB^
^scuttles back^

well, it's kinda working
it passing the clear checks, but not in a good way, considering they overlap like crazy
and for some reason it's only making rooms to the north. hm
Check out my blog at http://NotQuiteADoM.blogspot.com/ !

#### Vanguard

• Rogueliker
• Posts: 1112
• Karma: +0/-0
##### Re: Anyone know what's wrong with this code?
« Reply #6 on: October 04, 2009, 07:31:15 PM »
I don't know very much about VB, but I would recommend writing some diagnostic code - have it give you a message at exactly what point clear is set to false, and have the message include the values used to get there.  It might help figure out what's happening.

A lot of the time with these things the problem doesn't originate in the particular block of code that has a problem, but rather an error elsewhere "presents" itself at that point, making it look like the problem is there.

Anyway, good luck.

#### corremn

• Rogueliker
• Posts: 700
• Karma: +0/-0
• SewerJack Extraordinaire
##### Re: Anyone know what's wrong with this code?
« Reply #7 on: October 06, 2009, 12:47:07 AM »
Surely you can step into it with the debugger.
corremn's Roguelikes. To admit defeat is to blaspheme against the Emperor.  Warhammer 40000 the Roguelike

#### Ex

• IRC Communications Delegate
• Rogueliker
• Posts: 313
• Karma: +0/-0
##### Re: Anyone know what's wrong with this code?
« Reply #8 on: October 06, 2009, 04:56:52 AM »
well the room size is a minimum of 5, maximum of 12 [including walls]

let me try that and see if it works...
^scuttles off to poke and prod at VB^
^scuttles back^

well, it's kinda working
it passing the clear checks, but not in a good way, considering they overlap like crazy
and for some reason it's only making rooms to the north. hm

If all the rooms overlap in the top left corner, it could be a sign that width is set to Left, or height to Top, or that width = Left+width, or Top= Top+width. Any number of these variations would quickly start returning 100% false the further you got away from the top left corner. Can you show the function that is calling this function?

I would suspect since you said north instead of north and left that the problem is that height is set to Top+Height, or that top is set to Top+Height. In any case, your problem is probably due to something related to the Height/Top variables, due to the north specific placement of rooms.

To easily debug this function the "old fashioned" way, call some kind of text or dialog output function in the middle of the function that displays all the functions variables. For instance, right after Clear = true, put some kind of "Display_Text("Top = %d Height = %d",Top,Height)". That way your output would notify you of what is going on inside the function so you can pinpoint what is going wrong.