/// <summary> /// Moves the piece from startPosition to endPosition. Kills the piece at endPosition if it exists. /// </summary> /// <param name="startPosition"></param> /// <param name="endPosition"></param> public void MovePiece(Position startPosition, Position endPosition) { MoveSpecification move = new MoveSpecification(startPosition, endPosition); // Kill the piece at the destination, if there is one var endPiece = internalPieceAt(endPosition); if (endPiece != null) { #if DEBUG changeTurn(); System.Diagnostics.Debug.WriteLine("num in captured before capture on captured team " + WhoseTurn + " is " + capturedPiecesByTeam[WhoseTurn].Count); if (capturedPiecesByTeam[WhoseTurn].Count > 0) { System.Diagnostics.Debug.WriteLine("piece at count - 1 before this capture: " + capturedPiecesByTeam[WhoseTurn][capturedPiecesByTeam[WhoseTurn].Count - 1].ToShortString()); } foreach (var piece in capturedPiecesByTeam[WhoseTurn]) { System.Diagnostics.Debug.Write(piece.ToShortString() + ", "); } System.Diagnostics.Debug.WriteLine("are the pieces captured for " + WhoseTurn); changeTurn(); #endif move.Capture = endPosition; capturedPiecesByTeam[endPiece.Team].Add(endPiece); // Remove a killed piece from our valid pieceLocationsByType list var listOfEndPieceType = pieceLocationsByType[endPiece.Type]; listOfEndPieceType.Remove(new Point2D(endPosition)); #if DEBUG changeTurn(); System.Diagnostics.Debug.WriteLine("num in captured after capture on captured team " + WhoseTurn + " is " + capturedPiecesByTeam[WhoseTurn].Count); System.Diagnostics.Debug.WriteLine("piece at count - 1 after this capture: " + capturedPiecesByTeam[WhoseTurn][capturedPiecesByTeam[WhoseTurn].Count - 1].ToShortString()); foreach (var piece in capturedPiecesByTeam[WhoseTurn]) { System.Diagnostics.Debug.Write(piece.ToShortString() + ", "); } System.Diagnostics.Debug.WriteLine("are the pieces captured for " + WhoseTurn); changeTurn(); #endif } //TODO: else if en passant var movingPiece = internalPieceAt(startPosition); //Move the moving piece in boardMatrix SetPieceAt(endPosition, movingPiece); SetPieceAtToNull(startPosition); if (!movingPiece.HasMoved) //note changes if this is a piece's first move in it and in pastMoves { movingPiece.HasMoved = true; move.HasMovedChange = true; } // Replace the old position for this piece with the new position in the pieceLocationsByType list var listOfMovingPieceType = pieceLocationsByType[movingPiece.Type]; listOfMovingPieceType.Remove(new Point2D(startPosition)); listOfMovingPieceType.Add(new Point2D(endPosition)); pastMoves.Add(move); changeTurn(); }
//undoes castle moves private void undoCastle(MoveSpecification lastMove) { int rookSideDir = Math.Sign(lastMove.Start.Column - KingCol); //calculates the direction of the rook from the king initially //Move King back in BoardMatrix Position movedKingPos = new Position(KingCol + 2 * rookSideDir, lastMove.Start.Row); var king = internalPieceAt(movedKingPos); Position unMovedKingPos = new Position(KingCol, lastMove.Start.Row); SetPieceAt(unMovedKingPos, king); SetPieceAtToNull(movedKingPos); king.HasMoved = false; //set King HasMoved to false //Update King Location in pieceLocationsByType var listOfPieceType = pieceLocationsByType[PieceType.King]; listOfPieceType.Remove(movedKingPos); listOfPieceType.Add(unMovedKingPos); //Move Rook back var rook = internalPieceAt(lastMove.End); SetPieceAt(new Point2D(lastMove.Start), rook); SetPieceAtToNull(new Point2D(lastMove.End)); rook.HasMoved = false; //set Rook HasMoved to false //Update Rook Location in pieceLocationsByType listOfPieceType = pieceLocationsByType[PieceType.Rook]; listOfPieceType.Remove(new Point2D(lastMove.End)); listOfPieceType.Add(new Point2D(lastMove.Start)); }
//undoes non-castle moves private void undoRegularMove(MoveSpecification lastMove) { var movingPiece = internalPieceAt(lastMove.End); //get the move that is being undone if (lastMove.HasMovedChange) //set a piece to unmoved if this was a move that changed that status { movingPiece.HasMoved = false; } //Perform reverse move upon boardMatrix SetPieceAt(lastMove.Start, internalPieceAt(lastMove.End)); SetPieceAtToNull(lastMove.End); //Replace the old position for this piece with the new position in the pieceLocationsByType list var listOfMovingPieceType = pieceLocationsByType[internalPieceAt(lastMove.Start).Type]; listOfMovingPieceType.Remove(lastMove.End); listOfMovingPieceType.Add(lastMove.Start); if (lastMove.Capture != null) //if move being undone has a capture location, restore the most recently taken other team's piece to that location { #if DEBUG System.Diagnostics.Debug.WriteLine("num in captured before restore for " + WhoseTurn + " is " + capturedPiecesByTeam[WhoseTurn].Count); System.Diagnostics.Debug.WriteLine("restored captured piece: " + capturedPiecesByTeam[WhoseTurn][capturedPiecesByTeam[WhoseTurn].Count - 1].ToShortString()); foreach (var piece in capturedPiecesByTeam[WhoseTurn]) { System.Diagnostics.Debug.Write(piece.ToShortString() + ", "); } System.Diagnostics.Debug.WriteLine("are the pieces captured for " + WhoseTurn); #endif SetPieceAt((Position)lastMove.Capture, capturedPiecesByTeam[WhoseTurn][capturedPiecesByTeam[WhoseTurn].Count - 1]); //restores captured piece to boardMatrix #if DEBUG System.Diagnostics.Debug.WriteLine("num in captured mid restore for " + WhoseTurn + " is " + capturedPiecesByTeam[WhoseTurn].Count); System.Diagnostics.Debug.WriteLine("piece being restored is " + capturedPiecesByTeam[WhoseTurn][capturedPiecesByTeam[WhoseTurn].Count - 1]); System.Diagnostics.Debug.Write(ToString()); #endif capturedPiecesByTeam[WhoseTurn].RemoveAt(capturedPiecesByTeam[WhoseTurn].Count - 1); //removes restored captured piece from the list of capturedPiecesByTeam for WhoseTurn, which hasn't been changed yet // Add a captured piece to our valid pieceLocationsByType list var listOfCapturedPieceType = pieceLocationsByType[internalPieceAt((Position)lastMove.Capture).Type]; listOfCapturedPieceType.Add((Position)lastMove.Capture); #if DEBUG System.Diagnostics.Debug.WriteLine("num in captured after restore for " + WhoseTurn + " is " + capturedPiecesByTeam[WhoseTurn].Count); if (capturedPiecesByTeam[WhoseTurn].Count > 0) { System.Diagnostics.Debug.WriteLine("piece at count - 1: " + capturedPiecesByTeam[WhoseTurn][capturedPiecesByTeam[WhoseTurn].Count - 1].ToShortString()); } foreach (var piece in capturedPiecesByTeam[WhoseTurn]) { System.Diagnostics.Debug.Write(piece.ToShortString() + ", "); } System.Diagnostics.Debug.WriteLine("are the pieces captured for " + WhoseTurn); #endif } }
//undoes the last move (can be called repeatedly) public void UndoMove() { MoveSpecification lastMove = pastMoves[pastMoves.Count - 1]; if (lastMove.Castle) { undoCastle(lastMove); } else //not a castle { undoRegularMove(lastMove); } pastMoves.RemoveAt(pastMoves.Count - 1); changeTurn(); }
public void Castle(Point2D initialRookPoint) { int rookDir = Math.Sign(initialRookPoint.X - KingCol); //calculates the direction of the rook from the king initially //Move the King in boardMatrix Position initialKingPos = new Position(KingCol, initialRookPoint.Y); var king = internalPieceAt(initialKingPos); Position finalKingPos = new Position(KingCol + 2 * rookDir, initialRookPoint.Y); SetPieceAt(finalKingPos, king); SetPieceAtToNull(initialKingPos); king.HasMoved = true; //update King's HasMoved //Replace the old position for the King with the new position in the pieceLocationsByType list var listOfPieceType = pieceLocationsByType[PieceType.King]; listOfPieceType.Remove(new Point2D(initialKingPos)); listOfPieceType.Add(new Point2D(finalKingPos)); //Move this Rook in boardMatrix var rook = internalPieceAt(initialRookPoint); Position finalRookPos = new Position(KingCol + rookDir, initialRookPoint.Y); SetPieceAt(finalRookPos, rook); SetPieceAtToNull(initialRookPoint); rook.HasMoved = true; //update Rook's HasMoved //Replace the old position for this Rook with the new position in the pieceLocationsByType list listOfPieceType = pieceLocationsByType[PieceType.Rook]; listOfPieceType.Remove(initialRookPoint); listOfPieceType.Add(new Point2D(finalRookPos)); //TODO: figure out if this creation is unnecessary and casting is done automatically. MoveSpecification move = new MoveSpecification(new Position(initialRookPoint), finalRookPos, null, true, true); pastMoves.Add(move); changeTurn(); }