public virtual IEnumerable <string> GetMinMax(IKnuthRoundStateDto dto)
        {
            var score = new Dictionary <string, int>();

            foreach (var possibleKey in dto.PossibleKeys)
            {
                var scoreCount = new Dictionary <string, int>();
                foreach (var keyLeft in dto.KeysLeft)
                {
                    var checkValue = CheckAnswer(possibleKey, keyLeft);
                    var check      = $"{checkValue.WhitePoints}.{checkValue.BlackPoints}";
                    if (scoreCount.Keys.Contains(check))
                    {
                        var count = scoreCount[check];
                        scoreCount[check] = count + 1;
                    }
                    else
                    {
                        scoreCount[check] = 1;
                    }
                }
                var max = scoreCount.Values.Max();
                score[possibleKey] = max;
            }
            var min    = score.Values.Min();
            var result = score.Keys
                         .Where(k => score[k] == min)
                         .OrderBy(k => k);

            return(result);
        }
Exemplo n.º 2
0
        // todo unit test me
        public override IEnumerable <string> GetMinMax(IKnuthRoundStateDto dto)
        {
            var score = new  ConcurrentDictionary <string, int>();

            Parallel.ForEach(dto.PossibleKeys, possibleKey => { // TODO split to threads instead
                var scoreCount = new Dictionary <string, int>();
                foreach (var keyLeft in dto.KeysLeft)
                {
                    var checkValue = CheckAnswer(possibleKey, keyLeft);
                    var check      = $"{checkValue.WhitePoints}.{checkValue.BlackPoints}";
                    if (scoreCount.Keys.Contains(check))
                    {
                        var count         = scoreCount[check];
                        scoreCount[check] = count + 1;
                    }
                    else
                    {
                        scoreCount[check] = 1;
                    }
                }
                var max            = scoreCount.Values.Max();
                score[possibleKey] = max;
            });

            var min    = score.Values.Min();
            var result = score.Keys
                         .Where(k => score[k] == min)
                         .OrderBy(k => k);

            return(result);
        }
 protected virtual string GetNextGuess(IKnuthRoundStateDto dto, IEnumerable <string> maxScores)
 {
     foreach (var maxScoredCode in maxScores)
     {
         if (dto.KeysLeft.Contains(maxScoredCode))
         {
             return(maxScoredCode);
         }
     }
     foreach (var maxScoredCode in maxScores)
     {
         if (dto.PossibleKeys.Contains(maxScoredCode))
         {
             return(maxScoredCode);
         }
     }
     throw new InvalidOperationException("No minimax scores for possible keys!");
 }
Exemplo n.º 4
0
        protected override string GetNextGuess(IKnuthRoundStateDto dto, IEnumerable <string> maxScores)
        {
            var maxScoresLeft = maxScores
                                .Where(maxScoredCode => dto.KeysLeft.Contains(maxScoredCode))
                                .ToList();
            var maxScoresPossible = maxScores
                                    .Where(maxScoredCode => dto.PossibleKeys.Contains(maxScoredCode))
                                    .ToList();

            if (maxScoresLeft.Any())
            {
                return(GetRandomKeyGuess(maxScoresLeft));
            }
            else if (maxScoresPossible.Any())
            {
                return(GetRandomKeyGuess(maxScoresPossible));
            }
            throw new InvalidOperationException("No minimax scores for possible keys!");
        }