public void ResultFromPreviousGuess(int numberOfCorrectsPegs, int numberOfPegsAtWrongPosition) { // filter out all lines that does not give the same result as the previous guess _PosibleSolutions = _PosibleSolutions.Where(l => { var r = _LineComparer.Compare(_Guess, l); return(r.NumberOfCorrectPegs == numberOfCorrectsPegs && r.NumberOfPegsAtWrongPosition == numberOfPegsAtWrongPosition); }).ToList(); }
public void GiveTheExpectedResult(int guessPeg1, int guessPeg2, int secretPeg1, int secretPeg2, int expectedCorrect, int expectedWrongPosition) { var lineComparer = new LineComparer(); var guess = new int[] { guessPeg1, guessPeg2 }; var secret = new int[] { secretPeg1, secretPeg2 }; var actualResult = lineComparer.Compare(guess, secret); Assert.Equal(expectedCorrect, actualResult.NumberOfCorrectPegs); Assert.Equal(expectedWrongPosition, actualResult.NumberOfPegsAtWrongPosition); }
public static string Merge(IEnumerable <string> sortedFiles, long cacheSize) { var files = sortedFiles.ToArray(); if (files.Length == 1) { return(files[0]); } var resultFile = $"./{_mergeFolder}/{Path.GetRandomFileName()}"; if (File.Exists(resultFile)) { File.Delete(resultFile); } int fileCount = files.Length; var iterators = new BufferedFileIterator[fileCount]; for (var i = 0; i < fileCount; i++) { var iterator = new BufferedFileIterator(files[i], cacheSize); iterator.MoveNext(); iterators[i] = iterator; } var comaprer = new LineComparer(); var buffer = new List <string>(); var currentSize = 0; while (true) { IEnumerator <string> minIterator = null; foreach (var iterator in iterators) { if (iterator.Current != null) { if (minIterator == null || comaprer.Compare(minIterator.Current, iterator.Current) > 0) { minIterator = iterator; } } } if (minIterator == null) { break; } var minString = minIterator.Current; buffer.Add(minString); currentSize += minString.Length; minIterator.MoveNext(); if (currentSize > cacheSize) { File.AppendAllLines(resultFile, buffer); buffer.Clear(); currentSize = 0; } } File.AppendAllLines(resultFile, buffer); foreach (var i in iterators) { i.Dispose(); } return(resultFile); }
public int[] GetGuess() { int[] guess; if (!_UsedGuesses.Any()) { // 2. Start with initial guess 1122 guess = Enumerable.Range(0, _NumberOfPegsPerLine).Select(i => (i % _NumberOfDifferentPegs) / 2).ToArray(); } else { // 6. Apply minimax technique to find a next guess as follows: var guessesWithMaximumScore = new List <int[]>(); var maximumScore = 0; // For each possible guess, that is, any unused code of the 1296 not just those in S, var possibleGuesses = _AllLines.Except(_UsedGuesses); if (!possibleGuesses.Any()) { throw new InvalidOperationException("All guesses used"); } if (!_PosibleSolutions.Any()) { throw new InvalidOperationException("No posible solution"); } foreach (var possibleGuess in possibleGuesses) { // calculate how many possibilities in S would be eliminated for each possible colored/white peg score. // The score of a guess is the minimum number of possibilities it might eliminate from S. // A single pass through S for each unused code of the 1296 will provide a hit count for each colored/white peg score found; var hitCounts = new int[_NumberOfPegsPerLine + 1, _NumberOfPegsPerLine + 1]; foreach (var posibleSolution in _PosibleSolutions) { var result = _LineComparer.Compare(possibleGuess, posibleSolution); hitCounts[result.NumberOfCorrectPegs, result.NumberOfPegsAtWrongPosition]++; } // the colored/white peg score with the highest hit count will eliminate the fewest possibilities; var highestHitCount = 0; for (int i = 0; i <= _NumberOfPegsPerLine; i++) { for (int j = 0; j <= _NumberOfPegsPerLine; j++) { highestHitCount = Math.Max(highestHitCount, hitCounts[i, j]); } } // calculate the score of a guess by using "minimum eliminated" = "count of elements in S" - (minus) "highest hit count". var minimumEliminated = _PosibleSolutions.Count - highestHitCount; if (minimumEliminated > maximumScore) { guessesWithMaximumScore.Clear(); guessesWithMaximumScore.Add(possibleGuess); maximumScore = minimumEliminated; } else if (minimumEliminated == maximumScore) { guessesWithMaximumScore.Add(possibleGuess); } } // From the set of guesses with the maximum score, select one as the next guess, choosing a member of S whenever possible. guess = guessesWithMaximumScore.FirstOrDefault(l => _PosibleSolutions.Any(l2 => IsEqual(l, l2))) ?? guessesWithMaximumScore.First(); // 7. Repeat from step 3. } // 3. Play the guess to get a response of colored and white pegs. _UsedGuesses.Add(guess); return(guess); }