public static card[] leftovers(card[] hand) { var rv = new card[52-hand.Length]; int idx =0; for (int i = 0; i < 52; i++) { if(!hand.Any<card>(x => i == x.id)) rv[idx++] = new card(i); } return rv; }
public hand(card[] cards) { this.cards = cards; }
public static int[][] discardDist(card[] hand) { var cntA = new int[15][]; int cnti = 0; var cards = new card[4]; var rem = leftovers(hand); for (int i = 0; i < hand.Length; i++) // first discard for (int j = i+1; j < hand.Length; j++, cnti++) // second discard { cntA[cnti] = new int[30]; int l = 0; for (int k = 0; k < hand.Length; k++) { if (k == i || k == j) continue; cards[l++] = hand[k]; } var h = new hand(cards); for (int k = 0; k < rem.Length; k++) { int score = CribbageHand.score(h, rem[k], false); cntA[cnti][score]++; } } return cntA; }
public static int score(hand h, card turn, bool crib) { int score = 0; h.cards.CopyTo(cA, 0); cA[4] = turn; Array.Sort<card>(cA, (x, y) => { if (x.face != y.face) return x.face-y.face; return x.suit-y.suit; } ); for (int i = 0; i < h.Count; i++) { bool flush = (i == 0); if (h[i].face == 11 && h[i].suit == turn.suit) score++; // his nibs if (h[i].face == turn.face) score += 2; // pair for (int j = i+1; j < h.Count; j++) { if (h[i].face == h[j].face) score += 2; // pair // flushes if (flush && h[i].suit != h[j].suit) flush = false; } if (flush) if (h[i].suit == turn.suit) score += 5; else if (!crib) score += 4; } // Straights - count the runs&repeats at the same time. int last = cA[0].face; int repeats = 1; int combos = 1; int run = 1; for (int i = 1; i < cA.Length; i++) { if (cA[i].face == last) { repeats++; } else if (cA[i].face == last+1) { combos *= repeats; repeats=1; run++; } else if (run >= 3) { break; } else { repeats = 1; combos = 1; run = 1; } last = cA[i].face; } if (run >= 3) score += run*combos; // 15 counts - use the bits of an int to generate all the combos for (int i = 1; i <= 0x1F; i++) { int sum = 0; int tmp = i; int j = 0; while (tmp > 0) { if ((tmp & 0x1) == 0x1) sum += cA[j].value; tmp >>= 1; j++; } if (sum == 15) score += 2; } return score; }