Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 33 34 [35] 36 37 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 887890 times)

Aqizzar

  • Bay Watcher
  • There is no 'U'.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #510 on: January 13, 2012, 07:33:16 pm »

Making my random-corridor-drawing program.

353 lines of code (about a third of it just brackets).  As usual, I've copy/pasted the Hell out of lines I need to change.  Not quite sure how to change them though - I need to insert some boundary checking to make sure it never tries to check a location outside of an array.  This is going to take some head-scratching.
Logged
And here is where my beef pops up like a looming awkward boner.
Please amplify your relaxed states.
Quote from: PTTG??
The ancients built these quote pyramids to forever store vast quantities of rage.

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: if self.isCoder(): post() #Programming Thread
« Reply #511 on: January 13, 2012, 07:45:50 pm »

I am working on design docs for merging container, parent consolidation container, tsdf container management systems into a single unified hazwaste container management system. Fun. And I should not be here, but I am going insane.
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #512 on: January 13, 2012, 08:07:54 pm »

353 lines of code
Sounds big and bulky. Here is what you should be thinking right now
-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.

-Can I move my logic off into a relevant class? E.g, instead of having your levelGen method add walls and doors to rooms, you have a room class that can do that instead. Making the rooms build their own walls and floor and doors is not only easier, it is a better programming practice.

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #513 on: January 13, 2012, 08:18:19 pm »

Making my random-corridor-drawing program.

353 lines of code (about a third of it just brackets).  As usual, I've copy/pasted the Hell out of lines I need to change.  Not quite sure how to change them though - I need to insert some boundary checking to make sure it never tries to check a location outside of an array.  This is going to take some head-scratching.

Does C# not have anything like Java's length attribute for arrays?

Aqizzar

  • Bay Watcher
  • There is no 'U'.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #514 on: January 13, 2012, 08:21:47 pm »

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.
« Last Edit: January 13, 2012, 08:39:14 pm by Aqizzar »
Logged
And here is where my beef pops up like a looming awkward boner.
Please amplify your relaxed states.
Quote from: PTTG??
The ancients built these quote pyramids to forever store vast quantities of rage.

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #515 on: January 13, 2012, 08:24:09 pm »

Does C# not have anything like Java's length attribute for arrays?

It does.
Code: [Select]
array.Length;will get you the total number of items in the array, while
Code: [Select]
array.GetLength(int index);will get you the length for a specific dimension.

So, for example, if I had a 2d array that is 4 * 5, then array.Length = 20, array.GetLength(0) = 4 and array.GetLength(1) = 5

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #516 on: January 13, 2012, 08:29:45 pm »

-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.
It doesn't matter if breaking the code up makes it shorter, maintainability and simplicity are more important. If you've got a one-liner that you're using a few times, writing a function for it may take more place, but you'll hve a nice mneomnic of what the code does instead of a cryptic one-liner.
Logged

Aqizzar

  • Bay Watcher
  • There is no 'U'.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #517 on: January 13, 2012, 09:59:36 pm »

I'm back in Stump City again.  I can see how I could break some of this code out into even more method calls, but that's immaterial to fixing the actual problem.  Take a look with me.

Code: (Imagine this same construction similarly-repeated twenty three more times.) [Select]
do{
output[xCurrent, (yCurrent - 1)] = new MapTile(002);
yCurrent--;
}while ((yCurrent != 0) || (yCurrent != yObjective) || (output[xCurrent, (yCurrent - 1)].GetStyleType() == 001));

Here's your challenge: Find a way to rewrite this same code, so that if will continue to move "up" in the 2D array (output) so long as (output[xCurrent, (yCurrent - 1)].GetStyleType() == 001) is true, until it reaches the objective, without making either reference to (output) hit an out-of-bounds array slot.  If I can figure out how to do that, I can modify it as necessary for movement in every other direction, but I can't see how to wrap this code inside-out of itself so that it updates itself but never looks at an invalid location (meaning an array y-dimension of less than zero).
Logged
And here is where my beef pops up like a looming awkward boner.
Please amplify your relaxed states.
Quote from: PTTG??
The ancients built these quote pyramids to forever store vast quantities of rage.

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #518 on: January 13, 2012, 10:06:28 pm »

I'm back in Stump City again.  I can see how I could break some of this code out into even more method calls, but that's immaterial to fixing the actual problem.  Take a look with me.

Code: (Imagine this same construction similarly-repeated twenty three more times.) [Select]
do{
output[xCurrent, (yCurrent - 1)] = new MapTile(002);
yCurrent--;
}while ((yCurrent != 0) || (yCurrent != yObjective) || (output[xCurrent, (yCurrent - 1)].GetStyleType() == 001));

Here's your challenge: Find a way to rewrite this same code, so that if will continue to move "up" in the 2D array (output) so long as (output[xCurrent, (yCurrent - 1)].GetStyleType() == 001) is true, until it reaches the objective, without making either reference to (output) hit an out-of-bounds array slot.  If I can figure out how to do that, I can modify it as necessary for movement in every other direction, but I can't see how to wrap this code inside-out of itself so that it updates itself but never looks at an invalid location (meaning an array y-dimension of less than zero).

Code: (Attempt 1) [Select]
for(yCurrent; yCurrent != yObjective && yCurrent >= 1 && output[xCurrent, yCurrent-1].GetStyleType() != 001; yCurrent--)
output[xCurrent, (yCurrent - 1)] = new MapTile(002);

Alternatively:

Code: (Attempt 2) [Select]
do {
        output[xCurrent, --yCurrent] = new MapTile(002);
} while(yCurrent >= 0 && yCurrent != yObjective && output[xCurrent, yCurrent].GetStyleType() != 001);
« Last Edit: January 13, 2012, 10:10:36 pm by Mego »
Logged

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #519 on: January 13, 2012, 10:08:56 pm »

I'm back in Stump City again.  I can see how I could break some of this code out into even more method calls, but that's immaterial to fixing the actual problem.  Take a look with me.

Code: (Imagine this same construction similarly-repeated twenty three more times.) [Select]
do{
output[xCurrent, (yCurrent - 1)] = new MapTile(002);
yCurrent--;
}while ((yCurrent != 0) || (yCurrent != yObjective) || (output[xCurrent, (yCurrent - 1)].GetStyleType() == 001));

Here's your challenge: Find a way to rewrite this same code, so that if will continue to move "up" in the 2D array (output) so long as (output[xCurrent, (yCurrent - 1)].GetStyleType() == 001) is true, until it reaches the objective, without making either reference to (output) hit an out-of-bounds array slot.  If I can figure out how to do that, I can modify it as necessary for movement in every other direction, but I can't see how to wrap this code inside-out of itself so that it updates itself but never looks at an invalid location (meaning an array y-dimension of less than zero).

It sounds like what you want is to change all those ORs to ANDs.  The first two check bounds while the third checks whether you've reached the destination and the fourth checks GetStyleType().  All must be true or it doesn't let you do anything.  That's what you wanted, right?
Code: [Select]
while((yCurrent >= 0) && yCurrent <= output.GetLength(yCurrent) && (yCurrent != yObjective) && (output[xCurrent, (yCurrent - 1)].GetStyleType() == 001)) {
output[xCurrent, (yCurrent - 1)] = new MapTile(002);
yCurrent--;
}

EDIT: Pay attention, Stargrasper.
« Last Edit: January 13, 2012, 10:16:20 pm by Stargrasper »
Logged

Darvi

  • Bay Watcher
  • <Cript> Darvi is my wifi.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #520 on: January 13, 2012, 10:11:20 pm »

In fact you could drop the >=1 or !=0 entirely. I think. My brain is tired at the moment.
Logged

Aqizzar

  • Bay Watcher
  • There is no 'U'.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #521 on: January 13, 2012, 10:16:56 pm »

It sounds like what you want is to change all those ORs to ANDs.  The first two check bounds while the third checks GetStyleType().  All must be true or it doesn't let you do anything.  That's what you wanted, right?

Yeah, I originally had them as ANDs (with some different qualifiers) and it seemed to work better (i.e. crash in a deeper place) when I changed them to ORs so I got thinking they should stay that way.  I'll try out your and Mego's offers and see what works.  Of course, until I actually see something reach the screen, I'll have no idea if this is even a good plan or not.

Code: (Attempt 1) [Select]
for(yCurrent; yCurrent != yObjective && yCurrent >= 1 && output[xCurrent, yCurrent-1].GetStyleType() != 001; yCurrent--)
output[xCurrent, (yCurrent - 1)] = new MapTile(002);

Alternatively:

Code: (Attempt 2) [Select]
do {
        output[xCurrent, --yCurrent] = new MapTile(002);
} while(yCurrent >= 0 && yCurrent != yObjective && output[xCurrent, yCurrent].GetStyleType() != 001);

Exactly the kind of stuff I would never even think of.  When I start down a path, I get really invested in it, and have a hard time thinking of stuff that's fundamentally different.
Logged
And here is where my beef pops up like a looming awkward boner.
Please amplify your relaxed states.
Quote from: PTTG??
The ancients built these quote pyramids to forever store vast quantities of rage.

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #522 on: January 13, 2012, 10:22:59 pm »

I learned methods to iterate through arrays the hard way, refusing to try C++-0x (aka C++-11) until the standard was published. Loops are my specialty.

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #523 on: January 13, 2012, 10:24:14 pm »

You need them as ANDs.  That way, it asks if all of them are true.  If you use ORs, goes ahead if any one or more are true.

Mego's examples don't check the array bounds.  I forgot that too, initially.  That's the edit in my post.  Note that I changed the first entry in that loop and added the second for boundary checks.

I also don't like Mego's examples for stylistic reasons.  I was taught not to use for loops if you're checking more than one condition.  And I was taught not to use do loops.  Why exactly did you have that as a do loop initially, anyway?  A while loop is a check that stops the player from doing anything illegal to begin with and a do loop means the content will happen at least once, so you need to make sure it's a legal move first.
Logged

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #524 on: January 13, 2012, 10:25:34 pm »

Depending on how c# handles out of bound array values, this might work:

Code: [Select]
while(output[xCurrent, yCurrent] and output[xCurrent, yCurrent].GetStyleType() == 001) {
output[xCurrent, yCurrent] = new MapTile(002);
yCurrent--;
}

In ruby, an out of bound array returns a null value, which also counts as a false when testing conditionals.  Not sure if c# does the same with its arrays though.
« Last Edit: January 13, 2012, 10:29:14 pm by Levi »
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout
Pages: 1 ... 33 34 [35] 36 37 ... 796