Esempio n. 1
0
        public int SearchBestMove(byte depth, Dot player, float alpha, float beta)
        {
            int bestMove = 0;

            var moves      = MoveGenerator.GenerateMoves(player);
            Dot nextPlayer = player.NextPlayer();

            foreach (var move in moves)
            {
                if (alpha < beta)
                {
                    if (Field.MakeMove(move))
                    {
                        HashField.UpdateHash();
                        float tmp = -EvaluatePosition((byte)(depth - 1), nextPlayer, -beta, -alpha, HashField.Key,
                                                      MoveGenerator.GenerateMoves(nextPlayer, moves));
                        Field.UnmakeMove();
                        HashField.UpdateHash();
                        if (tmp > alpha)
                        {
                            alpha    = tmp;
                            bestMove = move;
                        }
                    }
                }
            }

            return(bestMove);
        }
Esempio n. 2
0
 public override int GetHashCode()
 {
     if (HashField != null)
     {
         return(HashField.GetHashCode());
     }
     return(_reference.GetHashCode());
 }
Esempio n. 3
0
        /// <summary>
        ///     Returns a hash code for this instance.
        /// </summary>
        /// <returns>
        ///     A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
        /// </returns>
        public override int GetHashCode( )
        {
            unchecked
            {
                int hash = 17;

                hash = hash * 92821 + Key.GetHashCode( );

                hash = hash * 92821 + HashField.GetHashCode( );

                return(hash);
            }
        }
Esempio n. 4
0
        private unsafe float EvaluatePosition(byte depth, Dot player, float alpha, float beta, ulong key, HashSet <int> moves)
        {
            float oldAlpha   = alpha;
            Dot   nextPlayer = player.NextPlayer();

            float score = CheckCollision(player, depth, alpha, beta, key, moves);

            if (score >= 0)
            {
                return(score);
            }

            if (depth == 0)
            {
                return(Estimator.Estimate(player));
            }

            foreach (var move in moves)
            {
                if (Field.MakeMove(move))
                {
                    HashField.UpdateHash();
                    float tmp = -EvaluatePosition((byte)(depth - 1), nextPlayer, -beta, -alpha, HashField.Key,
                                                  MoveGenerator.GenerateMoves(nextPlayer, moves));
                    Field.UnmakeMove();
                    HashField.UpdateHash();

                    if (tmp > alpha)
                    {
                        TranspositionTable.RecordHash(
                            (byte)depth, tmp, tmp < beta ? enmHashEntryType.Exact : enmHashEntryType.Beta, HashField.Key, (ushort)move);

                        alpha = tmp;
                        if (alpha >= beta)
                        {
                            return(beta);
                        }
                    }
                }
            }

            if (alpha == oldAlpha)
            {
                TranspositionTable.RecordHash((byte)depth, alpha, enmHashEntryType.Alpha, HashField.Key, 0);
            }

            return(alpha);
        }
Esempio n. 5
0
        void MakeHashField()
        {
            HashField = MakeTarget(w, h);

            Color[] _hash = new Color[w * h];

            var rnd = new System.Random(5902981);

            for (int i = 0; i < w; i++)
            {
                for (int j = 0; j < h; j++)
                {
                    _hash[i * h + j] = new Color(rnd.IntRange(0, 256), rnd.IntRange(0, 256), rnd.IntRange(0, 256), rnd.IntRange(0, 256));
                }
            }

            HashField.SetData(_hash);
        }
Esempio n. 6
0
 public override int GetHashCode()
 {
     return(HashField == null?base.GetHashCode() : HashField.GetHashCode());
 }
Esempio n. 7
0
        private unsafe float CheckCollision(Dot player, byte depth, float alpha, float beta, ulong key, HashSet <int> moves)
        {
            fixed(HashEntry *hashEntry = &TranspositionTable.HashEntries_[key % AiSettings.HashTableSize])
            {
                if (hashEntry->HashKey == key)
                {
                    if (hashEntry->Depth >= depth)
                    {
                        float score = hashEntry->Score;

                        if (hashEntry->Type == enmHashEntryType.Alpha)
                        {
                            if (score <= alpha)
                            {
                                return(alpha);
                            }

                            /*if (score < beta)
                             *      beta = score;
                             * if (beta <= alpha)
                             *      return alpha;*/
                        }
                        else
                        {
                            if (score > alpha)
                            {
                                alpha = score;
                            }
                            if (alpha >= beta)
                            {
                                return(beta);
                            }
                        }
                    }
                    if (hashEntry->Type != enmHashEntryType.Alpha && depth != 0)
                    {
                        if (Field.MakeMove(hashEntry->BestMove))
                        {
                            HashField.UpdateHash();
                            float tmp = -EvaluatePosition((byte)(depth - 1), player.NextPlayer(), -beta, -alpha, HashField.Key,
                                                          MoveGenerator.GenerateMoves(player.NextPlayer(), moves));
                            Field.UnmakeMove();
                            HashField.UpdateHash();

                            if (tmp > alpha)
                            {
                                TranspositionTable.RecordHash(depth, tmp,
                                                              tmp < beta ? enmHashEntryType.Exact : enmHashEntryType.Beta,
                                                              key, hashEntry->BestMove);
                                alpha = tmp;
                                if (alpha >= beta)
                                {
                                    return(beta);
                                }
                            }
                        }
                    }
                }
            }

            return(-1);
        }