I’m sure the answer to this is quite simple; however, it has got me completely baffled. I’m currently writing a Tetris clone in C# using Monogame. I have run into a problem while trying to implement a wall kick for shape rotations. When a shape needs to be rotated, a new temporary shape is created from the original shape and then rotated. This rotated temporary shape is then used to check whether or not the rotation is valid by checking to see if any part of the shape covers any of the non-falling squares on the board or is out of bounds. If either is true, it tries to kick the shape left, if it’s still invalid it kicks it to the right and so on.
The kick operation is performed using a method named TryKick which accepts the temporary rotated shape as a parameter named aRotatedTetrimino. When trying to kick the shape, aRotatedTetrimino is passed by value to a method named CanKick which determines whether or not the shape can be kicked in the direction specified. This is where my problems lie. When aRotatedTetrimino is passed to CanKick, changes that are made to it are being applied in TryKick. I have no idea how this is happening.
Here is the code:
Here is the output from a shape trying to be kicked (Important lines in blue):
The way I expect it to work is the first “Original X coord” value after “Num of Kicks Left: 0” and “Num of Kicks Right: 0” should be the same because they are each from their own method calls which should be using the same value for their parameter. However, as the output shows the first “Original X coord” value after “Num of Kicks Right: 0” is the value that was set on the last kick left ("Num of Kicks Left: 1").
What I need to know is why are the changes being applied to the calling method when it is passed by value? I would be very grateful if somebody could explain this to me.