public void ConvertBitBoardMoveToString_IsCorrectPromotionKnightStringReturned_Equals() { ChessBoard chessBoard = new ChessBoard(); chessBoard.WhitePawn.Bits = BoardSquare.A2 | BoardSquare.B2 | BoardSquare.C2 | BoardSquare.D2 | BoardSquare.E7 | BoardSquare.F2 | BoardSquare.G2 | BoardSquare.H2; PawnBitBoard toCoordinate = new PawnBitBoard(ChessPieceColors.White); toCoordinate.Bits = BoardSquare.A2 | BoardSquare.B2 | BoardSquare.C2 | BoardSquare.D2 | BoardSquare.E8 | BoardSquare.F2 | BoardSquare.G2 | BoardSquare.H2; toCoordinate.Promote(PawnBitBoard.PromotionPiece.Knight); string correctMoveString = "e7e8n"; StringBitboardConverter converter = new StringBitboardConverter(chessBoard, ChessPieceColors.White); Assert.Equal(correctMoveString, converter.ConvertBitBoardMoveToString(toCoordinate)); }
public void ConvertStringMoveToBitBoard_IsCorrectPromotionKnigtBitBoardReturned_Equal() { ChessBoard chessBoard = new ChessBoard(); chessBoard.WhitePawn.Bits = BoardSquare.A2 | BoardSquare.B2 | BoardSquare.C2 | BoardSquare.D2 | BoardSquare.E7 | BoardSquare.F2 | BoardSquare.G2 | BoardSquare.H2; string stringMove = "e7e8n"; StringBitboardConverter converter = new StringBitboardConverter(chessBoard, ChessPieceColors.White); PawnBitBoard currentMoveBitBoard = (PawnBitBoard)converter.ConvertStringMoveToBitBoard(stringMove); PawnBitBoard correctMoveBitBoard = new PawnBitBoard(ChessPieceColors.White); correctMoveBitBoard.Bits = BoardSquare.A2 | BoardSquare.B2 | BoardSquare.C2 | BoardSquare.D2 | BoardSquare.E8 | BoardSquare.F2 | BoardSquare.G2 | BoardSquare.H2; correctMoveBitBoard.Promote(PawnBitBoard.PromotionPiece.Knight); Assert.Equal(correctMoveBitBoard.GetType(), currentMoveBitBoard.GetType()); Assert.Equal(correctMoveBitBoard.Bits, currentMoveBitBoard.Bits); Assert.Equal(correctMoveBitBoard.Promotion, currentMoveBitBoard.Promotion); }
public void Undo_BlackPromotionQueen_Equal() { ChessBoard testBoard = new ChessBoard(); testBoard.InitializeGame(); PawnBitBoard move1 = new PawnBitBoard(ChessPieceColors.White); move1.Bits = (testBoard.WhitePawn.Bits ^ BoardSquare.G2) | BoardSquare.G4; PawnBitBoard move2 = new PawnBitBoard(ChessPieceColors.Black); move2.Bits = (testBoard.BlackPawn.Bits ^ BoardSquare.H7) | BoardSquare.H5; PawnBitBoard move3 = new PawnBitBoard(ChessPieceColors.White); move3.Bits = (move1.Bits ^ BoardSquare.H2) | BoardSquare.H3; PawnBitBoard move4 = new PawnBitBoard(ChessPieceColors.Black); move4.Bits = (move2.Bits ^ BoardSquare.H5) | BoardSquare.G4; KnightBitBoard move5 = new KnightBitBoard(ChessPieceColors.White); move5.Bits = (testBoard.WhiteKnight.Bits ^ BoardSquare.G1) | BoardSquare.H3; //PawnBitBoard move4 = new PawnBitBoard( ChessPieceColors.Black ); //move4.Bits = ( move2.Bits ^ BoardSquare.G5 ) | BoardSquare.H4; PawnBitBoard move6 = new PawnBitBoard(ChessPieceColors.Black); move6.Bits = (move4.Bits ^ BoardSquare.G4) | BoardSquare.G3; KnightBitBoard move7 = new KnightBitBoard(ChessPieceColors.White); move7.Bits = (move5.Bits ^ BoardSquare.H3) | BoardSquare.G1; PawnBitBoard move8 = new PawnBitBoard(ChessPieceColors.Black); move8.Bits = (move6.Bits ^ BoardSquare.G3) | BoardSquare.G2; KnightBitBoard move9 = new KnightBitBoard(ChessPieceColors.White); move9.Bits = (move7.Bits ^ BoardSquare.G1) | BoardSquare.H3; PawnBitBoard move10 = new PawnBitBoard(ChessPieceColors.Black); move10.Bits = (move8.Bits ^ BoardSquare.G2) | BoardSquare.G1; move10.Promote(PawnBitBoard.PromotionPiece.Queen); testBoard.Update(move1); testBoard.Update(move2); testBoard.Update(move3); testBoard.Update(move4); testBoard.Update(move5); testBoard.Update(move6); testBoard.Update(move7); testBoard.Update(move8); testBoard.Update(move9); ulong expectedKey = testBoard.BoardHash.Key; testBoard.Update(move10); testBoard.Undo(); ulong testHash = testBoard.BoardHash.Key; Assert.Equal(expectedKey, testHash); }
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 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); }