private bool IsMoveValid(Piece piece, ChessBoardCell proposedMovementCell) { var oldPosition = piece.ChessBoardCell; var oldCellsUnderAttack = CellsUnderAttackBySide[piece.Side.Opposite()]; piece.ChessBoardCell = proposedMovementCell; var oldPieceAtProposedMovement = proposedMovementCell.Piece; proposedMovementCell.Piece = piece; RecalculateCellsUnderAttack(piece.Side.Opposite()); var myKing = PiecesBySideInPlay[piece.Side][typeof(King)].First(); // If my movement would put my king under attack (or leave it under attack), this is not a valid move. var isValid = !CellsUnderAttackBySide[piece.Side.Opposite()].Contains(myKing.ChessBoardCell); // Return the state back to where it was. piece.ChessBoardCell = oldPosition; oldPosition.Piece = piece; CellsUnderAttackBySide[piece.Side.Opposite()] = oldCellsUnderAttack; proposedMovementCell.Piece = oldPieceAtProposedMovement; return(isValid); }
public bool TryGetCellFromPosition(int x, int y, out ChessBoardCell chessBoardCell) { chessBoardCell = null; if (x < 0 || x > 7 || y < 0 || y > 7) { return(false); } chessBoardCell = _boardCells[x][y]; return(true); }
private void InitializeCells() { var boardCells = new ChessBoardCell[8][]; for (var x = 0; x < 8; x++) { var row = new ChessBoardCell[8]; for (var y = 0; y < 8; y++) { row[y] = new ChessBoardCell(x, y, this); } boardCells[x] = row; } _boardCells = boardCells; }
private void InitializeCells() { var boardCells = new ChessBoardCell[8][]; for (var rowNumber = 0; rowNumber < 8; rowNumber++) { var row = new ChessBoardCell[8]; for (var columnNumber = 0; columnNumber < 8; columnNumber++) { row[columnNumber] = new ChessBoardCell(rowNumber, columnNumber, this); } boardCells[rowNumber] = row; } _boardCells = boardCells; }