/* * Check all squares in the rooks row, then all squares in it's column */ private static bool ValidRook(MoveLogic ml, Square fromSquare, ChessColor color, bool FirstValid, int row, int col) { // check horizontally for (int i = 0; i < 8; i++) { if (ml.MovePiece(fromSquare, ml.gb.squares[i, col], true)) { if (IsValidMove(ml, color, FirstValid, i, col)) { if (FirstValid) { return(true); } } } } // check vertically for (int i = 0; i < 8; i++) { if (ml.MovePiece(fromSquare, ml.gb.squares[row, i], true)) { if (IsValidMove(ml, color, FirstValid, row, i)) { if (FirstValid) { return(true); } } } } return(false); }
/* * Check two spaces in front and one space to the left and right of the pawn. Since a pawn can only move forward, * the sign variable captures that. */ private static bool ValidPawn(MoveLogic ml, Square fromSquare, ChessColor color, bool FirstValid, int row, int col) { int sign = fromSquare.Piece.Color == ChessColor.White ? 1 : -1; for (int i = 1; i <= 2; i++) { for (int j = -1; j <= 1; j++) { if (row + (i * sign) < GameBoard.XDim && row + (i * sign) >= 0 && col + j < GameBoard.YDim && col + j >= 0) { if (ml.MovePiece(fromSquare, ml.gb.squares[row + (i * sign), col + j], true)) { if (IsValidMove(ml, color, FirstValid, row + (i * sign), col + j)) { if (FirstValid) { return(true); } } } } } } return(false); }
/* * Checks all squares one space around the kind, and 2 spaces to it;s left and right (for castling) */ private static bool ValidKing(MoveLogic ml, Square fromSquare, ChessColor color, bool FirstValid, int row, int col) { for (int i = -1; i <= 1; i++) { for (int j = -2; j <= 2; j++) { if (row + i < GameBoard.XDim && row + i >= 0 && col + j < GameBoard.YDim && col + j >= 0) { if (ml.MovePiece(fromSquare, ml.gb.squares[row + i, col + j], true)) { if (IsValidMove(ml, color, FirstValid, row + i, col + j)) { if (FirstValid) { return(true); } } } } } } return(false); }
/* * Calculates the given equation |x2 - x1| = |y2 - y1| where x2 is the piece row, x1 is a row 0-8 * y2 is the piece column, y1 a column 0-8. This gives the diagonals of the bishop. If it is true, * then start checking for valid moves */ private static bool ValidBishop(MoveLogic ml, Square fromSquare, ChessColor color, bool FirstValid, int row, int col) { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (Math.Abs(row - i) == Math.Abs(col - j)) { if (ml.MovePiece(fromSquare, ml.gb.squares[i, j], true)) { if (IsValidMove(ml, color, FirstValid, i, j)) { if (FirstValid) { return(true); } } } } } } return(false); }