/* * The King can move in any direction by 1 space, unless it is Castling. * The King asks the Move Validator if it is allowed to Castle. * It is only a valid move if it can Castle, or if the square it's moving from has a piece and it is not * moving to itself. Also need to check if the space it's moving to is not occupied by it's own colored pieces. */ public bool IsValidMove(GameBoard gb, Square fromSquare, Square toSquare) { int fromRow = fromSquare.RowID; int fromCol = fromSquare.ColID; int toRow = toSquare.RowID; int toCol = toSquare.ColID; bool isOccupied = MoveValidator.IsOccupied(toSquare); bool isEnemy = MoveValidator.IsEnemy(fromSquare, toSquare); if (fromSquare.Piece == null || fromSquare.Piece == toSquare.Piece) { return(false); } bool isCastle = MoveValidator.IsCastle(gb, fromSquare, toSquare); if (isCastle) { return(true); } if (((toRow == fromRow + 1 && toCol == fromCol) || (toCol == fromCol + 1 && toRow == fromRow) || (toRow == fromRow - 1 && toCol == fromCol) || (toCol == fromCol - 1 && toRow == fromRow) || (toRow == fromRow + 1 && toCol == fromCol + 1) || (toRow == fromRow - 1 && toCol == fromCol - 1) || (toRow == fromRow + 1 && toCol == fromCol - 1) || (toRow == fromRow - 1 && toCol == fromCol + 1)) && (!isOccupied || isEnemy)) { return(true); } return(false); }
public void IsEnemyTest() { GameBoard gb = new GameBoard(8, 8); bool actual = MoveValidator.IsEnemy(gb.squares[6, 0], gb.squares[1, 0]); Assert.AreEqual(true, actual); actual = MoveValidator.IsEnemy(gb.squares[1, 1], gb.squares[1, 0]); Assert.AreEqual(false, actual); }
/* * The knight moves in horizontally or vertically 2 spaces, then the other direction * 1 space. It is only a valid move if the square it's moving from has a piece and it is not moving to * itself. Also need to check if the space it's moving to is not occupied by it's own colored pieces. */ public bool IsValidMove(GameBoard gb, Square fromSquare, Square toSquare) { int fromRow = fromSquare.RowID; int fromCol = fromSquare.ColID; int toRow = toSquare.RowID; int toCol = toSquare.ColID; bool isOccupied = MoveValidator.IsOccupied(toSquare); bool isEnemy = MoveValidator.IsEnemy(fromSquare, toSquare); if (fromSquare.Piece == null || fromSquare.Piece == toSquare.Piece) { return(false); } if (toRow == fromRow + 1 && fromCol == toCol + 2 && (!isOccupied || isEnemy)) { return(true); } else if (toRow == fromRow + 1 && fromCol == toCol - 2 && (!isOccupied || isEnemy)) { return(true); } else if (toRow == fromRow - 1 && fromCol == toCol + 2 && (!isOccupied || isEnemy)) { return(true); } else if (toRow == fromRow - 1 && fromCol == toCol - 2 && (!isOccupied || isEnemy)) { return(true); } else if (toRow == fromRow + 2 && fromCol == toCol + 1 && (!isOccupied || isEnemy)) { return(true); } else if (toRow == fromRow - 2 && fromCol == toCol + 1 && (!isOccupied || isEnemy)) { return(true); } else if (toRow == fromRow + 2 && fromCol == toCol - 1 && (!isOccupied || isEnemy)) { return(true); } else if (toRow == fromRow - 2 && fromCol == toCol - 1 && (!isOccupied || isEnemy)) { return(true); } else { return(false); } }
/* * The Pawn has 4 different moves it can make. A Pawn can only move forward, never backward. * It may move 1 space forward as long as that space is not occupied by any other piece. It can * move 2 spaces forward as long as it has never moved before. It can move diagonally forward 1 * space as long as that space is an enemy. It asks the MoveValidator if it can En Passant. * Under these conditions it is a Valid Move and if the square it's moving from has a piece and it is not * moving to itself. Also need to check if the space it's moving to is not occupied by it's own colored pieces. */ public bool IsValidMove(GameBoard gb, Square fromSquare, Square toSquare) { int fromRow = fromSquare.RowID; int fromCol = fromSquare.ColID; int toRow = toSquare.RowID; int toCol = toSquare.ColID; bool isOccupied = MoveValidator.IsOccupied(toSquare); bool isEnemy = MoveValidator.IsEnemy(fromSquare, toSquare); if (fromSquare.Piece == null || fromSquare.Piece == toSquare.Piece) { return(false); } bool enPassant = MoveValidator.IsEnPassant(gb, fromSquare, toSquare); int sign = fromSquare.Piece.Color == ChessColor.White ? 1 : -1; if (MoveCount == 0) { if ((toRow == fromRow + (2 * sign) && fromCol == toCol && !isOccupied) && (!MoveValidator.IsOccupied(gb.squares[fromRow + (1 * sign), fromCol]))) { return(true); } } if (toRow == fromRow + (1 * sign) && fromCol == toCol && !isOccupied) { return(true); } else if (toRow == fromRow + (1 * sign) && toCol == fromCol + 1 && (isEnemy || enPassant)) { return(true); } else if (toRow == fromRow + (1 * sign) && toCol == fromCol - 1 && (isEnemy || enPassant)) { return(true); } else { return(false); } }