public void RandomPop_WithEmptySpan_Throws() { int[] array = new int[0]; Assert.Throws <IndexOutOfRangeException>( () => Spans.PopRandom(new Random(), new Span <int>(array))); }
public void RandomPop_WithOneValue_ReturnsValueAndLeavesUnchanged() { Span <int> span = stackalloc int[1]; span[0] = 5; Assert.Equal(5, Spans.PopRandom(new Random(), span)); Assert.Equal(5, span[0]); }
public void RandomPop_WithManyValues_ReturnsValueAndSwapsValueToEnd() { Span <int> span = stackalloc int[5] { 0, 1, 2, 3, 4 }; Assert.Equal(1, Spans.PopRandom(new Random(1234), span)); Assert.Equal(new int[5] { 0, 4, 2, 3, 1 }, span.ToArray()); }
private static bool _TrySolveRandomly(Guesser <TPuzzle> guesser, Random rand) { if (guesser.IsSolved) { return(true); } Span <Guess> guesses = stackalloc Guess[guesser.MaxGuessCount]; var guessCount = guesser.PopulateBestGuesses(guesses); while (guessCount > 0) { ref Guess guess = ref Spans.PopRandom(rand, guesses[0..guessCount--]);
private bool _TrySolveRandomly(SquareTracker <TPuzzle> tracker, Random random) { if (tracker.Puzzle.NumEmptySquares == 0) { return(true); } Coordinate c = tracker.GetBestCoordinateToGuess(); Span <int> possibleValues = stackalloc int[tracker.Puzzle.Size]; int numPossible = tracker.PopulatePossibleValues(in c, possibleValues); while (numPossible > 0) { int possibleValue = Spans.PopRandom(random, possibleValues[0..numPossible--]);