Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 339 340 [341] 342 343 ... 796

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

ECrownofFire

  • Bay Watcher
  • Resident Dragoness
    • View Profile
    • ECrownofFire
Re: if self.isCoder(): post() #Programming Thread
« Reply #5100 on: October 05, 2013, 02:14:52 pm »

Or just replace break with continue.
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5101 on: October 05, 2013, 03:40:43 pm »

Or just replace break with continue.
Continue works like that? Why do I not know about this. :(
Logged

ECrownofFire

  • Bay Watcher
  • Resident Dragoness
    • View Profile
    • ECrownofFire
Re: if self.isCoder(): post() #Programming Thread
« Reply #5102 on: October 05, 2013, 07:07:18 pm »

Or just replace break with continue.
Continue works like that? Why do I not know about this. :(
Yep, continue just continues the loop by skipping the rest of that particular iteration. So it tests the condition again in a while or does the increment parameter in for and tests the condition.

Keep in mind in C++ it only breaks the lowest loop you're in. Some other languages, such as Java and Rust, let you label loops so you can break out of nested loops with one line. But nested loops are generally not good and if you find yourself using one, you should probably refactor it...

It would be cool to have a break 2; or something similar, though.
Logged

freeformschooler

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5103 on: October 05, 2013, 07:13:23 pm »

Keep in mind in C++ it only breaks the lowest loop you're in. Some other languages, such as Java and Rust, let you label loops so you can break out of nested loops with one line. But nested loops are generally not good and if you find yourself using one, you should probably refactor it...

I hear that a lot, actually - does that apply to all loops? Like, say I'm looping through a 2D array in some language (an array of arrays). Would having a for loop inside a for loop for each dimension of the array be bad form? If so, what's the best alternative? Use a different container than arrays? Structure the first for loop differently somehow?
Logged

ECrownofFire

  • Bay Watcher
  • Resident Dragoness
    • View Profile
    • ECrownofFire
Re: if self.isCoder(): post() #Programming Thread
« Reply #5104 on: October 05, 2013, 07:42:10 pm »

Keep in mind in C++ it only breaks the lowest loop you're in. Some other languages, such as Java and Rust, let you label loops so you can break out of nested loops with one line. But nested loops are generally not good and if you find yourself using one, you should probably refactor it...

I hear that a lot, actually - does that apply to all loops? Like, say I'm looping through a 2D array in some language (an array of arrays). Would having a for loop inside a for loop for each dimension of the array be bad form? If so, what's the best alternative? Use a different container than arrays? Structure the first for loop differently somehow?
It's generally slower and having a large amount of indentation looks gross and is harder to read. Though the compiler is probably just going to convert it to a single loop either way. If it's only 2 or 3 layers deep then it's not that bad. 4 is where you probably want to restructure.

Basically you use a flat array and two small wrapper functions to help convert coordinates or whatever to/from the array index. Then you just loop through the array once, using the wrapper functions as necessary.

But as always, your mileage may vary. Just use whatever makes sense and remember that premature optimization is the root of all evil...

More importantly than that, arr[j][k][l] should always loop through j in the outermost loop and l in the innermost. It's always both faster and easier to understand.
Logged

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5105 on: October 05, 2013, 08:40:43 pm »

It would be cool to have a break 2; or something similar, though.

Yes, agreed completely.  It doesn't come up that often, but when it does it's usually a pain to work around the lack of this feature, unless you want to stoop to using gotos.  When I first discovered it in other languages (PHP in this case), I was pretty surprised that such a feature existed but have found it useful more times than I expected.

I also agree with trying to minimize loop nesting.  Walking multidimensional arrays is one thing, but the handful of times I found myself iterating four levels deep and using l (lower case L) as the induction variable I felt immensely dirty and had to refactor the code.
Logged
Through pain, I find wisdom.

freeformschooler

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5106 on: October 05, 2013, 08:47:59 pm »

Oh, that does make sense, ECoF. Thanks.
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #5107 on: October 05, 2013, 09:25:08 pm »

The ideal way to break out of a nested loop is to move the whole thing into a function and return when you wanted to break 2.
At least that's what I think.

Goto works well, too, though. Sometimes it's simpler than other solutions.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5108 on: October 06, 2013, 01:38:55 am »

While I generally don't like having unnecessary variables, if I have reason to use embedded loops, my preferred means to break out is to use that extra variable.
Code: [Select]
var stop = 0
while(continuationConditionOuter && !stop)
 while(continuationConditionInner && !stop)
  if(stopCondition)
   stop = 1
Efficiency aside, sometimes embedded loops make the code easier to read or write.  It's generally alright to write inefficient code so long as you're clear enough about what you're doing that the compiler can figure out what you want and rewrite it to be efficient for you.
Logged

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5109 on: October 06, 2013, 05:15:01 pm »

If I want my program to print things in a more organized fashion (printing stocks, then structures, then population, etc instead of printing all of the contents of the data map in alphabetical order), would making a bunch of separate maps be the most efficient way to do it?

Also, there are some structures in my game that have variables themselves (namely mines, which have a finite amount of ore that can be extracted from them, as well as a specific type of mineral that's being extracted). Could I make a struct for that specific structure, then read the variables of that struct from a data file and assign them to them that way or something?
Logged

lue

  • Bay Watcher
  • [PREFSTRING:missing right bracket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5110 on: October 06, 2013, 06:22:17 pm »

If I want my program to print things in a more organized fashion (printing stocks, then structures, then population, etc instead of printing all of the contents of the data map in alphabetical order), would making a bunch of separate maps be the most efficient way to do it?

Depends. If all that stocks, structures, and population data doesn't fit well in one map, then you should separate them anyway. Pretty printing largely depends on writing a lot of sometimes unpleasant code, and doesn't usually get affected much by how the data is stored. If need be you can create a bunch of pointers/variables to shorten data acces from, say, array[1][2][46][2][8][2][0] to apointer[0] (a bit extreme of an example, but you get the picture :) ).

Also, there are some structures in my game that have variables themselves (namely mines, which have a finite amount of ore that can be extracted from them, as well as a specific type of mineral that's being extracted). Could I make a struct for that specific structure, then read the variables of that struct from a data file and assign them to them that way or something?

Structures generally have variables themselves :) . On a more serious note, you surely can. My personal favorite is through binary files, though if you're not up for that text files may be a better option. Some ideas:

INI files are a quick and easy text file format, that you could even write a parser inside the code, if you're willing to spend the small time needed. XML files are a bit heavy-handed unless you're storing a lot of hierarchical data. You'll definitely want to find someone else's parser library for that.

I'm not aware of any pre-designed general binary formats, so if you want to go the binary route you'd have to design the format yourself. (it's up to you whether or not that's a fun way to pass the time :P)
Logged
Post not guaranteed accurate or pristine for all of time.
Sigtext. Enticing, yes? If you do not know where things I have "sigged" go, this page will explain.

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5111 on: October 06, 2013, 08:11:29 pm »

Would I necessarily have to do away with the map that all of the inputs are fed to if I was using structs for some structures? I mean I don't see how I could differentiate between normal structures and the ones that have additional variables if all of the lines are being fed into a map. Would I have to break up the lines in the holdings file, then run through them word-by-word in order to categorize them like that? Blargh >.<
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #5112 on: October 07, 2013, 12:01:53 am »

Hmm, anyone know of a resource about steering algorithms for ships? I can't easily think of a way to make ships sorta spiral or circle a certain distance away from a point...
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5113 on: October 07, 2013, 12:35:35 am »

Hmm, anyone know of a resource about steering algorithms for ships? I can't easily think of a way to make ships sorta spiral or circle a certain distance away from a point...

Circle: For distance r from a point, x = r*cos(t), and y = r*sin(t) for some time t

Spiral: For distance r from a point, increasing by a each rotation, x = (r + a*t/(2*pi))*cos(t), and y = (r + a*t/(2*pi))*sin(t) for some time t

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #5114 on: October 07, 2013, 02:15:58 am »

But a ship can't magically change its position. It has variables of bearing, acceleration, and speed which it can change to a degree.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward
Pages: 1 ... 339 340 [341] 342 343 ... 796