The more I look at my code, the more I think it should be fool proof. I get the feeling that something else is feeding it bad data.
(1) Scan the array and make two Lists, for the X and Y positions of each door tile, and make a Doors counter.
(2) Make a counting-array for the number of doors, and shuffle it.
(3) Count down the shuffled array, send the X and Y positions of the resulting random door and another door to the Draw Corridors method.
(4) Check to see if Current X and Y (initialized as Starting X and Y) are different from Objective X and Y
(A) If X's are equal, walk up or down as appropriate.
(a1) If the next tile up/down is replaceable, replace it.
(a2) If the next tile up/down is not replaceable, walk left or right until it is, then start over from (4).
(B) If Y's are equal, walk left or right as appropriate.
(b1) If the next tile left/right is replaceable, replace it.
(b2) If the next tile left/right is not replaceable, walk up or down until it is, then start over from (4).
(C) If neither are equal, randomly pick one of two directions towards the Objective, and act like A/B.
(c1 and c2) Copy/pasted from a/b.
The weird part is, it seems to be a1 and b1 (well, just a1 as far as I can tell) that throws an exception, when it eventually makes an out-of-bounds reference on an array as part of a do/while conditional. While preventing this from throwing should be possible, I can't figure out why it would be happening anyway. That do-while is only called if the operating-tile is in the same row/column as the objective tile, and the conditional checks to see if the next tile can be replaced. That the conditional is being thrown out-of-bounds means it's walking to the edge of the map and tries to keep going, even though the other conditional of the loop is that it stops if it reaches the objective.
Something is not working right, but I can't figure out why. All of the logic at play should be working fine. I guess I'll have to start flipping operators or something.
-Can I separate some of this into sub methods? E.g, instead of just BuildLevel() that does everything, you have BuildRooms(), BuildHalls(), AddEnemies(), AddItems() and so on. This is making your code 'atomic' and helps a lot.
That's exactly what I'm doing. The 353 lines of code is
just the part that draws the corridors. It has a lot of similar code sure, but I can't think of a way to break it down into more internal methods that would actually make the code shorter by more than a dozen lines.
EDIT: Holy fuck, I am retarded. Okay I'm going to try something, this should do it.
EDIT2: Moved the problem, but I think I'm getting closer to the issue then.