Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 98 99 [100] 101 102 ... 796

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

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1485 on: February 15, 2012, 03:20:33 pm »

Operator overloading is particularly useful for creating classes for common mathematical constructs. For example, if you create a class to represent a mathematical vector, you may want to override * in the vector class to do piece wise multiplication against other instances of the vector class as well as scalar multiplication. Without overrides, your code would look more like
Vector3 = Vector1.Multiply(Vector2);
Vector2 = Vector1.Multiply(scalar);
or worse (if not implemented as a function at all), instead of the more clean
Vector3 = Vector1 * Vector2;
Vector2 = Vector1 * scalar;
The problem with this is that not everybody always thinks of using an operator when looking for a function, as such to make life easy for everybody, I like to include a function as well, most often like so.

Code: [Select]
public Vector3 Multiply(Vector2 v)
{
return this * v;
}

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1486 on: February 15, 2012, 03:32:42 pm »

Hey... new stupid question.

I want to take a dictionary and return the key that maps to the minimal value.  Any of y'all know how to do that with built-in functions?

Oh wait, I found a way to do this.  Turns out python has a filter() function that makes it easier.

Code: [Select]
dict = {'a': 1, 'b': 2, 'c': 1, 'd': 5, 'e':2, 'f':1}
keys = map(lambda x: x[0], filter(lambda x: x[1] == min(dict.items()[1]), dict.items()))
print keys

Its a bit hard to read, but it does the trick.  the filter method returns a subset of a list for where the lambda function is true, and the map function I'm using to just return the keys, instead of both keys and values.


Any reason you can't just use min(dict, key=dict.get) or something similar?
Logged

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1487 on: February 15, 2012, 03:38:44 pm »

Any reason you can't just use min(dict, key=dict.get) or something similar?

That'll only return one of the keys, rather than all of them.  That is cool though, I didn't realize min could take a method parameter like that.
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1488 on: February 15, 2012, 03:40:41 pm »

That is cool. Virex knows all the sweet moves.

SolarShado

  • Bay Watcher
  • Psi-Blade => Your Back
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1489 on: February 15, 2012, 11:29:52 pm »

Question for my fellow Java programmers:

In making the game area for a TBS game (something I've done a couple times (I'm horrible about starting project and never finishing them)), there seem to be 2 basic ways to go about it. You either have one big component that encompasses the entire field, with a fairly complex draw routine and contains a 2d array to track each square. Or you could use a GridLayout to organize a number of smaller components, which each encompass only a single square.

The question is this: which option seems 'better'?
The first sounds like it would use less memory (fewer objects, or at least smaller objects), at the expense of a fair bit more programmer work (the drawing routine).
The second might use more memory, but (at least on the surface) sounds a lot easier to implement...
Logged
Avid (rabid?) Linux user. Preferred flavor: Arch

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1490 on: February 16, 2012, 02:16:34 am »

There are several ways of doing this.  More than your two, anyway.  But lets just focus on those two and why either is probably okay in the grand scheme.

It's always good practice to be efficient.  It's worth writing a complicated drawing method if it saves a ton of memory.  The fact is that unless you've done something really weird in your code, that drawing method should be relatively straight forward.  You can probably do it with a double- or triple- for loop and just make sure you're drawing things in the correct order.  I'm envisioning this being a few for loops with no more than a half dozen draw commands in them.  If you think you need something more complex than that, ask yourself why.

Realistically, you don't necessarily go with "good practice".  More realistic practice is to do whatever you can do the easiest/fastest.  Go with that unless it actually causes problems somewhere.  Java is admittedly slow compared to native applications because it has to go through the JVM.  If using that much memory causes perceptible lag, then try to find out why.  You might even be able to speed things up with a little bit of parallel.  Point is, Java should handle that many objects just fine.  By far, the slowest part of the operation is the io.  Drawing takes forever compared to the internal stuff it's doing.

Take the efficient path if you have the time and patience.  That more realistic path exists because in the real world of programming, you're usually under a time constraint.  Either way, just try to be simple and straight forward.  If you can't be "simple", then be explicit.  If the compiler can figure out what you want, it might be able to optimize it enough to handle efficiency problems.  I'd be happy to give advice on either path (or to devise an alternative) if you want or end up having problems.
Logged

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #1491 on: February 16, 2012, 05:31:23 am »

I have a choice. I've an interview for a placement tomorrow. It's a C++ developer role, and those kind of placements are rare, but I think I have a decent chance. My alternatives are these .NET developer roles. The main downside of the C++ developer placement is it is in London which means I'd have to live outside of London and catch a train every day to go to work. The .NET roles are in a much cheaper area to live in for the same quality of life than anywhere within reasonable training distance to London. Seriously, for what I'm paying for my tiny flat at the moment, I could practically rent a one-bedroom house further north.

Decisions decisions :(
« Last Edit: February 16, 2012, 05:41:11 am by MorleyDev »
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1492 on: February 16, 2012, 05:40:56 am »

What one gives you better pay?
I mean I'm sort of going to support the .net job, because I sold my soul to Microsoft and I am no longer able to give objective advice. My dark lords demand I endorse their propriety system unquestionably, regardless of implementation.

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1493 on: February 16, 2012, 07:07:51 am »

And I'm going to say avoid .NET like the plague. And London is cool, yo. You really want to move out of the awesomeness of the city? City jobs tend to pay a metric crapton more as well (because cost of living is obviously higher and they need to to attract good talent). So at least find that out first.
Logged

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: if self.isCoder(): post() #Programming Thread
« Reply #1494 on: February 16, 2012, 08:57:33 am »

Go for quality of life. That may mean staying in London where they have things to do, or going farther out and having more space, or going farther out at being able to build up savings. The difference in language most likely isn't going to be the primary factor in what position will be better for you.
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.

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1495 on: February 16, 2012, 10:49:02 am »

Go for the job you think you'll hate least.  You really aren't expected to like your job, but location and pay start to mean very little if you outright hate your job.
Logged

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #1496 on: February 16, 2012, 11:05:27 am »

All of them are city jobs. It's just the other city is Nottingham. I know lots of people there, being from there and all, and could easily live with my parents for the year or, if me and my dad can't stay in close space for that long without trying to kill each other, get a flat for a well over a hundred a month less than down south. It's a placement job, which means the pay is usually shit and the C++ is no exception being about 15k. The .NET ones would be actual junior jobs retrofitted to act as a placement which may mean higher pay (one of my old teachers, works at one and knows the others so being a cool guy who I still keep in touch with, recommended me).

The only thing really making me go "buuut" is the question of which is better on the CV...
« Last Edit: February 16, 2012, 11:10:22 am by MorleyDev »
Logged

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1497 on: February 16, 2012, 11:09:05 am »

.NET is good if you want to do more .NET.

C++ offers a lot more variety of potential routes for career progression, imo.
Logged

MaximumZero

  • Bay Watcher
  • Stare into the abyss.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1498 on: February 16, 2012, 11:10:28 am »

Go for the job you think you'll hate least.  You really aren't expected to like your job, but location and pay start to mean very little if you outright hate your job.
QFT
Logged
  
Holy crap, why did I not start watching One Punch Man earlier? This is the best thing.
probably figured an autobiography wouldn't be interesting

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1499 on: February 16, 2012, 11:12:01 am »

Also, if you LIKE c#, being (semi)locked into a c# career isn't exactly a deal breaker.The specifics of the job are more important for your CV than the language.

(Also, seriously - pick the one that pays the most if you're worried about career progression)
Logged
Pages: 1 ... 98 99 [100] 101 102 ... 796