syszee
|
 |
«
Posted
2014-06-20 21:35:00 » |
|
Hi there,
I'm having the worst luck coming up with a system for this project. Basically, I need a simply algorithm that detects if player1 is in a door, and player2 is in a door.. then proceed to next level. Else, if player1 is in the door but player 2 isn't, then don't go. (& vise versa).
I've tried many things. Something simple please!
Thanks! - A
(Yes, this is like beginner, I don't know why I can't get it)
|
|
|
|
Longarmx
|
 |
«
Reply #1 - Posted
2014-06-20 21:58:25 » |
|
? I don't understand what you're having trouble with. You just have to make exactly what you described: 1 2 3 4
| if(player1.inDoor() && player2.inDoor()) { nextLevel(); } |
|
|
|
|
syszee
|
 |
«
Reply #2 - Posted
2014-06-20 22:00:39 » |
|
I know, I don't understand it either! It is really weird because I've tried many things and it just keeps failing. Let me give this a try though. Thank you! Edit: It worked! I don't understand why this worked and nothing else I tried did! Thank you though! 
|
|
|
|
Games published by our own members! Check 'em out!
|
|
PandaMoniumHUN
|
 |
«
Reply #3 - Posted
2014-06-20 22:20:06 » |
|
Edit: It worked! I don't understand why this worked and nothing else I tried did! Thank you though!  is a logical AND operator, it only proceeds to the if block if both conditions around it are true. No offense, but you should learn the basics of programming before you dive into such a deep topic as game development.
|
My Blog | Jumpbutton Studio - INOP Programmer Can't stress enough: Don't start game development until you haven't got the basics of programming down! 
|
|
|
BurntPizza
|
 |
«
Reply #4 - Posted
2014-06-20 23:16:59 » |
|
For the record, is not logical AND. It is the short-circuiting logical AND. Only evaluates 2nd operand if the first is true. is the logical AND as well as the bitwise AND.
|
|
|
|
PandaMoniumHUN
|
 |
«
Reply #5 - Posted
2014-06-21 08:03:07 » |
|
For the record, is not logical AND. It is the short-circuiting logical AND. Only evaluates 2nd operand if the first is true. is the logical AND as well as the bitwise AND. That's true, but I didn't want him to be confused by short-circuiting/not short-circuiting thing. If he knows that he can use for logical AND it's all good, he should look up a doc or a book anyways to fully learn how to utilize operators. 
|
My Blog | Jumpbutton Studio - INOP Programmer Can't stress enough: Don't start game development until you haven't got the basics of programming down! 
|
|
|
Rayvolution
|
 |
«
Reply #6 - Posted
2014-06-21 08:49:12 » |
|
For the record, is not logical AND. It is the short-circuiting logical AND. Only evaluates 2nd operand if the first is true. is the logical AND as well as the bitwise AND. [slightDerail] What purpose would you have to use a logical/bitwise &? I mean, if & checks both(or all) conditions, but && checks them 1 at a time, and gives up when it fails, how is that functionally different to your program? I can't think of a reason why you would want to continue checking the rest of the conditions if even one failed, because if one failed the if statement will fail anyway? Kinda seems like a waste of processing. Having said that, that means I guess the most logical way to order your IF (condition && condition) statements is to have your least CPU intensive check first, and most last. [/slightDerail]
|
|
|
|
Orangy Tang
|
 |
«
Reply #7 - Posted
2014-06-21 10:13:02 » |
|
[slightDerail] What purpose would you have to use a logical/bitwise &? I mean, if & checks both(or all) conditions, but && checks them 1 at a time, and gives up when it fails, how is that functionally different to your program? I can't think of a reason why you would want to continue checking the rest of the conditions if even one failed, because if one failed the if statement will fail anyway? Kinda seems like a waste of processing.
Having said that, that means I guess the most logical way to order your IF (condition && condition) statements is to have your least CPU intensive check first, and most last. [/slightDerail]
If all of the things being checked are boolean values then they're functionally identical. You'll get differences if the blocks are function calls instead. Consider: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| class Player { int x, y; int vx, vy; boolean moveAndCheckIfDoor() { x += vx; y += vy; return x == 0; } }
public void update() { if (player1.moveAndCheckIfDoor() && player2.moveAndCheckIfDoor()) { } } |
Here because moveAndCheckDoor() has side effects, you'll get different behaviour depending on whether short circuiting takes place or not. Although personally I'd strongly suggest *not* writing code like this, sometimes this kind of behaviour can creep in more subtly without you noticing.
|
|
|
|
BurntPizza
|
 |
«
Reply #8 - Posted
2014-06-21 11:16:37 » |
|
Yeah precisely, side effects. Even simple stuff like this: 1 2 3 4 5 6 7 8 9 10 11 12
| if (a > 0 && ++b > 0) { ... }
if (a > 0) { b++; if (b > 0) { ... } } |
Of course you shouldn't do that, but it's there.
|
|
|
|
Riven
|
 |
«
Reply #9 - Posted
2014-06-21 11:28:40 » |
|
Those two pieces of code are not identical.
Ah, you corrected it.
|
Hi, appreciate more people! Σ ♥ = ¾ Learn how to award medals... and work your way up the social rankings!
|
|
|
Games published by our own members! Check 'em out!
|
|
BurntPizza
|
 |
«
Reply #10 - Posted
2014-06-21 11:30:43 » |
|
Those two pieces of code are not identical.
Ah, you corrected it.
Yeah, actually got it wrong twice...  See kids, don't do that!
|
|
|
|
Rayvolution
|
 |
«
Reply #11 - Posted
2014-06-21 17:49:07 » |
|
Well, I was thinking more along the lines of actualling using the operator: 1 2 3
| if (isTrue) & (isAlsoTrue){ System.out.println("This is totally true!"); } |
compared to.. 1 2 3
| if (isTrue) && (isAlsoTrue){ System.out.println("This is totally true!"); } |
In pretty much all situations using & and && would be identical behavior in the end, wouldn't it? The only difference is & would check if isAlsoTrue == true, even though isTrue came back false.
|
|
|
|
BurntPizza
|
 |
«
Reply #12 - Posted
2014-06-21 20:33:17 » |
|
Yeah, they are semantically identical when the conditions are pure (have no side effects). However they are not with impure conditions, which can certainly happen. An important distinction to be aware of IMO, could leave someone unaware completely stumped on a bug otherwise.
That said I rarely ever use the non-short circuiting operator (OR or AND).
|
|
|
|
Riven
|
 |
«
Reply #13 - Posted
2014-06-21 20:40:01 » |
|
I've had collegues that wrote code like this: 1 2 3
| public static boolean update() { return (selectX() || selectY()) && updateZ(); } |
where updateZ was actually meant to only run if selectX and/or selectY returned true. Sadly, the code was way more complex: a one-liner of a few hundred characters with a dozen method calls all returning booleans and almost all having side effects, were each was supposed to be executed in very specific conditions. Obviously it was broken.
|
Hi, appreciate more people! Σ ♥ = ¾ Learn how to award medals... and work your way up the social rankings!
|
|
|
BurntPizza
|
 |
«
Reply #14 - Posted
2014-06-21 20:49:21 » |
|
That sucks.
I say if you want to have a function like that be called only after a condition, it should be done explicitly, i.e. in an if block. Makes it clear what the code does with only a cursory glance. Sure it's more verbose, but that's a feature in this scenario.
|
|
|
|
Rayvolution
|
 |
«
Reply #15 - Posted
2014-06-21 21:02:01 » |
|
Yeah, they are semantically identical when the conditions are pure (have no side effects). However they are not with impure conditions, which can certainly happen. An important distinction to be aware of IMO, could leave someone unaware completely stumped on a bug otherwise.
That said I rarely ever use the non-short circuiting operator (OR or AND).
Yeah, I never use them either. I never ran into a situation where I wanted/needed to. That's the main reason why I was asking, maybe it was more useful than I realized. 
|
|
|
|
BurntPizza
|
 |
«
Reply #16 - Posted
2014-06-21 21:03:58 » |
|
Yeah it's really only useful to guarantee side effects, and if you find yourself in that scenario, you should just invoke them explicitly. Much cleaner IMO. Code clean, not clever.
|
|
|
|
Herjan
|
 |
«
Reply #17 - Posted
2014-06-21 21:50:52 » |
|
Code clean, not clever.
Its amazing what people do when they are bored (fan art  ):  
|
|
|
|
syszee
|
 |
«
Reply #18 - Posted
2014-06-22 00:04:28 » |
|
Edit: It worked! I don't understand why this worked and nothing else I tried did! Thank you though!  is a logical AND operator, it only proceeds to the if block if both conditions around it are true. No offense, but you should learn the basics of programming before you dive into such a deep topic as game development. No offense, but I know what && is. Lol, I was using it before I created the topic. I don't understand why this worked actually, I believe it may have just been a bug. The && wasn't at all the fix. Creating the nextLevel(); method was generally how I got it working.  If I didn't understand java, I certainly wouldn't be bugging people about it on the forum.
|
|
|
|
ctomni231
|
 |
«
Reply #19 - Posted
2014-06-22 02:00:08 » |
|
Creating the nextLevel(); method was generally how I got it working.  If I didn't understand java, I certainly wouldn't be bugging people about it on the forum. Yeah... maybe it isn't understanding Java that is the issue here, maybe it is learning how it actually applies to 2D game programming. Try reading this...Regardless of what you know, there is a disconnect somewhere. But, just as well, gaming is one of the hardest parts of developing. Researching deeply into the topics, and looking at how other games do things, is the answer to solving most problems you have. Honestly, it isn't that you don't know Java, I think that the issue is that enough time in researching the topics isn't spent.
|
|
|
|
|