• Advertisement

Hypostomus

Member
  • Content count

    3
  • Joined

  • Last visited

Community Reputation

224 Neutral

About Hypostomus

  • Rank
    Newbie
  1. Parameter passed by value changing calling class.

    Very good to know. Guess I need to pay a little more attention in the future. Thanks for all the help and quick replies.
  2. 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:   [spoiler] /// <summary> /// Tries to rotate the tetrimino shape. /// </summary> public void RotateTetrimino(List<Square> aNonFallingSquares) { if (!IsRotateOnCooldown()) { List<Square> rotatedTetrimino = GetRotatedTetrimino(); bool rotateSuccessful = false; if (IsRotationValid(rotatedTetrimino, aNonFallingSquares)) { Console.WriteLine("Good Rotation"); rotateSuccessful = true; } else { Console.WriteLine("Bad Rotation Needs Kick"); rotatedTetrimino = TryKick(rotatedTetrimino, aNonFallingSquares, ref rotateSuccessful); } if(rotateSuccessful) { squares = rotatedTetrimino; currentShapeVariaton = IncrementShapeVariation(); timeSinceLastRotate = 0; } timeSinceLastRotate = 0; //Remove when rotatedTetrimino problem fixed. } } /// <summary> /// Tries to kick the tetrimino received in the parameter left, then right, and then down. /// If the kick is valid, sets the successful rotation flag to true and saves and returns /// the kicked shape, otherwise it returns the original tetrimino shape received in the /// parameter. /// </summary> /// <param name="aRotatedTetrimino">Tetrimino to kick.</param> /// <param name="aNonFallingTetrimino">Non falling tetriminos on the gameboard.</param> /// <returns>A tetrimino shape.</returns> public List<Square> TryKick(List<Square> aRotatedTetrimino, List<Square> aNonFallingSquares, ref bool aRotationSuccessful) { Console.WriteLine("Kicking"); if (CanKick(aRotatedTetrimino, aNonFallingSquares, -1.0f, //Left true)) { aRotationSuccessful = true; Console.WriteLine("Kick Left Good"); } else if (CanKick(aRotatedTetrimino, aNonFallingSquares, 1.0f, //Right true)) { aRotationSuccessful = true; Console.WriteLine("Kick Right Good"); } else { aRotationSuccessful = false; } if (aRotationSuccessful) { aRotatedTetrimino = tempSquares; } return aRotatedTetrimino; } /// <summary> /// Tries to kick a tetrimino in the direction specified in the parameter up to two spaces /// and returns the result. /// </summary> /// <param name="aRotatedTetrimino">Tetrimino to kick.</param> /// <param name="aNonFallingTetrimino">Non falling tetriminos on the gameboard.</param> /// <param name="aDirection">Direction to kick (-1=Left/Down, 1=Right/Up)</param> /// <param name="HorizontalKick">Whether to kick horizontally or vertically.</param> /// <returns></returns> public bool CanKick(List<Square> aRotatedTetrimino, List<Square> aNonFallingSquares, float aDirection, bool HorizontalKick) { List<Square> tempRotatedTetrimino = aRotatedTetrimino; bool canKick = false; //Kick up to two times. for (int a = 0; a < 2; a++) { Console.WriteLine("Num of Kicks {0}: {1}", aDirection == 1 ? "Right" : "Left", a); foreach (Square thisFallingSquare in tempRotatedTetrimino) { if (HorizontalKick) { Console.WriteLine("Original X coord: {0}", thisFallingSquare.X); thisFallingSquare.X += (Common.squareSize * aDirection); Console.WriteLine("Kicked X coord: {0}", thisFallingSquare.X); } else { thisFallingSquare.Y += (Common.squareSize * aDirection); } } if (IsRotationValid(tempRotatedTetrimino, aNonFallingSquares)) { tempSquares = tempRotatedTetrimino; canKick = true; break; } else { canKick = false; } } return canKick; } [/spoiler]   Here is the output from a shape trying to be kicked (Important lines in blue):   [spoiler] Bad Rotation Needs Kick Kicking Num of Kicks Left: 0 Original X coord: -40 Kicked X coord: -80 Original X coord: 0 Kicked X coord: -40 Original X coord: 40 Kicked X coord: 0 Original X coord: 40 Kicked X coord: 0 isValid: False Num of Kicks Left: 1 Original X coord: -80 Kicked X coord: -120 Original X coord: -40 Kicked X coord: -80 Original X coord: 0 Kicked X coord: -40 Original X coord: 0 Kicked X coord: -40 isValid: False Num of Kicks Right: 0 Original X coord: -120 Kicked X coord: -80 Original X coord: -80 Kicked X coord: -40 Original X coord: -40 Kicked X coord: 0 Original X coord: -40 Kicked X coord: 0 isValid: False Num of Kicks Right: 1 Original X coord: -80 Kicked X coord: -40 Original X coord: -40 Kicked X coord: 0 Original X coord: 0 Kicked X coord: 40 Original X coord: 0 Kicked X coord: 40 isValid: False [/spoiler]   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.  
  3. Help with Worm Clone

    Assign a new Vector2 to it with appropriate values like so:   wormBodyPosition[i] = new Vector2((wormBodyPosition[i].X + wormBodyWidth), wormBodyPosition[i].Y);
  • Advertisement