//Bishop can only move only diagonally public override bool IsValidMove(CoOrdinate to, History history) { if (to.XCoOrdinate == CurrentCoOrdinate.XCoOrdinate || to.YCoOrdinate == CurrentCoOrdinate.YCoOrdinate) { return(false); } CoOrdinate finalCheckCoOrdinate; //For Bishop to move all cells should be empty in diagonal direction if (to.XCoOrdinate > CurrentCoOrdinate.XCoOrdinate) { //Try to move diagonally to the right of current position finalCheckCoOrdinate = AreCellsEmpty(CurrentCoOrdinate.XCoOrdinate, CurrentCoOrdinate.YCoOrdinate, to.YCoOrdinate, history, true); } else { //Try to move diagonally to the left of current position finalCheckCoOrdinate = AreCellsEmpty(CurrentCoOrdinate.XCoOrdinate, CurrentCoOrdinate.YCoOrdinate, to.YCoOrdinate, history, false); } return(to.Equals(finalCheckCoOrdinate)); }
//King can move in any direction, only if there is piece of another color in that position public override bool IsValidMove(CoOrdinate to, History history) { Piece foundPiece; if (history.LayOut.TryGetValue(to, out foundPiece) && Color == foundPiece.Color) { return(false); } if (to.XCoOrdinate == CurrentCoOrdinate.XCoOrdinate) { return(Math.Abs(to.YCoOrdinate - CurrentCoOrdinate.YCoOrdinate) == 1); } if (to.YCoOrdinate == CurrentCoOrdinate.YCoOrdinate) { return(Math.Abs(to.XCoOrdinate - CurrentCoOrdinate.XCoOrdinate) == 1); } var diagonalYCoOrdinate = Color == Color.White ? CurrentCoOrdinate.YCoOrdinate + 1 : CurrentCoOrdinate.YCoOrdinate - 1; var diagonalBackwardYCoOrdinate = Color == Color.White ? CurrentCoOrdinate.YCoOrdinate - 1 : CurrentCoOrdinate.YCoOrdinate + 1; var leftDiagonalCoOrdinate = new CoOrdinate(CurrentCoOrdinate.XCoOrdinate - 1, diagonalYCoOrdinate); var leftBackwardDiagonalCoOrdinate = new CoOrdinate(CurrentCoOrdinate.XCoOrdinate - 1, diagonalBackwardYCoOrdinate); var rightDiagonalCoOrdinate = new CoOrdinate(CurrentCoOrdinate.XCoOrdinate + 1, diagonalYCoOrdinate); var rightBackwardDiagonalCoOrdinate = new CoOrdinate(CurrentCoOrdinate.XCoOrdinate + 1, diagonalBackwardYCoOrdinate); if (history.LayOut.TryGetValue(leftDiagonalCoOrdinate, out foundPiece)) { if (foundPiece.Color == Color) { foundPiece = null; } else { return(to.Equals(leftDiagonalCoOrdinate)); } } if (history.LayOut.TryGetValue(leftBackwardDiagonalCoOrdinate, out foundPiece)) { if (foundPiece.Color == Color) { foundPiece = null; } else { return(to.Equals(leftBackwardDiagonalCoOrdinate)); } } if (history.LayOut.TryGetValue(rightDiagonalCoOrdinate, out foundPiece)) { if (foundPiece.Color == Color) { foundPiece = null; } else { return(to.Equals(rightDiagonalCoOrdinate)); } } if (history.LayOut.TryGetValue(rightBackwardDiagonalCoOrdinate, out foundPiece)) { if (foundPiece.Color == Color) { foundPiece = null; } else { return(to.Equals(rightBackwardDiagonalCoOrdinate)); } } if (foundPiece == null) { return(to.Equals(leftDiagonalCoOrdinate) || to.Equals(leftBackwardDiagonalCoOrdinate) || to.Equals(rightDiagonalCoOrdinate) || to.Equals(rightBackwardDiagonalCoOrdinate)); } return(false); }
/* * A pawn moves straight forward one square, if that square is vacant. If it has not yet moved, * a pawn also has the option of moving two squares straight forward, provided both squares are vacant. * Pawns cannot move backwards. * Pawns are the only pieces that capture differently from how they move. A pawn can capture an enemy piece on either of the two squares diagonally in front of the pawn (but cannot move to those squares if they are vacant). */ public override bool IsValidMove(CoOrdinate to, History history) { Piece foundPiece; if (to.XCoOrdinate == CurrentCoOrdinate.XCoOrdinate) { //The next cell should be empty if moving forward if (!history.LayOut.TryGetValue(to, out foundPiece)) { int maxMoveMagnitude = CurrentCoOrdinate == FromCoOrdinate ? 2 : 1; if (maxMoveMagnitude == 2 && Math.Abs(to.YCoOrdinate - CurrentCoOrdinate.YCoOrdinate) == 2) { if (Color == Color.White) { var diffInCoOrdinate = to.YCoOrdinate - CurrentCoOrdinate.YCoOrdinate; if (diffInCoOrdinate > 0 && diffInCoOrdinate <= maxMoveMagnitude) { return(AreCellsEmpty(CurrentCoOrdinate.XCoOrdinate, CurrentCoOrdinate.YCoOrdinate, to.YCoOrdinate - 1, history, CoOrdinateType.X)); } } else { var diffInCoOrdinate = CurrentCoOrdinate.YCoOrdinate - to.YCoOrdinate; if (diffInCoOrdinate > 0 && diffInCoOrdinate <= maxMoveMagnitude) { return(AreCellsEmpty(CurrentCoOrdinate.XCoOrdinate, to.YCoOrdinate, CurrentCoOrdinate.YCoOrdinate - 1, history, CoOrdinateType.X)); } } } else { //Pwan cannot move backwards if (Color == Color.White) { return((to.YCoOrdinate - CurrentCoOrdinate.YCoOrdinate) == 1); } else { return((CurrentCoOrdinate.YCoOrdinate - to.YCoOrdinate) == 1); } } } } //Pawn can move diagonally only of there is piece of another color in that position var diagonalYCoOrdinate = Color == Color.White ? CurrentCoOrdinate.YCoOrdinate + 1 : CurrentCoOrdinate.YCoOrdinate - 1; var leftDiagonalCoOrdinate = new CoOrdinate(CurrentCoOrdinate.XCoOrdinate - 1, diagonalYCoOrdinate); var rightDiagonalCoOrdinate = new CoOrdinate(CurrentCoOrdinate.XCoOrdinate + 1, diagonalYCoOrdinate); if (history.LayOut.TryGetValue(leftDiagonalCoOrdinate, out foundPiece) && foundPiece.Color != Color) { return(to.Equals(leftDiagonalCoOrdinate)); } if (history.LayOut.TryGetValue(rightDiagonalCoOrdinate, out foundPiece) && foundPiece.Color != Color) { return(to.Equals(rightDiagonalCoOrdinate)); } return(false); }