示例#1
0
 public ChessPosition()
 {
     initChessEval();
     Board = new SquareInfo[64];
     emptyBoard = new SquareInfo[64];
     Princ = new ChessMove[maxDepth, maxDepth];
     killers = new ChessMove[2, 1000];
     mvvLva = new int[8, 8];
     searchHistory = new int[16, 64];
     // Init the mvvLva array
     for (int v = 0; v <= 6; v++)
         for (int a = 0; a <= 6; a++)
             mvvLva[v, a] = v * 100 +6 - a;
     // Init the killers array
     for (int i = 0; i < 1000; i++)
     {
         killers[0, i] = new ChessMove(new Square(99, 99), new Square(99, 99));
         killers[1, i] = new ChessMove(new Square(99, 99), new Square(99, 99));
     }
     // Init the emptyBoard and Board arrays
     for (int i = 0; i <= 63; i++)
     {
         emptyBoard[i] = new SquareInfo(Pieces.None, new Square(i));
         Board[i] = emptyBoard[i];
     }
     // Init the hashKeys
     Random rng = new Random();
     pieceKeys = new UInt64[16, 64];
     for (int p = 0; p <= 15; p++)
         for (int sq = 0; sq <= 63; sq++)
             pieceKeys[p, sq] = (UInt64)rng.Next() + ((UInt64)rng.Next() << 15) + ((UInt64)rng.Next() << 30) + ((UInt64)rng.Next() << 45) + ((UInt64)rng.Next() << 60);
     castleKeys = new UInt64[16];
     for (int i = 0; i <= 15; i++)
         castleKeys[i] = (UInt64)rng.Next() + ((UInt64)rng.Next() << 15) + ((UInt64)rng.Next() << 30) + ((UInt64)rng.Next() << 45) + ((UInt64)rng.Next() << 60);
     sideKey = (UInt64)rng.Next() + ((UInt64)rng.Next() << 15) + ((UInt64)rng.Next() << 30) + ((UInt64)rng.Next() << 45) + ((UInt64)rng.Next() << 60);
     // Init the pvTable
     pvTable = new Dictionary<ulong, int>();
     EpSquare = new Square(0, 0);
     white = new ToMoveData(PieceColour.White, new Piece(PieceType.Pawn, PieceColour.White), new Piece(PieceType.Knight, PieceColour.White),
         new Piece(PieceType.Bishop, PieceColour.White), new Piece(PieceType.Rook, PieceColour.White), new Piece(PieceType.Queen, PieceColour.White),
         new Piece(PieceType.King, PieceColour.White), 1, 7, 1, CastleFlags.Wks, CastleFlags.Wqs, new Square("e1"), new Square("h1"), new Square("a1"));
     black = new ToMoveData(PieceColour.Black, new Piece(PieceType.Pawn, PieceColour.Black), new Piece(PieceType.Knight, PieceColour.Black),
         new Piece(PieceType.Bishop, PieceColour.Black), new Piece(PieceType.Rook, PieceColour.Black), new Piece(PieceType.Queen, PieceColour.Black),
         new Piece(PieceType.King, PieceColour.Black), 6, 0, -1, CastleFlags.Bks, CastleFlags.Bqs, new Square("e8"), new Square("h8"), new Square("a8"));
     PossibleMoves = new Collection<ChessMove>();
     MoveHistory = new Collection<ChessMove>();
     undoStack = new Stack<ChessMove>();
     NewGame();
 }
示例#2
0
 public bool Initialise(string strFen)
 {
     // returns false if position is illegal
     // No validation checks yet
     for (int i = 0; i <= 63; i++)
         Board[i] = emptyBoard[i];
     MoveHistory.Clear();
     undoStack.Clear();
     Square sq = new Square("a8");
     int p = 0;  // Position within the FEN string
     char c;
     while (p < strFen.Length && (c = strFen[p++]) != ' ')
     {
         if (c >= '1' && c <= '8')
         {
             sq.File += c - '0';
         }
         int i = pieceStr.IndexOf(Char.ToUpper(c));
         if (i >= 1)
         {
             // Replace the empty SquareInfos with new ones for the pieces
             // The original empty SquareInfos are in emptyBoard
             Board[sq] = new SquareInfo(new Piece((PieceType)i, c < 'a' ? PieceColour.White : PieceColour.Black), sq);
             sq.File++;
         }
         if (c == '/')
         {
             sq.Rank--;
             sq.File = 0;
         }
     }
     c = strFen[p++];
     if (c == 'b')
     {
         ToMove = PieceColour.Black;
     }
     else
     {
         ToMove = PieceColour.White;
     }
     CastleFlags = 0;
     p++;
     while (p < strFen.Length && (c = strFen[p++]) != ' ')
     {
         switch (c)
         {
             case 'K': CastleFlags |= CastleFlags.Wks; break;
             case 'Q': CastleFlags |= CastleFlags.Wqs; break;
             case 'k': CastleFlags |= CastleFlags.Bks; break;
             case 'q': CastleFlags |= CastleFlags.Bqs; break;
         }
     }
     c = strFen[p++];
     if (c == '-')
         EpSquare = new Square(0, 0);
     else
     {
         char d = strFen[p++];
         EpSquare = new Square(c - 'a', d - '1');
     }
     StringBuilder n = new StringBuilder();
     p++;
     while (p < strFen.Length && (c = strFen[p++]) != ' ')
         n.Append(c);
     HalfMovesSinceLastPawnMoveOrCapture = int.Parse(n.ToString());
     n.Clear();
     while (p < strFen.Length && (c = strFen[p++]) != ' ')
         n.Append(c);
     GameStartPlyNr = (int.Parse(n.ToString()) - 1) * 2 + (ToMove == PieceColour.Black ? 1 : 0);
     initializePieceLists();
     generatePossibleMoves(PossibleMoves);
     removeIllegalMoves(PossibleMoves);
     return true;
 }