/// <summary> /// /// </summary> /// <param name="availableCastles">At init : both. queen side rook moves: KingSide or king side rook moves: QueenSide. If both rooks moved or king moved: none. /// The value passed here is the CHANGE, if one rook is moved, that is passed, if king is moved none is passed, if neither both is passed.</param> /// <param name="squareFrom">null if no from move</param> public void Update( ChessPieceType pieceType, ChessPieceColors color, BoardSquare?squareTo = null, BoardSquare?squareFrom = null, ZobristCastling?availableCastles = null, BoardSquare?enPassant = null ) { int colorMoving = color == ChessPieceColors.White ? 0 : 1; if (squareFrom.HasValue) { /* Remove bit from previous position */ int squareIndexFrom = GetIndexFromBoardSquare(squareFrom.Value); ulong keyd = _pieces[(int)pieceType, colorMoving, squareIndexFrom]; Key ^= _pieces[(int)pieceType, colorMoving, squareIndexFrom]; #if DEBUG _debugPieces[(int)pieceType, colorMoving, squareIndexFrom] = !_debugPieces[(int)pieceType, colorMoving, squareIndexFrom]; #endif } if (squareTo.HasValue) { /* Add bit from new position */ int squareIndexTo = GetIndexFromBoardSquare(squareTo.Value); ulong keyd = _pieces[(int)pieceType, colorMoving, squareIndexTo]; Key ^= _pieces[(int)pieceType, colorMoving, squareIndexTo]; #if DEBUG _debugPieces[(int)pieceType, colorMoving, squareIndexTo] = !_debugPieces[(int)pieceType, colorMoving, squareIndexTo]; #endif } if (enPassant.HasValue) { int squareIndexEnPassant = GetIndexFromBoardSquare(enPassant.Value); ulong keyd = _enPassant[colorMoving, squareIndexEnPassant]; Key ^= _enPassant[colorMoving, squareIndexEnPassant]; #if DEBUG _debugEnPassant[colorMoving, squareIndexEnPassant] = !_debugEnPassant[colorMoving, squareIndexEnPassant]; #endif } if (availableCastles.HasValue) { ulong keyd = _castling[colorMoving, (int)availableCastles.Value]; Key ^= _castling[colorMoving, (int)availableCastles.Value]; #if DEBUG _debugCastling[colorMoving, (int)availableCastles.Value] = !_debugCastling[colorMoving, (int)availableCastles.Value]; #endif } }
/// <summary> /// Decides the best move for the player given by the color parameter, with a search /// of the given depth. /// </summary> public static ColoredBitBoard GetBestMove(ChessBoard board, int depth, ChessPieceColors color, GenerateMoveDelegate GenerateMoves, GetMaterialDelegate GetMaterialValue) { Debug.Assert(board != null); Debug.Assert(depth > 0); Debug.Assert(GenerateMoves != null); Debug.Assert(GetMaterialValue != null); //Alpha, have to add one to change symbol later otherwise overflow. double a = int.MinValue + 1; //Beta value. double b = int.MaxValue; double val = 0; ColoredBitBoard bestMove = null; List <ColoredBitBoard> moves = GenerateMoves(board, color); var ttEntry = new TranspositionEntry(board.BoardHash.Key, depth, val, false, EntryType.Exact); foreach (ColoredBitBoard move in moves) { board.Update(move); if (color == ChessPieceColors.Black) { val = -NegaMaxAlgorithm(board, depth - 1, -b, -a, ChessPieceColors.White, GenerateMoves, GetMaterialValue); } else if (color == ChessPieceColors.White) { val = -NegaMaxAlgorithm(board, depth - 1, -b, -a, ChessPieceColors.Black, GenerateMoves, GetMaterialValue); } if (val > a) { a = val; bestMove = move; ttEntry.BestMove = move; ttEntry.Score = a; } board.Undo(); } TranspositionTable.TranspositionCache.Add(ttEntry); return(bestMove); }
/// <summary> /// Determines whether the given board is in a terminal state, for the /// player given by the color parameter. /// </summary> /// <param name="board"></param> /// <param name="color"></param> /// <param name="moveDelegate"></param> /// <returns></returns> private static TerminalConditions IsTerminal(ChessBoard board, ChessPieceColors color, GenerateMoveDelegate moveGenerator) { List <ColoredBitBoard> legalMoves = new List <ColoredBitBoard>(); legalMoves = moveGenerator(board, color); if (color == ChessPieceColors.White) { if (legalMoves.Count == 0 && board.WhiteKing.IsChecked) { return(TerminalConditions.Win); } else if (legalMoves.Count == 0 && board.WhiteKing.IsChecked == false) { return(TerminalConditions.Draw); } else { return(TerminalConditions.NotTerminal); } } if (color == ChessPieceColors.Black) { if (legalMoves.Count == 0 && board.BlackKing.IsChecked) { return(TerminalConditions.Win); } else if (legalMoves.Count == 0 && board.BlackKing.IsChecked == false) { return(TerminalConditions.Draw); } else { return(TerminalConditions.NotTerminal); } } throw new Exception("Color should be either White or Black"); }
public static List <BishopBitBoard> BishopBitBoardResults(ChessBoard inputChessBoard, ChessPieceColors color, BitBoard blackPieces, BitBoard whitePieces, BitBoard allPieces) { List <BishopBitBoard> result = new List <BishopBitBoard>(); List <Tuple <BishopBitBoard, BishopBitBoard> > legalBishopMoves = new List <Tuple <BishopBitBoard, BishopBitBoard> >(); List <BishopBitBoard> sepBishopsInput = new List <BishopBitBoard>(); List <BishopBitBoard> sepBishopsOutput = new List <BishopBitBoard>(); if (color == ChessPieceColors.White) { sepBishopsInput = ColoredBitBoard.SplitBitBoard(inputChessBoard.WhiteBishop).ToList(); foreach (BishopBitBoard sepBishopBB in sepBishopsInput) { legalBishopMoves.Add(new Tuple <BishopBitBoard, BishopBitBoard>(sepBishopBB, ComputeWhiteBishop(sepBishopBB, whitePieces, allPieces))); } for (int i = 0; i < legalBishopMoves.Count; i++) { sepBishopsOutput = ColoredBitBoard.SplitBitBoard(legalBishopMoves[i].Item2).ToList(); foreach (BishopBitBoard sepBishopOutBB in sepBishopsOutput) { BishopBitBoard boardResult = new BishopBitBoard(color); boardResult.Bits = (inputChessBoard.WhiteBishop.Bits ^ legalBishopMoves[i].Item1.Bits) | sepBishopOutBB.Bits; result.Add(boardResult); } } } else { sepBishopsInput = ColoredBitBoard.SplitBitBoard(inputChessBoard.BlackBishop).ToList(); foreach (BishopBitBoard sepBishopBB in sepBishopsInput) { legalBishopMoves.Add(new Tuple <BishopBitBoard, BishopBitBoard>(sepBishopBB, ComputeBlackBishop(sepBishopBB, blackPieces, allPieces))); } for (int i = 0; i < legalBishopMoves.Count; i++) { sepBishopsOutput = ColoredBitBoard.SplitBitBoard(legalBishopMoves[i].Item2).ToList(); foreach (BishopBitBoard sepBishopOutBB in sepBishopsOutput) { BishopBitBoard boardResult = new BishopBitBoard(color); boardResult.Bits = (inputChessBoard.BlackBishop.Bits ^ legalBishopMoves[i].Item1.Bits) | sepBishopOutBB.Bits; result.Add(boardResult); } } } return(result); }
private static bool isOwnKingAttackable(ChessBoard cb, ColoredBitBoard legalMove, ChessPieceColors color) { List <ColoredBitBoard> legalMoves = new List <ColoredBitBoard>(); EmptyBitBoard blackPieces = new EmptyBitBoard(); EmptyBitBoard whitePieces = new EmptyBitBoard(); EmptyBitBoard allPieces = new EmptyBitBoard(); cb.Update(legalMove); blackPieces = MoveGenUtils.SetBlackBoard(cb); whitePieces = MoveGenUtils.SetWhiteBoard(cb); allPieces = MoveGenUtils.SetWholeBoard(cb); legalMoves.AddRange(GeneratePseudoLegalNonSlidingMoves(cb, color, blackPieces, whitePieces, allPieces)); legalMoves.AddRange(GeneratePseudoLegalSlidingMoves(cb, color, blackPieces, whitePieces, allPieces)); foreach (ColoredBitBoard cbb in legalMoves) { if (color == ChessPieceColors.White && (cb.BlackKing.Bits & cbb.Bits) != 0) { cb.Undo(); return(true); } else if (color == ChessPieceColors.Black && (cb.WhiteKing.Bits & cbb.Bits) != 0) { cb.Undo(); return(true); } } cb.Undo(); return(false); }
private static bool isEnemyKingAttackable(ChessBoard cb, ColoredBitBoard legalMove, ChessPieceColors color) { List <ColoredBitBoard> legalMoves = new List <ColoredBitBoard>(); EmptyBitBoard blackPieces = new EmptyBitBoard(); EmptyBitBoard whitePieces = new EmptyBitBoard(); EmptyBitBoard allPieces = new EmptyBitBoard(); cb.Update(legalMove); blackPieces = MoveGenUtils.SetBlackBoard(cb); whitePieces = MoveGenUtils.SetWhiteBoard(cb); allPieces = MoveGenUtils.SetWholeBoard(cb); if (legalMove is PawnBitBoard) { legalMoves.AddRange(PawnMoveGen.PawnBitBoardResults(cb, color, blackPieces, whitePieces, allPieces)); } else if (legalMove is BishopBitBoard) { legalMoves.AddRange(BishopMoveGen.BishopBitBoardResults(cb, color, blackPieces, whitePieces, allPieces)); } else if (legalMove is KingBitBoard) { legalMoves.AddRange(KingMoveGen.KingBitBoardResults(cb, color, blackPieces, whitePieces, allPieces)); } else if (legalMove is KnightBitBoard) { legalMoves.AddRange(KnightMoveGen.KnightBitBoardResults(cb, color, blackPieces, whitePieces, allPieces)); } else if (legalMove is QueenBitBoard) { legalMoves.AddRange(QueenMoveGen.QueenBitBoardResults(cb, color, blackPieces, whitePieces, allPieces)); } else if (legalMove is RookBitBoard) { legalMoves.AddRange(RookMoveGen.RookBitBoardResults(cb, color, blackPieces, whitePieces, allPieces)); } foreach (ColoredBitBoard cbb in legalMoves) { if (color == ChessPieceColors.White && (cb.BlackKing.Bits & cbb.Bits) != 0) { cb.Undo(); return(true); } else if (color == ChessPieceColors.Black && (cb.WhiteKing.Bits & cbb.Bits) != 0) { cb.Undo(); return(true); } } cb.Undo(); return(false); }
public static List <KingBitBoard> KingBitBoardResults(ChessBoard inputChessBoard, ChessPieceColors color, BitBoard blackPieces, BitBoard whitePieces, BitBoard allPieces) { List <KingBitBoard> result = new List <KingBitBoard>(); KingBitBoard legalWhiteKingMoves = new KingBitBoard(ChessPieceColors.White); KingBitBoard legalBlackKingMoves = new KingBitBoard(ChessPieceColors.Black); List <KingBitBoard> sepKingsOutput = new List <KingBitBoard>(); if (color == ChessPieceColors.White) { legalWhiteKingMoves = ComputeWhiteKing(inputChessBoard.WhiteKing, inputChessBoard, blackPieces, whitePieces, allPieces); sepKingsOutput = ColoredBitBoard.SplitBitBoard(legalWhiteKingMoves).ToList(); foreach (KingBitBoard sepKBB in sepKingsOutput) { result.Add(sepKBB); } _numberOfWhiteKingMoves = result.Count; } else { legalBlackKingMoves = ComputeBlackKing(inputChessBoard.BlackKing, inputChessBoard, blackPieces, whitePieces, allPieces); sepKingsOutput = ColoredBitBoard.SplitBitBoard(legalBlackKingMoves).ToList(); foreach (KingBitBoard sepKBB in sepKingsOutput) { result.Add(sepKBB); } _numberOfBlackKingMoves = result.Count; } return(result); }
public static List <ColoredBitBoard> GenerateLegalMoves(ChessBoard inputChessBoard, ChessPieceColors color) { List <ColoredBitBoard> result = new List <ColoredBitBoard>(); EmptyBitBoard blackPieces = MoveGenUtils.SetBlackBoard(inputChessBoard); EmptyBitBoard whitePieces = MoveGenUtils.SetWhiteBoard(inputChessBoard); EmptyBitBoard allPieces = MoveGenUtils.SetWholeBoard(inputChessBoard); result.AddRange(GeneratePseudoLegalNonSlidingMoves(inputChessBoard, color, blackPieces, whitePieces, allPieces)); result.AddRange(GeneratePseudoLegalSlidingMoves(inputChessBoard, color, blackPieces, whitePieces, allPieces)); result = RemoveOwnKingChecks(inputChessBoard, result, color); result = SetDoesCheck(inputChessBoard, result, color); var sortedMoves = MoveGenUtils.SortMoves(result, inputChessBoard, blackPieces, whitePieces); return(sortedMoves); }
public static List <ColoredBitBoard> GenerateCheckMateMoves(ChessBoard board, ChessPieceColors color) { return(new List <ColoredBitBoard>()); }
/// <summary> /// Implementation of the NegaMax algorithm. /// <param name="board"></param> /// <param name="depth"></param> /// <param name="a"></param> /// <param name="b"></param> /// <param name="color"></param> /// <param name="GenerateMoves"></param> /// <param name="GetMaterialValue"></param> /// <returns></returns> public static double NegaMaxAlgorithm( ChessBoard board, int depth, double a, double b, ChessPieceColors color, GenerateMoveDelegate GenerateMoves, GetMaterialDelegate GetMaterialValue ) { ColoredBitBoard bestMove = null; var hashBeforeTermCondition = board.BoardHash.Key; var termCondition = IsTerminal(board, color, GenerateMoves); Debug.Assert(hashBeforeTermCondition == board.BoardHash.Key); if (termCondition == TerminalConditions.Win || termCondition == TerminalConditions.Draw || depth == 0) { return((int)color * (GetMaterialValue(board))); } else { bool usingTT = false; ulong hash_beforeMove = board.BoardHash.Key; var ttEntry = TranspositionTable.TranspositionCache[board.BoardHash.Key]; if (ttEntry != null && ttEntry.Hash == board.BoardHash.Key && ttEntry.Depth >= depth ) { if (ttEntry.NodeType == EntryType.Alpha && ttEntry.Score <= a) { return(a); } if (ttEntry.NodeType == EntryType.Beta && ttEntry.Score >= b) { return(b); } if (ttEntry.NodeType == EntryType.Exact) { return(ttEntry.Score); } usingTT = true; } TranspositionEntry newEntry = new TranspositionEntry(board.BoardHash.Key, depth, 0, false, EntryType.Alpha); List <ColoredBitBoard> moves = new List <ColoredBitBoard>(); moves = GenerateMoves(board, color); foreach (ColoredBitBoard move in moves) { double val = 0; board.Update(move); var hashBeforeRec = board.BoardHash.Key; if (color == ChessPieceColors.White) { val = -NegaMaxAlgorithm(board, depth - 1, -b, -a, ChessPieceColors.Black, GenerateMoves, GetMaterialValue); } else if (color == ChessPieceColors.Black) { val = -NegaMaxAlgorithm(board, depth - 1, -b, -a, ChessPieceColors.White, GenerateMoves, GetMaterialValue); } Debug.Assert(hashBeforeRec == board.BoardHash.Key); board.Undo(); usingTT = false; if (val >= b) { if (!usingTT) { newEntry.NodeType = EntryType.Beta; newEntry.Score = val; TranspositionTable.TranspositionCache.Add(newEntry); Debug.Assert(hash_beforeMove == board.BoardHash.Key, "Board hash changed during update/undo"); } return(val); } if (val >= a) { a = val; bestMove = move; newEntry.NodeType = EntryType.Exact; } Debug.Assert(hash_beforeMove == board.BoardHash.Key, "Board hash changed during update/undo"); } if (!usingTT) { if (newEntry.NodeType == EntryType.Exact) { newEntry.BestMove = bestMove; } newEntry.Score = a; TranspositionTable.TranspositionCache.Add(newEntry); } return(a); } }
public static bool isOwnKingAttackable_TEST(ChessBoard cb, ColoredBitBoard legalMove, ChessPieceColors color) { bool flag; flag = isOwnKingAttackable(cb, legalMove, color); return(flag); }
public static bool isEnemyKingAttackable_TEST(ChessBoard cb, ColoredBitBoard legalMove, ChessPieceColors color) { return(isEnemyKingAttackable(cb, legalMove, color)); }
private static List <ColoredBitBoard> RemoveOwnKingChecks(ChessBoard inputChessBoard, List <ColoredBitBoard> inputPseudoLegalMoves, ChessPieceColors color) { List <ColoredBitBoard> legalMovesResult = inputPseudoLegalMoves.Select(p => p).ToList(); for (int i = 0; i < inputPseudoLegalMoves.Count; i++) { if (color == ChessPieceColors.White) { if (isOwnKingAttackable(inputChessBoard, inputPseudoLegalMoves[i], ChessPieceColors.Black)) { legalMovesResult.RemoveAt(legalMovesResult.IndexOf(inputPseudoLegalMoves[i])); } } else { if (isOwnKingAttackable(inputChessBoard, inputPseudoLegalMoves[i], ChessPieceColors.White)) { legalMovesResult.RemoveAt(legalMovesResult.IndexOf(inputPseudoLegalMoves[i])); } } } return(legalMovesResult); }
private static List <ColoredBitBoard> SetDoesCheck(ChessBoard inputChessBoard, List <ColoredBitBoard> inputLegalMoves, ChessPieceColors color) { foreach (ColoredBitBoard legalMove in inputLegalMoves) { if (color == ChessPieceColors.White) { if (isEnemyKingAttackable(inputChessBoard, legalMove, color)) { legalMove.DoesCheck = true; } } else { if (isEnemyKingAttackable(inputChessBoard, legalMove, color)) { legalMove.DoesCheck = true; } } } return(inputLegalMoves); }
private static List <ColoredBitBoard> GeneratePseudoLegalSlidingMoves(ChessBoard inputChessBoard, ChessPieceColors color, EmptyBitBoard blackPieces, EmptyBitBoard whitePieces, EmptyBitBoard allPieces) { List <ColoredBitBoard> result = new List <ColoredBitBoard>(); result.AddRange(RookMoveGen.RookBitBoardResults(inputChessBoard, color, blackPieces, whitePieces, allPieces)); result.AddRange(BishopMoveGen.BishopBitBoardResults(inputChessBoard, color, blackPieces, whitePieces, allPieces)); result.AddRange(QueenMoveGen.QueenBitBoardResults(inputChessBoard, color, blackPieces, whitePieces, allPieces)); return(result); }
public static List <PawnBitBoard> PawnBitBoardResults(ChessBoard inputChessBoard, ChessPieceColors color, BitBoard blackPieces, BitBoard whitePieces, BitBoard allPieces) { List <PawnBitBoard> result = new List <PawnBitBoard>(); List <Tuple <PawnBitBoard, PawnBitBoard> > legalPawnMoves = new List <Tuple <PawnBitBoard, PawnBitBoard> >(); List <PawnBitBoard> sepPawnsInput = new List <PawnBitBoard>(); List <PawnBitBoard> sepPawnsOutput = new List <PawnBitBoard>(); if (color == ChessPieceColors.White) { sepPawnsInput = ColoredBitBoard.SplitBitBoard(inputChessBoard.WhitePawn).ToList(); foreach (PawnBitBoard spb in sepPawnsInput) { var newMove = new Tuple <PawnBitBoard, PawnBitBoard>(spb, ComputeWhitePawn(spb, inputChessBoard, blackPieces, whitePieces, allPieces)); legalPawnMoves.Add(newMove); } for (int i = 0; i < legalPawnMoves.Count; i++) { sepPawnsOutput = ColoredBitBoard.SplitBitBoard(legalPawnMoves[i].Item2).ToList(); foreach (PawnBitBoard sepPBB in sepPawnsOutput) { if ((sepPBB.Bits & (BoardSquare)MoveGenUtils.RANK_8) != BoardSquare.Empty) { PawnBitBoard boardResult_B = new PawnBitBoard(color); PawnBitBoard boardResult_R = new PawnBitBoard(color); PawnBitBoard boardResult_N = new PawnBitBoard(color); PawnBitBoard boardResult_Q = new PawnBitBoard(color); boardResult_B.Bits = sepPBB.Bits; boardResult_R.Bits = sepPBB.Bits; boardResult_N.Bits = sepPBB.Bits; boardResult_Q.Bits = sepPBB.Bits; boardResult_B.Promote(PawnBitBoard.PromotionPiece.Bishop); boardResult_R.Promote(PawnBitBoard.PromotionPiece.Rook); boardResult_N.Promote(PawnBitBoard.PromotionPiece.Knight); boardResult_Q.Promote(PawnBitBoard.PromotionPiece.Queen); boardResult_B.Bits = (inputChessBoard.WhitePawn.Bits ^ legalPawnMoves[i].Item1.Bits) | sepPBB.Bits; boardResult_R.Bits = (inputChessBoard.WhitePawn.Bits ^ legalPawnMoves[i].Item1.Bits) | sepPBB.Bits; boardResult_N.Bits = (inputChessBoard.WhitePawn.Bits ^ legalPawnMoves[i].Item1.Bits) | sepPBB.Bits; boardResult_Q.Bits = (inputChessBoard.WhitePawn.Bits ^ legalPawnMoves[i].Item1.Bits) | sepPBB.Bits; result.Add(boardResult_B); result.Add(boardResult_R); result.Add(boardResult_N); result.Add(boardResult_Q); } else { PawnBitBoard boardResult = new PawnBitBoard(color); boardResult.Bits = (inputChessBoard.WhitePawn.Bits ^ legalPawnMoves[i].Item1.Bits) | sepPBB.Bits; var oldBoard = inputChessBoard.GetOldBitBoardFromBitBoard(inputChessBoard.WhitePawn); BoardSquare bitsDiff = (BoardSquare)((ulong)oldBoard.Bits ^ (ulong)boardResult.Bits); System.Diagnostics.Debug.Assert(new EmptyBitBoard((BoardSquare)((ulong)oldBoard.Bits ^ (ulong)boardResult.Bits)).Count == 2, "More than one piece moved"); result.Add(boardResult); } } } } else { sepPawnsInput = ColoredBitBoard.SplitBitBoard(inputChessBoard.BlackPawn).ToList(); foreach (PawnBitBoard spb in sepPawnsInput) { legalPawnMoves.Add(new Tuple <PawnBitBoard, PawnBitBoard>(spb, ComputeBlackPawn(spb, inputChessBoard, blackPieces, whitePieces, allPieces))); } for (int i = 0; i < legalPawnMoves.Count; i++) { sepPawnsOutput = ColoredBitBoard.SplitBitBoard(legalPawnMoves[i].Item2).ToList(); foreach (PawnBitBoard sepPBB in sepPawnsOutput) { if ((sepPBB.Bits & (BoardSquare)MoveGenUtils.RANK_1) != BoardSquare.Empty) { PawnBitBoard boardResult_B = new PawnBitBoard(color); PawnBitBoard boardResult_R = new PawnBitBoard(color); PawnBitBoard boardResult_N = new PawnBitBoard(color); PawnBitBoard boardResult_Q = new PawnBitBoard(color); boardResult_B.Bits = sepPBB.Bits; boardResult_R.Bits = sepPBB.Bits; boardResult_N.Bits = sepPBB.Bits; boardResult_Q.Bits = sepPBB.Bits; boardResult_B.Promote(PawnBitBoard.PromotionPiece.Bishop); boardResult_R.Promote(PawnBitBoard.PromotionPiece.Rook); boardResult_N.Promote(PawnBitBoard.PromotionPiece.Knight); boardResult_Q.Promote(PawnBitBoard.PromotionPiece.Queen); boardResult_B.Bits = (inputChessBoard.BlackPawn.Bits ^ legalPawnMoves[i].Item1.Bits) | sepPBB.Bits; boardResult_R.Bits = (inputChessBoard.BlackPawn.Bits ^ legalPawnMoves[i].Item1.Bits) | sepPBB.Bits; boardResult_N.Bits = (inputChessBoard.BlackPawn.Bits ^ legalPawnMoves[i].Item1.Bits) | sepPBB.Bits; boardResult_Q.Bits = (inputChessBoard.BlackPawn.Bits ^ legalPawnMoves[i].Item1.Bits) | sepPBB.Bits; result.Add(boardResult_B); result.Add(boardResult_R); result.Add(boardResult_N); result.Add(boardResult_Q); } else { PawnBitBoard boardResult = new PawnBitBoard(color); boardResult.Bits = (inputChessBoard.BlackPawn.Bits ^ legalPawnMoves[i].Item1.Bits) | sepPBB.Bits; result.Add(boardResult); } } } } return(result); }
public static List <KingBitBoard> Test_KingBitBoardResults(ChessBoard inputChessBoard, ChessPieceColors color, BitBoard blackPieces, BitBoard whitePieces, BitBoard allPieces) { return(KingBitBoardResults(inputChessBoard, color, blackPieces, whitePieces, allPieces)); }
public static List <ColoredBitBoard> GenerateStaticMoves_Depth12(ChessBoard board, ChessPieceColors color) { List <ColoredBitBoard> legalMoves = new List <ColoredBitBoard>(); if (color == ChessPieceColors.Black) { if (board.WhitePawn.Bits == BoardSquare.B3) { PawnBitBoard move1 = new PawnBitBoard(ChessPieceColors.Black); move1.Bits = (board.BlackPawn.Bits ^ BoardSquare.D3) | BoardSquare.D2; KingBitBoard move2 = new KingBitBoard(ChessPieceColors.Black); move2.Bits = BoardSquare.B3; legalMoves.Add(move1); legalMoves.Add(move2); } else if (board.WhitePawn.Bits == BoardSquare.D3) { PawnBitBoard move3 = new PawnBitBoard(ChessPieceColors.Black); move3.Bits = (board.BlackPawn.Bits ^ BoardSquare.B3) | BoardSquare.B2; KingBitBoard move4 = new KingBitBoard(ChessPieceColors.Black); move4.Bits = BoardSquare.A3; legalMoves.Add(move3); legalMoves.Add(move4); } else if (board.WhitePawn.Bits == BoardSquare.C3) { KingBitBoard move8 = new KingBitBoard(ChessPieceColors.Black); move8.Bits = BoardSquare.C3; KingBitBoard move9 = new KingBitBoard(ChessPieceColors.Black); move9.Bits = BoardSquare.C4; legalMoves.Add(move8); legalMoves.Add(move9); } } else if (color == ChessPieceColors.White) { PawnBitBoard move5 = new PawnBitBoard(ChessPieceColors.White); move5.Bits = (board.WhitePawn.Bits ^ BoardSquare.C2) | BoardSquare.B3; PawnBitBoard move6 = new PawnBitBoard(ChessPieceColors.White); move6.Bits = (board.WhitePawn.Bits ^ BoardSquare.C2) | BoardSquare.D3; PawnBitBoard move7 = new PawnBitBoard(ChessPieceColors.White); move7.Bits = (board.WhitePawn.Bits ^ BoardSquare.C2) | BoardSquare.C3; legalMoves.Add(move5); legalMoves.Add(move6); legalMoves.Add(move7); } return(legalMoves); }
public static TerminalConditions IsTerminal_Debug(ChessBoard board, ChessPieceColors color, GenerateMoveDelegate moveGenerator) { return(IsTerminal(board, color, moveGenerator)); }
public static List <ColoredBitBoard> GenerateStaticMoves_White_Depth1(ChessBoard board, ChessPieceColors color) { List <ColoredBitBoard> legalMoves = new List <ColoredBitBoard>(); //White king moves KingBitBoard move1 = new KingBitBoard(ChessPieceColors.White); KingBitBoard move2 = new KingBitBoard(ChessPieceColors.White); KingBitBoard move3 = new KingBitBoard(ChessPieceColors.White); move1.Bits = BoardSquare.H2; move2.Bits = BoardSquare.G1; move3.Bits = BoardSquare.G2; //White pawn moves PawnBitBoard pawnEnd1 = new PawnBitBoard(ChessPieceColors.White); PawnBitBoard pawnEnd2 = new PawnBitBoard(ChessPieceColors.White); PawnBitBoard pawnEnd3 = new PawnBitBoard(ChessPieceColors.White); pawnEnd1.Bits = BoardSquare.C3; pawnEnd2.Bits = BoardSquare.D3; pawnEnd3.Bits = BoardSquare.B3; PawnBitBoard move4 = new PawnBitBoard(ChessPieceColors.White); PawnBitBoard move5 = new PawnBitBoard(ChessPieceColors.White); PawnBitBoard move6 = new PawnBitBoard(ChessPieceColors.White); move4.Bits = (board.WhitePawn.Bits ^ BoardSquare.C2) | pawnEnd1.Bits; move5.Bits = (board.WhitePawn.Bits ^ BoardSquare.C2) | pawnEnd2.Bits; move6.Bits = (board.WhitePawn.Bits ^ BoardSquare.C2) | pawnEnd3.Bits; legalMoves.Add(move1); legalMoves.Add(move2); legalMoves.Add(move3); legalMoves.Add(move4); legalMoves.Add(move5); legalMoves.Add(move6); return(legalMoves); }
public ColoredBitBoard ConvertStringMoveToBitBoard(string stringMove) { string stringFromCoordinate = string.Format("{0}{1}", stringMove[0], stringMove[1]); string stringToCoordinate = string.Format("{0}{1}", stringMove[2], stringMove[3]); string stringPromotion = "None"; BoardSquare fromCoordinate = (BoardSquare)Enum.Parse(typeof(BoardSquare), stringFromCoordinate, true); BoardSquare toCoordinate = (BoardSquare)Enum.Parse(typeof(BoardSquare), stringToCoordinate, true); if (_chessBoard.GetBitBoardFromSquare(fromCoordinate, _engineColor) == null) { if (_engineColor == ChessPieceColors.White) { _engineColor = ChessPieceColors.Black; } else { _engineColor = ChessPieceColors.White; } } ColoredBitBoard moveBitBoard = _chessBoard.GetBitBoardFromSquare(fromCoordinate, _engineColor); if (moveBitBoard != null) { moveBitBoard.Bits ^= fromCoordinate; moveBitBoard.Bits |= toCoordinate; } if (stringMove.Length == 5) { stringPromotion = stringMove[4].ToString().ToLower(); PawnBitBoard movePromotionPawnBitBoard = (PawnBitBoard)moveBitBoard; switch (stringPromotion) { case "q": movePromotionPawnBitBoard.Promote(PawnBitBoard.PromotionPiece.Queen); break; case "r": movePromotionPawnBitBoard.Promote(PawnBitBoard.PromotionPiece.Rook); break; case "b": movePromotionPawnBitBoard.Promote(PawnBitBoard.PromotionPiece.Bishop); break; case "n": movePromotionPawnBitBoard.Promote(PawnBitBoard.PromotionPiece.Knight); break; default: throw new ArgumentOutOfRangeException("Move string 'promotion char' can only be q, r, b or n"); } return(movePromotionPawnBitBoard); } else if (stringMove.Length > 5) { throw new ArgumentOutOfRangeException("Move sting cannot be over five chars. It should be in the form 'e7e5' or 'e7e8q'."); } return(moveBitBoard); }
public static List <ColoredBitBoard> GenerateStaticMoves_Black_Depth1(ChessBoard board, ChessPieceColors color) { List <ColoredBitBoard> legalMoves = new List <ColoredBitBoard>(); //Black king moves KingBitBoard move1 = new KingBitBoard(ChessPieceColors.Black); KingBitBoard move2 = new KingBitBoard(ChessPieceColors.Black); KingBitBoard move3 = new KingBitBoard(ChessPieceColors.Black); KingBitBoard move4 = new KingBitBoard(ChessPieceColors.Black); KingBitBoard move5 = new KingBitBoard(ChessPieceColors.Black); KingBitBoard move6 = new KingBitBoard(ChessPieceColors.Black); KingBitBoard move7 = new KingBitBoard(ChessPieceColors.Black); move1.Bits = BoardSquare.A3; move2.Bits = BoardSquare.C3; move3.Bits = BoardSquare.A4; move4.Bits = BoardSquare.C4; move5.Bits = BoardSquare.A5; move6.Bits = BoardSquare.B5; move7.Bits = BoardSquare.C5; //Black Pawn moves, pawn at B3 PawnBitBoard pawnEnd11 = new PawnBitBoard(ChessPieceColors.Black); PawnBitBoard pawnEnd12 = new PawnBitBoard(ChessPieceColors.Black); PawnBitBoard move8 = new PawnBitBoard(ChessPieceColors.Black); PawnBitBoard move9 = new PawnBitBoard(ChessPieceColors.Black); pawnEnd11.Bits = BoardSquare.B2; pawnEnd12.Bits = BoardSquare.C2; move8.Bits = (board.BlackPawn.Bits ^ BoardSquare.B3) | pawnEnd11.Bits; move9.Bits = (board.BlackPawn.Bits ^ BoardSquare.B3) | pawnEnd12.Bits; //Pawn at D3 PawnBitBoard pawnEnd21 = new PawnBitBoard(ChessPieceColors.Black); PawnBitBoard pawnEnd22 = new PawnBitBoard(ChessPieceColors.Black); PawnBitBoard move10 = new PawnBitBoard(ChessPieceColors.Black); PawnBitBoard move11 = new PawnBitBoard(ChessPieceColors.Black); pawnEnd21.Bits = BoardSquare.C2; pawnEnd22.Bits = BoardSquare.D2; move10.Bits = (board.BlackPawn.Bits ^ BoardSquare.D3) | pawnEnd21.Bits; move11.Bits = (board.BlackPawn.Bits ^ BoardSquare.D3) | pawnEnd22.Bits; legalMoves.Add(move1); legalMoves.Add(move2); legalMoves.Add(move3); legalMoves.Add(move4); legalMoves.Add(move5); legalMoves.Add(move6); legalMoves.Add(move7); legalMoves.Add(move8); legalMoves.Add(move9); legalMoves.Add(move10); legalMoves.Add(move11); return(legalMoves); }
public static List <ColoredBitBoard> SetDoesCheck_TEST(ChessBoard inputChessBoard, List <ColoredBitBoard> inputLegalMoves, ChessPieceColors color) { return(SetDoesCheck(inputChessBoard, inputLegalMoves, color)); }
public static List <ColoredBitBoard> RemoveOwnKingChecks_TEST(ChessBoard inputChessBoard, List <ColoredBitBoard> inputPseudoLegalMoves, ChessPieceColors color) { return(RemoveOwnKingChecks(inputChessBoard, inputPseudoLegalMoves, color)); }
public static List <KnightBitBoard> KnightBitBoardResults(ChessBoard inputChessBoard, ChessPieceColors color, BitBoard blackPieces, BitBoard whitePieces, BitBoard allPieces) { List <KnightBitBoard> result = new List <KnightBitBoard>(); List <Tuple <KnightBitBoard, KnightBitBoard> > legalKnightMoves = new List <Tuple <KnightBitBoard, KnightBitBoard> >(); List <KnightBitBoard> sepKnightsInput = new List <KnightBitBoard>(); List <KnightBitBoard> sepKnightsOutput = new List <KnightBitBoard>(); if (color == ChessPieceColors.White) { sepKnightsInput = ColoredBitBoard.SplitBitBoard(inputChessBoard.WhiteKnight).ToList(); foreach (KnightBitBoard sepKnightBB in sepKnightsInput) { legalKnightMoves.Add(new Tuple <KnightBitBoard, KnightBitBoard>(sepKnightBB, ComputeWhiteKnight(inputChessBoard, sepKnightBB, blackPieces, whitePieces, allPieces))); } for (int i = 0; i < legalKnightMoves.Count; i++) { sepKnightsOutput = ColoredBitBoard.SplitBitBoard(legalKnightMoves[i].Item2).ToList(); foreach (KnightBitBoard sepKnightOutBB in sepKnightsOutput) { KnightBitBoard boardResult = new KnightBitBoard(color); boardResult.Bits = (inputChessBoard.WhiteKnight.Bits ^ legalKnightMoves[i].Item1.Bits) | sepKnightOutBB.Bits; result.Add(boardResult); } } } else { sepKnightsInput = ColoredBitBoard.SplitBitBoard(inputChessBoard.BlackKnight).ToList(); foreach (KnightBitBoard sepKnightBB in sepKnightsInput) { legalKnightMoves.Add(new Tuple <KnightBitBoard, KnightBitBoard>(sepKnightBB, ComputeBlackKnight(inputChessBoard, sepKnightBB, blackPieces, whitePieces, allPieces))); } for (int i = 0; i < legalKnightMoves.Count; i++) { sepKnightsOutput = ColoredBitBoard.SplitBitBoard(legalKnightMoves[i].Item2).ToList(); foreach (KnightBitBoard sepKnightOutBB in sepKnightsOutput) { KnightBitBoard boardResult = new KnightBitBoard(color); boardResult.Bits = (inputChessBoard.BlackKnight.Bits ^ legalKnightMoves[i].Item1.Bits) | sepKnightOutBB.Bits; result.Add(boardResult); } } } return(result); }
private static List <ColoredBitBoard> GeneratePseudoLegalNonSlidingMoves(ChessBoard inputChessBoard, ChessPieceColors color, EmptyBitBoard blackPieces, EmptyBitBoard whitePieces, EmptyBitBoard allPieces) { List <ColoredBitBoard> result = new List <ColoredBitBoard>(); result.AddRange(PawnMoveGen.PawnBitBoardResults(inputChessBoard, color, blackPieces, whitePieces, allPieces)); result.AddRange(KingMoveGen.KingBitBoardResults(inputChessBoard, color, blackPieces, whitePieces, allPieces)); result.AddRange(KnightMoveGen.KnightBitBoardResults(inputChessBoard, color, blackPieces, whitePieces, allPieces)); return(result); }
public StringBitboardConverter(ChessBoard chessBoard, ChessPieceColors engineColor) { _chessBoard = chessBoard; _engineColor = engineColor; }