public static List <Move> GetChecks(byte[] gamePosition, byte kingColor, List <Move> possibleEnemyMoves) { HelperVector2Int tempKing = PieceData.GetKingPosition(gamePosition, kingColor); HelperVector2Int KingPosition; if (tempKing != null) { KingPosition = tempKing; } else { return(new List <Move>()); } return(possibleEnemyMoves.Where(x => x.TargetPosition.x.Equals(KingPosition.x) && x.TargetPosition.y.Equals(KingPosition.y)).ToList()); }
public static int NegaMax(int depth, int alpha, int beta, byte[] gamePosition, byte color, out Move bestmove) { bestmove = null; if (depth == 0) { return(EvaluatePosition(gamePosition, color)); } byte EnemyColor = (color.Equals(PieceData.White)) ? (PieceData.Black) : (PieceData.White); List <Move> Moves = GetAllPossibleMoves(gamePosition, color, true); if (Moves.Count == 0) { if (PieceData.GetChecks(gamePosition, color, GetAllPossibleMoves(gamePosition, EnemyColor, true)).Any()) { return(NegInfinity); } return(0); } int value = NegInfinity; Move bestMoveout = null; foreach (Move move in Moves) { byte[] tempGamePosition = new byte[64]; Array.Copy(gamePosition, tempGamePosition, gamePosition.Length); tempGamePosition[move.StartPosition.x + move.StartPosition.y * 8] = PieceData.None; tempGamePosition[move.TargetPosition.x + move.TargetPosition.y * 8] = move.EndPiece; int eval = -NegaMax(depth - 1, -beta, -alpha, tempGamePosition, EnemyColor, out bestMoveout); if (eval >= value) { value = eval; bestmove = move; } alpha = (alpha >= value) ? (alpha) : (value); if (alpha > beta) { break; } } return(value); }
public static List <Move> GetAllPossibleMoves(byte[] gamePosition, byte color, bool CheckForCheck = false) { List <Move> PossibleMoves = new List <Move>(); for (int y = 0; y < 8; ++y) { for (int x = 0; x < 8; ++x) { if (!((gamePosition[x + y * 8] & color) != 0)) { continue; } if ((gamePosition[x + y * 8] & PieceData.Pawn) != 0) { PossibleMoves.AddRange(PieceData.GetPawnMoves(gamePosition, new HelperVector2Int(x, y))); } if ((gamePosition[x + y * 8] & PieceData.Knight) != 0) { PossibleMoves.AddRange(PieceData.GetKnightMoves(gamePosition, new HelperVector2Int(x, y))); } if ((gamePosition[x + y * 8] & PieceData.Bishop) != 0) { PossibleMoves.AddRange(PieceData.GetBishopMoves(gamePosition, new HelperVector2Int(x, y))); } if ((gamePosition[x + y * 8] & PieceData.Rook) != 0) { PossibleMoves.AddRange(PieceData.GetRookMoves(gamePosition, new HelperVector2Int(x, y))); } if ((gamePosition[x + y * 8] & PieceData.Queen) != 0) { PossibleMoves.AddRange(PieceData.GetQueenMoves(gamePosition, new HelperVector2Int(x, y))); } if ((gamePosition[x + y * 8] & PieceData.King) != 0) { PossibleMoves.AddRange(PieceData.GetKingMoves(gamePosition, new HelperVector2Int(x, y))); } } } foreach (Move move in PossibleMoves) { move.StartPiece += color; move.EndPiece += color; } if (CheckForCheck) { byte EnemyColor = (color.Equals(PieceData.White)) ? (PieceData.Black) : (PieceData.White); List <Move> CheckingMoves = PieceData.GetChecks(gamePosition, color, GetAllPossibleMoves(gamePosition, EnemyColor, false)); List <Move> Remove = new List <Move>(); //if so find move that removes all checking moves foreach (Move move in PossibleMoves) { byte[] AfterMovePossition = new byte[64]; Array.Copy(gamePosition, AfterMovePossition, gamePosition.Length); AfterMovePossition[move.TargetPosition.x + move.TargetPosition.y * 8] = (move.EndPiece); AfterMovePossition[move.StartPosition.x + move.StartPosition.y * 8] = PieceData.None; if (PieceData.GetChecks(AfterMovePossition, color, GetAllPossibleMoves(AfterMovePossition, EnemyColor, false)).Any()) { Remove.Add(move); } } foreach (Move move in Remove) { PossibleMoves.Remove(move); } } return(PossibleMoves); }