コード例 #1
0
ファイル: HashKeyTests.cs プロジェクト: Tearth/Cosette
        public void HashKey_FromInitialPosition(string[] moves, ulong expectedHashKey)
        {
            var polyglotBoard = new PolyglotBoard();

            polyglotBoard.InitDefaultState();

            foreach (var move in moves)
            {
                polyglotBoard.MakeMove(move);
            }

            Assert.Equal(expectedHashKey, polyglotBoard.CalculateHash());
        }
コード例 #2
0
        public List <string> GetRandomOpening(int movesCount)
        {
            var movesList     = new List <PolyglotBookEntry>();
            var polyglotBoard = new PolyglotBoard();

            polyglotBoard.InitDefaultState();

            for (var moveIndex = 0; moveIndex < movesCount; moveIndex++)
            {
                var availableMoves = GetBookEntries(polyglotBoard.CalculateHash());
                if (availableMoves.Count == 0)
                {
                    break;
                }

                availableMoves = availableMoves.OrderBy(p => p.Weight).ToList();
                var weightSum = availableMoves.Sum(p => p.Weight);

                var probabilityArray = new double[availableMoves.Count];
                for (var availableMoveIndex = 0; availableMoveIndex < availableMoves.Count; availableMoveIndex++)
                {
                    probabilityArray[availableMoveIndex] = (double)availableMoves[availableMoveIndex].Weight / weightSum;
                }

                var randomValue = _random.NextDouble();
                for (var availableMoveIndex = 0; availableMoveIndex < availableMoves.Count; availableMoveIndex++)
                {
                    if (probabilityArray[availableMoveIndex] > randomValue || availableMoveIndex == availableMoves.Count - 1)
                    {
                        movesList.Add(availableMoves[availableMoveIndex]);
                        polyglotBoard.MakeMove(availableMoves[availableMoveIndex].Move.ToString());
                        break;
                    }
                }
            }

            if (movesList.Count % 2 != 0)
            {
                movesList.Remove(movesList.Last());
            }

            return(movesList.Select(p => p.Move.ToString()).ToList());
        }