What are you playing?
Moderators: Master_Kale, TNM Team
Re: What are you playing?
Well, as far as I can see, you have a sharp lesser than in your clause which means i = array.length-1 would be rejected, so you don't get the last element.
If you use a sharp lesser than and have it append after i, as in for (i= 0; i< array.length; i++), you should be ok.
Generally, I don't like the old-timey for loops. I quite prefer the foreach type loops introduced in modern languages, because they leave less room open for mistakes: You specify
"foreach object o in objects"{
//shit is done to o here
}
Either give me that, or a while loop, if you need to do some advanced calculation before you decide to do another loop. It might not be obvious, but almost all modern languages will actually evaluate a method called within the header, so you can do something like
while(calculations.shouldTheLoopRun(this)){
//run the loop
}
where shouldTheLoopRun returns a bool value, of course. With a good IDE set up, people who wonder about how the loop works will just click the call, hit F3, and be taken to the codepage where the method is programmed. It's generally a heavy-handed solution to the problem, but it's much preferable to a big old bolean algebra thing in the header with lots of &&'s and ||'s in it.
You can see that I pass "this" into the method call in the header - that's to have a way to access the variables on the object running the code, but you could also simply have the evaluation in another place in the same class (probably more useful), in which case you wouldn't the object to pass a reference to itself.
If you use a sharp lesser than and have it append after i, as in for (i= 0; i< array.length; i++), you should be ok.
Generally, I don't like the old-timey for loops. I quite prefer the foreach type loops introduced in modern languages, because they leave less room open for mistakes: You specify
"foreach object o in objects"{
//shit is done to o here
}
Either give me that, or a while loop, if you need to do some advanced calculation before you decide to do another loop. It might not be obvious, but almost all modern languages will actually evaluate a method called within the header, so you can do something like
while(calculations.shouldTheLoopRun(this)){
//run the loop
}
where shouldTheLoopRun returns a bool value, of course. With a good IDE set up, people who wonder about how the loop works will just click the call, hit F3, and be taken to the codepage where the method is programmed. It's generally a heavy-handed solution to the problem, but it's much preferable to a big old bolean algebra thing in the header with lots of &&'s and ||'s in it.
You can see that I pass "this" into the method call in the header - that's to have a way to access the variables on the object running the code, but you could also simply have the evaluation in another place in the same class (probably more useful), in which case you wouldn't the object to pass a reference to itself.
Re: What are you playing?
Ffffffffuuuuucccccccckkkkkk mmmmmmmmmeeeeeeeee why didn't I think of just using overloads?! I know they exist and I know how they work but I totally haven't thought to use them to make certain parameters optional when the method is called.AEmer wrote:The solution is simple, and elegant: If you want default values in your headers, for parameter 4, simply overload it with a 3-parameter method of the same name, and have it call the 4-parameter method with the 3 first parameters and your default value, and return the result the 4-parameter method returns.
I am clearly not a very clever person.
Jonas Wæver
Chief Poking Manager of TNM
I've made some videogames:
Expeditions: Rome
Expeditions: Viking
Expeditions: Conquistador
Clandestine
Chief Poking Manager of TNM
I've made some videogames:
Expeditions: Rome
Expeditions: Viking
Expeditions: Conquistador
Clandestine
Re: What are you playing?
Dude, trust me, we've all been there. Programmers don't invent the wheel themselves each time because then every single car we ever made would be a rickety dink pile of scrap.
In fact, the method I outlined...passing data via parameter...is only useful in a fairly limited number of circumstances. It's not a particularly good or particularly general solution, but a lot of the best libraries use similar constructions, and I certainly think it's going to be viable for real time applications.
In general, what you really want to do is to avoid long method headers at all. What you want to do is to pass around objects that contain the variables you would've passed as parameters...it sounds simple enough, but it often results in much better code. We like to use the term "loose coupling" - the idea that our methods and classes are tied to as few other classes and methods as possible.
That obviously decreases the number of sideeffects of code changes, which makes code easier to update and maintain.
It's not always the best choice, but it should be the default choice; that method of coding has a name: The strategy pattern.
If you can code with the strategy pattern, you already know enough to pass most programming 101 courses
In fact, the method I outlined...passing data via parameter...is only useful in a fairly limited number of circumstances. It's not a particularly good or particularly general solution, but a lot of the best libraries use similar constructions, and I certainly think it's going to be viable for real time applications.
In general, what you really want to do is to avoid long method headers at all. What you want to do is to pass around objects that contain the variables you would've passed as parameters...it sounds simple enough, but it often results in much better code. We like to use the term "loose coupling" - the idea that our methods and classes are tied to as few other classes and methods as possible.
That obviously decreases the number of sideeffects of code changes, which makes code easier to update and maintain.
It's not always the best choice, but it should be the default choice; that method of coding has a name: The strategy pattern.
If you can code with the strategy pattern, you already know enough to pass most programming 101 courses
- Jetsetlemming
- Illuminati
- Posts: 2398
- Joined: Mon Sep 18, 2006 9:11 pm
- Contact:
Re: What are you playing?
No, because the increment is at the start of the loop but after the logic check, typically. ++i makes the increment happen at the end of a loop, thus BEFORE the next logic check starting the next loop. If I didn't include either a -1 or ++i on a for looping on an array, it'd end up on its final iteration hitting array[length], which is an index one higher than the array actually has, since they start counting at 0. This is basically something I end up running into every time I have loops and arrays or lists, most recently with my boardgame- the tiles on the boards are stored in an ObservableCollection<Tile> variable (ObservableCollection is a standard list except with some extra bits to let it use the standard Property Has Changed event WPF elements expect- you can't bind a normal list to a wpf element and expect it to update), and I forgot to include that -1 on my check to see if the player's turn calculation has reached the end of the board, thus has no more valid spaces to move forward, thus that player wins. It resulted in a crash to signify a victory every time.AEmer wrote:Well, as far as I can see, you have a sharp lesser than in your clause which means i = array.length-1 would be rejected, so you don't get the last element.
Re: What are you playing?
This is so true, thanks for this information!Jetsetlemming wrote:No, because the increment is at the start of the loop but after the logic check, typically. ++i makes the increment happen at the end of a loop, thus BEFORE the next logic check starting the next loop. If I didn't include either a -1 or ++i on a for looping on an array, it'd end up on its final iteration hitting array[length], which is an index one higher than the array actually has, since they start counting at 0. This is basically something I end up running into every time I have loops and arrays or lists, most recently with my boardgame- the tiles on the boards are stored in an ObservableCollection<Tile> variable (ObservableCollection is a standard list except with some extra bits to let it use the standard Property Has Changed event WPF elements expect- you can't bind a normal list to a wpf element and expect it to update), and I forgot to include that -1 on my check to see if the player's turn calculation has reached the end of the board, thus has no more valid spaces to move forward, thus that player wins. It resulted in a crash to signify a victory every time.AEmer wrote:Well, as far as I can see, you have a sharp lesser than in your clause which means i = array.length-1 would be rejected, so you don't get the last element.
Re: What are you playing?
first iteration, i = 0, i < array.length. Loop is run.for (int i = 0; i < array.Length - 1; i++)
iteration number array.Length - 2 (second to last iteration). i = array.Length - 2, which is less than array.length-1.
The loop runs, i++.
i = array.Length - 1. This is not less than array.Length -1. The loop doesn't run. You don't get the last entry of your array if you use array within the loop because the loop terminates before it runs with the check array[array.Length -1] which is going to be there when the array is 0-indexed.
for (int i = 0; i < array.Length; i++)
This is good enough.
for (int i = 0; i =< array.Length - 1; i++)
or this. Or theoretically
for (int i = 0; i != array.Length; i++)
this would be good enough as well. Or even:
for (int i = -1; i < array.Length - 1; ++i)
this.
Unless I've made a grievous error and I can't remember the old timey for loops
- Jetsetlemming
- Illuminati
- Posts: 2398
- Joined: Mon Sep 18, 2006 9:11 pm
- Contact:
Re: What are you playing?
You should probably experiment with what you're claiming, before claiming it, then, if you're out of habit with fors. I use them every day, because fors are pro imo, so I'm pretty sure with what I say.
- Hassat Hunter
- Illuminati
- Posts: 2182
- Joined: Fri Apr 10, 2009 1:20 pm
Re: What are you playing?
Wasn't this "what are you playing?" instead of "what are you making?"
Anyways, didn't really follow everything going on, but here's an example on how KOTOR does int++, and loops... might be related to what you guys discuss. Not sure if your code uses the same workway...
Anyway, still remaining to start on JC2 endgame / Borderlands DLC...
Anyways, didn't really follow everything going on, but here's an example on how KOTOR does int++, and loops... might be related to what you guys discuss. Not sure if your code uses the same workway...
Code: Select all
int int2 = 0;
while ((int2 < 12)) {
if (IsNPCPartyMember(int2)) {
RemoveNPCFromPartyToBase(int2);
}
(int2++);
}
Can somebody tell me how I can get a custom avatar?
Oh wait, I already got one...
Oh wait, I already got one...
Re: What are you playing?
*Solemnly points to name of forum*Hassat Hunter wrote:Wasn't this "what are you playing?" instead of "what are you making?"
Jonas Wæver
Chief Poking Manager of TNM
I've made some videogames:
Expeditions: Rome
Expeditions: Viking
Expeditions: Conquistador
Clandestine
Chief Poking Manager of TNM
I've made some videogames:
Expeditions: Rome
Expeditions: Viking
Expeditions: Conquistador
Clandestine
Re: What are you playing?
JSL & AEmer, is it possible that the behaviour of ++i and i++ varies from language to language? I mean, if JSL is claiming that where you put the plusses affects when in the "for loop" the increment happens, then maybe that's something that is specific to the language he's using? (Coz the internet seems to say there's no difference in loops -the increment happens after a run through the loop either way in java and all C variants..the consensus is that ++i is more efficient, but that compilers take i++ and turn it into ++i anyway)
All the internet says is that n= i++; means that i gets incremented but n is now whatever i was before (i.e. the increment happens after the assignment), whereas n= ++i; means that both n and i are the same value (increment happens before the assignment). In for loops there generally is no assignment within the context of the loop arguments themselves, so I can't see what difference it'd really make.
As far as I can tell, AEmer's approach is what I use for everything, and I'd like to think that I'd've noticed missing array elements by now. I have found that I sometimes get crashes when iterating through multiskins, though, which makes me wonder if something like
for(i=0;i<8;i++)
{
whatever=multiskins;
}
is problematic because while it never actually looks for multiskins[8] (and I've checked this) because the conditions for the loop are no longer met, it might nevertheless entertain the concept of a "multiskins[8] situation"?
I don't know, but I'm curious.
All the internet says is that n= i++; means that i gets incremented but n is now whatever i was before (i.e. the increment happens after the assignment), whereas n= ++i; means that both n and i are the same value (increment happens before the assignment). In for loops there generally is no assignment within the context of the loop arguments themselves, so I can't see what difference it'd really make.
As far as I can tell, AEmer's approach is what I use for everything, and I'd like to think that I'd've noticed missing array elements by now. I have found that I sometimes get crashes when iterating through multiskins, though, which makes me wonder if something like
for(i=0;i<8;i++)
{
whatever=multiskins;
}
is problematic because while it never actually looks for multiskins[8] (and I've checked this) because the conditions for the loop are no longer met, it might nevertheless entertain the concept of a "multiskins[8] situation"?
I don't know, but I'm curious.
- Hassat Hunter
- Illuminati
- Posts: 2182
- Joined: Fri Apr 10, 2009 1:20 pm
Re: What are you playing?
Point taken.Jonas wrote:*Solemnly points to name of forum*
Completed Just Cause II. Apparently the final 2 missions boosted my completion rate from 95% to 98%. Fine enough, no?
Can somebody tell me how I can get a custom avatar?
Oh wait, I already got one...
Oh wait, I already got one...
Re: What are you playing?
That's an A+ with a commendation.Hassat Hunter wrote:
Completed Just Cause II. Apparently the final 2 missions boosted my completion rate from 95% to 98%. Fine enough, no?
Growing old is inevitable.......Growing up is optional
Re: What are you playing?
In Java, the difference between o++ and ++o is the return value. one returns o+1, the other returns o, but both increase o by one.
"Delays are temporary; mediocrity is forever."
odio ergo sum
odio ergo sum
Re: What are you playing?
So for instance
i=0;
n=i++;
gives n = 0, i = 1, right? And
i=0;
n=++i;
gives n = 1, i = 1, right?
But
i=0;
i++;
n=i;
and
i=0;
++i;
n=i;
both give n = 1, i = 1, right? Because whatever's happening to i has already happened.
i=0;
n=i++;
gives n = 0, i = 1, right? And
i=0;
n=++i;
gives n = 1, i = 1, right?
But
i=0;
i++;
n=i;
and
i=0;
++i;
n=i;
both give n = 1, i = 1, right? Because whatever's happening to i has already happened.
Re: What are you playing?
Erm, for a total know-nothing like me, does this end a timer, or turn, or "movement" of any kind?
Growing old is inevitable.......Growing up is optional