Esempio n. 1
0
        public void TestBlankBoard()
        {
            var board = new Board();
            var fen   = Board.Fen(false, board);

            Assert.AreEqual("8/8/8/8/8/8/8/8 w - - 0 0", fen);
        }
Esempio n. 2
0
        public void TestStandardBoardFullFEN()
        {
            var standardFen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
            var board       = new Board(standardFen);
            var fen         = Board.Fen(false, board);

            Assert.AreEqual(standardFen, fen);
        }
Esempio n. 3
0
        public void TestStandardBoardShortFEN()
        {
            var standardFen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w - -";
            var board       = new Board(standardFen);
            var fen         = Board.Fen(true, board);

            Assert.AreEqual(standardFen, fen);
        }
Esempio n. 4
0
        private static bool FindPlayBookMove(ref MoveContent bestMove, Board chessBoard, IEnumerable <OpeningMove> openingBook)
        {
            //Get the Hash for the current Board;
            string boardFen = Board.Fen(true, chessBoard);

            //Check the Opening Move Book
            foreach (OpeningMove move in openingBook)
            {
                if (move.StartingFEN.Contains(boardFen))
                {
                    int index = 0;

                    bestMove = move.Moves[index];
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 5
0
        private static bool FindPlayBookMove(ref MoveContent bestMove, Board chessBoard, IEnumerable <OpeningMove> openingBook)
        {
            string str = Board.Fen(true, chessBoard);

            foreach (OpeningMove openingMove in openingBook)
            {
                if (openingMove.StartingFEN.Contains(str))
                {
                    int index = 0;
                    if (openingMove.Moves.Count > 1)
                    {
                        index = new Random(DateTime.Now.Second).Next(openingMove.Moves.Count - 1);
                    }
                    bestMove = openingMove.Moves[index];
                    return(true);
                }
            }
            return(false);
        }
Esempio n. 6
0
        internal static void SaveCurrentGameMove(Board currentBoard, Board previousBoard, ICollection <OpeningMove> gameBook, MoveContent bestMove)
        {
            try
            {
                var move = new OpeningMove();

                move.StartingFEN = Board.Fen(true, previousBoard);
                move.EndingFEN   = Board.Fen(true, currentBoard);
                move.Moves.Add(bestMove);

                gameBook.Add(move);

                foreach (OpeningMove move1 in gameBook)
                {
                    byte repeatedMoves = 0;

                    foreach (OpeningMove move2 in gameBook)
                    {
                        if (move1.EndingFEN == move2.EndingFEN)
                        {
                            repeatedMoves++;
                        }
                    }

                    if (previousBoard.RepeatedMove < repeatedMoves)
                    {
                        previousBoard.RepeatedMove = repeatedMoves;
                        currentBoard.RepeatedMove  = repeatedMoves;
                    }
                }
                if (currentBoard.RepeatedMove >= 3)
                {
                    currentBoard.StaleMate = true;
                }
            }
            catch (Exception)
            {
                return;
            }

            return;
        }
Esempio n. 7
0
        internal static MoveContent IterativeSearch(Board examineBoard, byte depth, ref int nodesSearched, ref int nodesQuiessence, ref string pvLine, ref byte plyDepthReached, ref byte rootMovesSearched, List <OpeningMove> currentGameBook)
        {
            List <Position> pvChild = new List <Position>();
            int             alpha   = -400000000;
            const int       beta    = 400000000;


            MoveContent bestMove = new MoveContent();

            //We are going to store our result boards here
            ResultBoards succ = GetSortValidMoves(examineBoard);

            rootMovesSearched = (byte)succ.Positions.Count;

            if (rootMovesSearched == 1)
            {
                //I only have one move
                return(succ.Positions[0].LastMove);
            }

            //Can I make an instant mate?
            foreach (Board pos in succ.Positions)
            {
                int value = -AlphaBeta(pos, 1, -beta, -alpha, ref nodesSearched, ref nodesQuiessence, ref pvChild, true);

                if (value >= 32767)
                {
                    return(pos.LastMove);
                }
            }

            int currentBoard = 0;

            alpha = -400000000;

            succ.Positions.Sort(Sort);

            depth--;

            plyDepthReached = ModifyDepth(depth, succ.Positions.Count);

            foreach (Board pos in succ.Positions)
            {
                currentBoard++;

                progress = (int)((currentBoard / (decimal)succ.Positions.Count) * 100);

                pvChild = new List <Position>();

                int value = -AlphaBeta(pos, depth, -beta, -alpha, ref nodesSearched, ref nodesQuiessence, ref pvChild, false);

                if (value >= 32767)
                {
                    return(pos.LastMove);
                }

                if (examineBoard.RepeatedMove == 2)
                {
                    string fen = Board.Fen(true, pos);

                    foreach (OpeningMove move in currentGameBook)
                    {
                        if (move.EndingFEN == fen)
                        {
                            value = 0;
                            break;
                        }
                    }
                }

                pos.Score = value;

                //If value is greater then alpha this is the best board
                if (value > alpha || alpha == -400000000)
                {
                    pvLine = pos.LastMove.ToString();

                    foreach (Position pvPos in pvChild)
                    {
                        pvLine += " " + pvPos.ToString();
                    }

                    alpha    = value;
                    bestMove = pos.LastMove;
                }
            }

            plyDepthReached++;
            progress = 100;

            return(bestMove);
        }
Esempio n. 8
0
        internal static MoveContent Execute(Board examineBoard, List <Board> positions, int depth, List <OpeningMove> currentGameBook)
        {
            List <Position> pvChild = new List <Position>();
            int             alpha   = -400000000;
            const int       beta    = 400000000;

            MoveContent bestMove = new MoveContent();

            int nodesSearched   = 0;
            int nodesQuiessence = 0;

            //Can I make an instant mate?
            foreach (Board pos in positions)
            {
                //TODO: Send each (or in packs of few i.e.) board to different process, and finish if any returned bigger value
                int value = -AlphaBeta(pos, 1, -beta, -alpha, ref nodesSearched, ref nodesQuiessence, ref pvChild, true);

                if (value >= 32767)
                {
                    pos.LastMove.Score = value;
                    return(pos.LastMove);
                }
            }

            int currentBoard = 0;

            alpha = -400000000;

            positions.Sort(Sort);

            depth--;

            int plyDepthReached = ModifyDepth((byte)depth, positions.Count());

            foreach (Board pos in positions)
            {
                currentBoard++;

                progress = (int)((currentBoard / (decimal)positions.Count) * 100);

                pvChild = new List <Position>();

                int value = -AlphaBeta(pos, (byte)depth, -beta, -alpha, ref nodesSearched, ref nodesQuiessence, ref pvChild, false);

                if (value >= 32767)
                {
                    pos.LastMove.Score = value;
                    return(pos.LastMove);
                }

                if (examineBoard.RepeatedMove == 2)
                {
                    string fen = Board.Fen(true, pos);

                    foreach (OpeningMove move in currentGameBook)
                    {
                        if (move.EndingFEN == fen)
                        {
                            value = 0;
                            break;
                        }
                    }
                }

                pos.Score = value;

                //If value is greater then alpha this is the best board
                if (value > alpha || alpha == -400000000)
                {
                    alpha          = value;
                    bestMove       = pos.LastMove;
                    bestMove.Score = value;
                }
            }

            plyDepthReached++;
            progress = 100;

            return(bestMove);
        }
Esempio n. 9
0
        internal static MoveContent IterativeSearchOld(Board examineBoard, ChessEngine.Engine.Engine.TimeSettings gameTimeSettings, ref int nodesSearched, ref int nodesQuiessence, ref string pvLine, BackgroundWorker worker, ref byte plyDepthReached, ref byte rootMovesSearched, List <OpeningMove> currentGameBook)
        {
            Zobrist.MarkAncient();
            MoveContent            moveContent1   = new MoveContent();
            MoveContent            moveContent2   = new MoveContent();
            string                 str1           = "";
            List <Search.Position> positionList1  = new List <Search.Position>();
            ResultBoards           sortValidMoves = Search.GetSortValidMoves(examineBoard);

            rootMovesSearched = (byte)sortValidMoves.Positions.Count;
            int  num1  = 30;
            int  num2  = 40;
            byte depth = 1;

            if (gameTimeSettings == ChessEngine.Engine.Engine.TimeSettings.Moves40In10Minutes)
            {
                num1 = 15;
            }
            else if (gameTimeSettings == ChessEngine.Engine.Engine.TimeSettings.Moves40In20Minutes)
            {
                num1 = 30;
            }
            else if (gameTimeSettings == ChessEngine.Engine.Engine.TimeSettings.Moves40In30Minutes)
            {
                num1 = 45;
            }
            else if (gameTimeSettings == ChessEngine.Engine.Engine.TimeSettings.Moves40In40Minutes)
            {
                num1 = 60;
            }
            else if (gameTimeSettings == ChessEngine.Engine.Engine.TimeSettings.Moves40In60Minutes)
            {
                num1 = 90;
            }
            else if (gameTimeSettings == ChessEngine.Engine.Engine.TimeSettings.Moves40In90Minutes)
            {
                num1 = 135;
            }
            DateTime now = DateTime.Now;

            do
            {
                pvLine = "";
                int num3 = -400000000;
                sortValidMoves.Positions.Sort(new Comparison <Board>(Search.Sort));
                foreach (Board position1 in sortValidMoves.Positions)
                {
                    if (DateTime.Now - now > TimeSpan.FromSeconds((double)num1))
                    {
                        pvLine = str1;
                        return(moveContent2);
                    }
                    if (worker != null)
                    {
                        worker.ReportProgress((int)((DateTime.Now - now).TotalSeconds / (double)num1 * 100.0));
                    }
                    List <Search.Position> pvLine1 = new List <Search.Position>();
                    int num4 = -Search.AlphaBeta(position1, depth, -400000000, -num3, ref nodesSearched, ref nodesQuiessence, ref pvLine1, true);
                    if (num4 >= (int)short.MaxValue)
                    {
                        pvLine = str1;
                        return(position1.LastMove);
                    }
                    if ((int)examineBoard.RepeatedMove == 2)
                    {
                        string str2 = Board.Fen(true, position1);
                        foreach (OpeningMove openingMove in currentGameBook)
                        {
                            if (openingMove.EndingFEN == str2)
                            {
                                num4 = 0;
                                break;
                            }
                        }
                    }
                    position1.Score = num4;
                    if (num4 > num3)
                    {
                        List <Search.Position> positionList2 = new List <Search.Position>();
                        pvLine = position1.LastMove.ToString();
                        foreach (Search.Position position2 in pvLine1)
                        {
                            pvLine = pvLine + " " + position2.ToString();
                            positionList2.Add(position2);
                        }
                        positionList2.Reverse();
                        num3         = num4;
                        moveContent1 = position1.LastMove;
                    }
                }
                moveContent2    = moveContent1;
                str1            = pvLine;
                plyDepthReached = depth;
                ++depth;
            }while (DateTime.Now - now < TimeSpan.FromSeconds((double)num1) && (int)plyDepthReached < 19);
            plyDepthReached = (byte)((uint)plyDepthReached + 1U);
            int num5 = num2 != 1 ? num2 - 1 : 40;

            return(moveContent2);
        }
Esempio n. 10
0
        internal static MoveContent IterativeSearch(Board examineBoard, byte depth, ref int nodesSearched, ref int nodesQuiessence, ref string pvLine, BackgroundWorker worker, ref byte plyDepthReached, ref byte rootMovesSearched, List <OpeningMove> currentGameBook)
        {
            List <Search.Position> pvLine1 = new List <Search.Position>();
            int num1 = -400000000;

            Zobrist.MarkAncient();
            MoveContent  moveContent    = new MoveContent();
            ResultBoards sortValidMoves = Search.GetSortValidMoves(examineBoard);

            rootMovesSearched = (byte)sortValidMoves.Positions.Count;
            if ((int)rootMovesSearched == 1)
            {
                return(sortValidMoves.Positions[0].LastMove);
            }
            foreach (Board position in sortValidMoves.Positions)
            {
                if (-Search.AlphaBeta(position, (byte)1, -400000000, -num1, ref nodesSearched, ref nodesQuiessence, ref pvLine1, true) >= (int)short.MaxValue)
                {
                    return(position.LastMove);
                }
            }
            int num2 = 0;
            int num3 = -400000000;

            sortValidMoves.Positions.Sort(new Comparison <Board>(Search.Sort));
            --depth;
            plyDepthReached = Search.ModifyDepth(depth, sortValidMoves.Positions.Count);
            foreach (Board position1 in sortValidMoves.Positions)
            {
                ++num2;
                if (worker != null)
                {
                    worker.ReportProgress((int)((Decimal)num2 / (Decimal)sortValidMoves.Positions.Count * new Decimal(100)));
                }
                List <Search.Position> pvLine2 = new List <Search.Position>();
                int num4 = -Search.AlphaBeta(position1, depth, -400000000, -num3, ref nodesSearched, ref nodesQuiessence, ref pvLine2, false);
                if (num4 >= (int)short.MaxValue)
                {
                    return(position1.LastMove);
                }
                if ((int)examineBoard.RepeatedMove == 2)
                {
                    string str = Board.Fen(true, position1);
                    foreach (OpeningMove openingMove in currentGameBook)
                    {
                        if (openingMove.EndingFEN == str)
                        {
                            num4 = 0;
                            break;
                        }
                    }
                }
                position1.Score = num4;
                if (num4 > num3 || num3 == -400000000)
                {
                    pvLine = position1.LastMove.ToString();
                    foreach (Search.Position position2 in pvLine2)
                    {
                        pvLine = pvLine + " " + position2.ToString();
                    }
                    num3        = num4;
                    moveContent = position1.LastMove;
                }
            }
            plyDepthReached = (byte)((uint)plyDepthReached + 1U);
            return(moveContent);
        }