public static void BFS(this Bitmap img, int x, int y, PointCallback callback) { //DFS会爆栈 var q = new Queue <Point>(); var Visited = new HashSet <Point>(); q.Enqueue(new Point(x, y)); while (q.Count > 0) { var NowPoint = q.Dequeue(); callback(NowPoint); int NowX = NowPoint.X, NowY = NowPoint.Y; TryVisit(NowX - 1, NowY); TryVisit(NowX, NowY - 1); TryVisit(NowX + 1, NowY); TryVisit(NowX, NowY + 1); bool _CanVisit(int px, int py) { bool IsVisited = Visited.Contains(new Point(px, py)); bool IsColorOk = ColorDiff(img.GetPixel(NowX, NowY), img.GetPixel(px, py)) <= 15; bool IsInRange = px > 0 && px < img.Width && py > 0 && py < img.Height; ; return(!IsVisited && IsInRange && IsColorOk); } void TryVisit(int px, int py) { if (_CanVisit(px, py)) { q.Enqueue(new Point(px, py)); Visited.Add(new Point(px, py)); } } } }
public static void SamplePlayedCardsScore(int pointsShown, int cardsPlayed, bool suitesMatch, PointCallback callback, int sampleCount = 256) { int cardsToPlay = 3 - cardsPlayed; Deck testDeck = new Deck(null); for (int i = 0; i < sampleCount; ++i) { testDeck.Shuffle(); Card[] restOfHand = testDeck.Deal(5 - cardsPlayed).ToArray(); System.Array.Sort(restOfHand, (a, b) => b.PointValue() - a.PointValue()); int rawPoints = restOfHand.Take(cardsToPlay).Select(card => card.PointValue()).Sum(); int tripleWinstonPoints = 0; if (suitesMatch) { var tripleWinstonHand = restOfHand.Where(card => card.suite == Suite.Skulls).Take(cardsToPlay); tripleWinstonPoints = tripleWinstonHand.Select(card => card.PointValue()).Sum() + restOfHand.Where(card => !tripleWinstonHand.Contains(card)).Take(1).Select(card => card.PointValue()).Sum(); } testDeck.Discard(restOfHand); callback(Mathf.Max(rawPoints, tripleWinstonPoints) + pointsShown); } }