private IEnumerable <Tuple <Position, double> > GetAllMoveAndScore(IBoard board, PieceType player)
 {
     foreach (var move in this.positions.GetEmptyPositions(board))
     {
         board.Set(move, player);
         double score = scorer.GetScore(board, player);
         board.Set(move, PieceType.Empty);
         yield return(new Tuple <Position, double>(move, score));
     }
 }
Example #2
0
        public string AutoComplete(string q, double?latitudeNullable, double?longitudeNullable, int maxResponseCount)
        {
            if (q == null)
            {
                return("q not specified");
            }
            if (latitudeNullable.HasValue && (latitudeNullable.Value < -90 || latitudeNullable.Value > 90))
            {
                return("latitude should be between -90 and 90");
            }
            if (longitudeNullable.HasValue && (longitudeNullable.Value < -180 || longitudeNullable.Value > 180))
            {
                return("longitude should be between -180 and 180");
            }

            IEnumerable <City> cities      = _cityStorage.GetCitiesStartsWith(q);
            List <Suggestion>  suggestions = new List <Suggestion>();

            foreach (City city in cities)
            {
                double score = _scorer.GetScore(city, q, latitudeNullable, longitudeNullable);
                suggestions.Add(new Suggestion
                {
                    Name      = city.FullName,
                    Latitude  = city.Latitude.ToString(),
                    Longitude = city.Longitude.ToString(),
                    Score     = score
                });
            }
            suggestions.Sort((s1, s2) => { return(s2.Score.CompareTo(s1.Score)); }); // sorting from highest to lowest
            if (maxResponseCount < suggestions.Count)
            {
                suggestions.RemoveRange(maxResponseCount, suggestions.Count - maxResponseCount);
            }

            return(_serializer.Serialize(suggestions));
        }
Example #3
0
 public string GetScore()
 {
     return(scorer.GetScore());
 }
Example #4
0
        private AiSearchResult Search(bool isMaxSearch,
                                      IBoard board,
                                      PieceType curPlayer,
                                      int depth,
                                      double minPossibleScore,
                                      double maxPossibleScore)
        {
            if (depth >= maxDepth || board.IsFull() || HasWinner(board))
            {
                this.leafCount++;

                // Return the score of current board.
                double score = scorer.GetScore(board, player);
                return(new AiSearchResult(score));
            }

            PieceType otherPlayer = curPlayer.GetOther();

            double         worseScore = isMaxSearch ? minPossibleScore : maxPossibleScore;
            AiSearchResult bestMove   = new AiSearchResult(worseScore);

            foreach (Position move in this.moveEnumerator.GetMoves(board, curPlayer))
            {
                // Make a move.
                board.Set(move, curPlayer);
                // Search deeper moves.
                AiSearchResult result = Search(!isMaxSearch, board, otherPlayer, depth + 1, minPossibleScore, maxPossibleScore);
                // Undo the move.
                board.Set(move, PieceType.Empty);

                double score = result.Score;

                if (isMaxSearch)
                {
                    if (score > bestMove.Score)
                    {
                        result.Moves.Push(new PlayerAndMove(curPlayer, move));
                        bestMove = result;

                        if (score >= maxPossibleScore)
                        {
                            // Result is better than max possible score, so this whole max search branch is meaningless.
                            // Just stop searching and return meaningless result.
                            break;
                        }

                        minPossibleScore = score;
                    }
                }
                else
                {
                    if (score < bestMove.Score)
                    {
                        result.Moves.Push(new PlayerAndMove(curPlayer, move));
                        bestMove = result;

                        if (score <= minPossibleScore)
                        {
                            // Result is worse than min possible score, so this whole min search branch is meaningless.
                            // Just stop searching and return meaningless result.
                            break;
                        }

                        maxPossibleScore = score;
                    }
                }
            }

            return(bestMove);
        }