Click to See Complete Forum and Search --> : Compairing Points


pwnedjoo
04-26-2005, 01:54 PM
I have put the following if statement into the mouse released event handler in a program I am working on,

if (frogs[0].frogsLocator == r6Pos || frogs[0].frogsLocator == r8Pos &&
frogs[1].frogsLocator == r8Pos || frogs[1].frogsLocator == r6Pos &&
frogs[2].frogsLocator == r1Pos || frogs[2].frogsLocator == r1Pos &&
frogs[3].frogsLocator == r3Pos || frogs[3].frogsLocator == r3Pos )
{
System.out.println("Winnar \r\n");
}

it is supposed to check the positions of all the points at which my frogs are sitting and then carry out the print line statement, however it doesn't. The or staement is necessary as in the puzzle the frog can be in either one of two places.

I have tried diagnosing the problem with a series of system outs and the co-ordinates are being updated for each move and do match when the puzzle is completed however the if statement is either
a) boloney
or
b) not being reached

i have tried variations on this, as before the if statement there are a series of "if, else if's" but no matter where i put it it has no effect;

* A sample of 1 of 8 preceding if statements *
if (originalPos == r1Pos)
{
if (newPos.x >= r5.x && newPos.y >= r5.y && newPos.x <= r5.x + 100 && newPos.y <= r5.y + 100 && r5PosOccupied == false)
{
frogs[selectedindex].snapFrog(r5Pos);
r5PosOccupied = true;
r1PosOccupied = false;
frogs[selectedindex].getlocation(r5Pos);
repaint();
}

else if (newPos.x >= r7.x && newPos.y >= r7.y && newPos.x <= r7.x + 100 && newPos.y <= r7.y + 100 && r7PosOccupied == false)
{
frogs[selectedindex].snapFrog(r7Pos);
r7PosOccupied = true;
r1PosOccupied = false;
frogs[selectedindex].getlocation(r7Pos);
repaint();
}
else
{
frogs[selectedindex].snapFrog(r1Pos);
repaint();
}
}
any ideas anyone :0

fino534
04-26-2005, 03:15 PM
the things you compare are what Objects? if they are objects and not primative types try using .equals() instead of ==

pwnedjoo
04-26-2005, 03:55 PM
thanks that seems to have improved things slightly,

I have shortened the statement to this


if (frogs[0].frogsLocator.equals(r6Pos) || frogs[0].frogsLocator.equals(r8Pos) &&
frogs[1].frogsLocator.equals(r8Pos) || frogs[1].frogsLocator.equals(r6Pos))
{
System.out.println("Winnar \r\n");
}



now the system out is produced even if only one part of the statement is true
i.e if frogs[0].frogsLocator is in the right place and frogs[1] is not

it is almost as if the && is being picked up as ||

any ideas why this may happen,

fino534
04-26-2005, 05:08 PM
try placing the sets of or statements into parens

if( (x||y) && (a||b) )

is that what you intend it to be?

pwnedjoo
04-26-2005, 05:39 PM
just came back to tell you i'd figured it out, and you've written the same thing,

your spot on it was the parenthesis

thanks very much for your help

cheers :)