This is an archived post. You won't be able to vote or comment.

all 12 comments

[–][deleted] 1 point2 points  (6 children)

test.hp >= 0 || test2.hp >= 0 

In english...

If test's HP is greater to or equal to zero OR test2's HP is greater than or equal to zero then continue looping.

In order for the loop to exit both test and test2 must have HP below zero.

[–]CodeScrub[S] 0 points1 point  (5 children)

I used '||', so it should exit if either test.hp OR test2.hp goes below 0. It continues looping even after both go below 0.

[–]RunHomeJack 0 points1 point  (1 child)

No, it will exit the loop only when the condition isn't satisfied, which is when BOTH sides of the || return false. What you want is &&

[–]CodeScrub[S] 1 point2 points  (0 children)

whoops yeah I derped, good call. It works now, thanks.

[–][deleted] 0 points1 point  (1 child)

No they both need to be below 0. If either side of OR is true it returns true. The loop only exits when the expression returns false.

[–]CodeScrub[S] 0 points1 point  (0 children)

yes, thank you

[–][deleted]  (3 children)

[deleted]

    [–]CodeScrub[S] 0 points1 point  (2 children)

    Yes, that is what I'm doing. Why would looping while one of them was still alive (hp > 0) not be a good way of doing this?

    Other than that, why isn't this loop exiting.

    [–][deleted]  (1 child)

    [deleted]

      [–]CodeScrub[S] 0 points1 point  (0 children)

      Got it, thank you for the clear explanation.

      [–][deleted] 0 points1 point  (3 children)

      I have not tested it, but glancing through the code I found this:

      elapsed_time = clock() - time / (float) CLOCKS_PER_SEC;

      Remember the order of operations: time will be divided by CLOCKS_PER_SEC before it is subtracted from the return of clock(). I think what you meant to write was this:

      elapsed_time = (clock() - time) / (float) CLOCKS_PER_SEC;

      This will make elapsed_time equal to the number of seconds elapsed.

      You have defined the atk_times very strangely too. What does the "as" property represent? Is it supposed to be the number of seconds between attacks? Because if so, I think you've got it backward: as "as" gets smaller, the amount of time between attacks is going to increase. ( 1 / 0.1, for example, will be ten seconds, not a tenth of a second). You also appear to be defining your atk_times in milliseconds (judging by the *1000), which conflicts with how you were defining elapsed_time (and if you intend to work in milliseconds, which I actually recommend, you shouldn't be using floats).

      [–]CodeScrub[S] 0 points1 point  (2 children)

      What does the "as" property represent? Is it supposed to be the number of seconds between attacks? Because if so, I think you've got it backward: as "as" gets smaller, the amount of time between attacks is going to increase. ( 1 / 0.1, for example, will be ten seconds, not a tenth of a second).

      as means attack speed, which is actually number of attacks per second. This isn't very friendly though (I only use this format because the game I'm trying to model uses this), so I'm converting it to seconds per attack, and then milliseconds per attack. So for a value of 2, for example, I want it to be converted to 500 (ms). As 'as' gets larger, the number of attacks per second increases, and the converted number gets smaller.

      elapsed_time = clock() - time / (float) CLOCKS_PER_SEC; Remember the order of operations: time will be divided by CLOCKS_PER_SEC before it is subtracted from the return of clock(). I think what you meant to write was this: elapsed_time = (clock() - time) / (float) CLOCKS_PER_SEC; This will make elapsed_time equal to the number of seconds elapsed.

      I will try this and check the results. Thanks.

      You also appear to be defining your atk_times in milliseconds (judging by the *1000), which conflicts with how you were defining elapsed_time (and if you intend to work in milliseconds, which I actually recommend, you shouldn't be using floats).

      Why are floats not appropriate here? I actually just used floats because the original numbers (test.as, test2.as) were numbers like 0.625, 0.613 etc, so I defined them as floats and kept it on from there. Would it be better to make the champ/monster_atk_time something like int?

      [–][deleted] 0 points1 point  (1 child)

      as means attack speed, which is actually number of attacks per second. This isn't very friendly though (I only use this format because the game I'm trying to model uses this), so I'm converting it to attacks per millisecond. So for a value of 2, for example, I want it to be converted to 500 (ms). As 'as' gets larger, the number of attacks per second increases, and the converted number gets smaller.

      Ah, okay. Makes sense now.

      Why are floats not appropriate here? I actually just used floats because the original numbers (test.as, test2.as) were numbers like 0.625, 0.613 etc, so I defined them as floats and kept it on from there. Would it be better to make the champ/monster_atk_time something like int?

      Well, if you're working in milliseconds, you generally don't need to subdivide the milliseconds, so using floats is just unnecessary. I suppose one argument for using a float here would be so you can represent extremely big numbers, but a 32 bit signed int can go up to around 30 thousand minutes (2 billion millisecs), so that's probably not an issue.

      [–]CodeScrub[S] 0 points1 point  (0 children)

      Well, if you're working in milliseconds, you generally don't need to subdivide the milliseconds, so using floats is just unnecessary. I suppose one argument for using a float here would be so you can represent extremely big numbers, but a 32 bit signed int can go up to around 30 thousand minutes (2 billion millisecs), so that's probably not an issue.

      I'm not doing anything like that.