public bool IsCheck() { int[] straightDx = { 0, 1, 1, 1, 0, -1, -1, -1 }; int[] straightDy = { 1, 1, 0, -1, -1, -1, 0, 1 }; int[] knightDx = { 1, 2, 2, 1, -1, -2, -2, -1 }; int[] knightDy = { 2, 1, -1, -2, -2, -1, 1, 2 }; int[] pawnDx = { -1, 1 }; int[] pawnDy = { (int)Team, (int)Team }; Board.PieceGetter getFirstPieceOnPath = new Board.PieceGetter(board.GetFirstPieceOnPath); Board.PieceGetter getPieceAtPoint = new Board.PieceGetter(board.GetPieceAtPoint); return(_IsCheck(straightDx, straightDy, getFirstPieceOnPath) || _IsCheck(knightDx, knightDy, getPieceAtPoint) || _IsCheck(pawnDx, pawnDy, getPieceAtPoint)); }
private bool _IsCheck(int[] dx, int[] dy, Board.PieceGetter GetPiece) { for (int i = 0; i < dx.Length; i++) { Point delta = new Point(dx[i], dy[i]); ImmutablePiece piece = GetPiece(king.CurrentPoint, delta); if (piece == null || king.IsFriendly(piece)) { continue; } if (piece.CanMoveTo(king.CurrentPoint, board)) { return(true); } } return(false); }