File: cribbage.5c

package info (click to toggle)
nickle 2.77-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,612 kB
  • ctags: 3,746
  • sloc: ansic: 26,986; yacc: 1,873; sh: 954; lex: 884; makefile: 225
file content (44 lines) | stat: -rw-r--r-- 912 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
 * Cribbage scoring aid
 * Counts only runs and 15s
 *
 * Copyright © 1997 Bart Massey.
 * All Rights Reserved.  See the file COPYING in this directory
 * for licensing information.
 */

namespace Cribbage {

  int countsum(int c, int[*] v, int n) {
    if (c < 0)
      return 0;
    int t = 0;
    for (int i = 0; i < n; i++)
      t += v[i];
    if (t == c)
      return 1;
    if (t < c)
      return 0;
    return countsum(c, v, n - 1) + countsum(c - v[n - 1], v, n - 1);
  }

  int countpairs(int[*] v, int n) {
    if (n < 2)
      return 0;
    int c = 0;
    for (int i = 1; i < n; i++)
	if (v[i] == v[0])
	  c++;
    int[n - c - 1] w;
    int j = 0;
    for (int i = 1; i < n; i++)
      if (v[i] != v[0])
	w[j++] = v[i];
    return c * (c + 1) // 2 + countpairs(w, n - c - 1);
  }

  public int score(int[*] v) {
    int n = dim(v);
    return 2 * countsum(15, v, n) + 2 * countpairs(v, n);
  }
}