Question about Piece and equals

Hey,

I have a questions about comparing Piece Objects.

I’m not sure if I misunderstand the project presentation or whatever, but I’m not sure how equality works in this case.

In the public tests, everything is fine. The Method “checkPieceEqual” returns true in all cases where it should (normal public tests and my own).

The problem comes up when calling “old”.equals(“new”) (like in pieceComponent and probably the daily tests). This returns false (which shouldn’t be a surprise, since they are different objects). The “new” Array is the clone with the parameters and a clone of point and a deepcopy of the body.
In the project presentation it sounded like we should overwrite the equals() functions, but obviously writing it in my implementation shouldn’t do anything (and isn’t atm) because the objects are instances of the Piece interface (please correct me if I’m wrong).

Can somebody give me an intuition for this?

Thank you in advance!

PS: here is a picture of the pieceComponent. you can see that it doesn’t recognize the final rotation and does one extra before it gets stopped by the limit.

image

Hi there!

The equals method from Object implements the most discriminating possible equivalence relation on objects, which means that x.equals(y) only returns true if x and y are the exact same object. However, this doesn’t exactly reflect our understanding of equivalence for pieces. As stated in the project description, we want two pieces to be considered equal if they are of the same type and have the same orientation and point of rotation. So yes, you do need to overwrite the equals method to reflect this understanding of equality.

In the project presentation it sounded like we should overwrite the equals() functions, but obviously writing it in my implementation shouldn’t do anything (and isn’t atm) because the objects are instances of the Piece interface (please correct me if I’m wrong).

You are correct in saying that your pieces should have the static type Piece. That means that if you define additional methods in your Piece implementation you can’t call them on the pieces you instantiate using Piece piece = PieceImplementation(). The methods from the Object class are an exception to this rule however. So if you overwrite any of these methods, equals included, any call to said methods will use your version of them.

1 Like

Thank you!

I found the bug. The Method I wrote in my PieceImplementation took an Object of type “Piece”, so I didnt overwrite the method in Object but only overloaded it (I think).

Note that there is no “magic sauce” around the methods from Object. Since every class extends Object (except Object itself), these methods are always there. So you can invoke them on every instance of any class, and overwrite them in any class.

The interface Piece also already admits you calling these function, since Java knows that every object that implements this interface must extend Object, so you can already use them.

Indeed. That’s why you should always put @Override on methods where you intend to override the partent class’s method, so that the compliler warns you if your method does not actually override anything.