#include #include #include "mm_help.h" void bw (char * g1, char * g2, int * b, int * w) { int i, c1[20], c2[20]; (*b) = 0; (*w) = 0; for (i=0; i<20; ++i) { c1[i] = c2[i] = 0; } for (i=0; g1[i]; ++i) { assert (g2[i]); if (g1[i] == g2[i]) { (*b)++; } else { c1[(int)(g1[i]-'A')]++; c2[(int)(g2[i]-'A')]++; } } assert (!g2[i]); for (i=0; i<20; ++i) { if (c1[i] < c2[i]) (*w) += c1[i]; else (*w) += c2[i]; } } struct guess * get_guesses (int col, int len, int unique) { struct guess * ret, * last; int used[20]; int pos = 1; int i, j, m; assert (col < 20 && len < 20); ret = last = NULL; if (!unique) { for (i=1; i<=len; ++i) { pos *= col; if (pos > 1100000) return NULL; } ret = last = NULL; for (i=0; inext = NULL; g->g[len] = 0; val = i; for (j=len-1; j>= 0; j--) { g->g[j] = 'A' + val%col; val /= col; } if (!last) { ret = last = g; } else { last->next = g; last = g; } } return ret; } /* UNIQUE */ assert (col >= len); pos = 1; m = col; for (i=0; i 1100000) return NULL; } ret = malloc (sizeof (struct guess)); last = ret; ret->next = NULL; for (i=0; ig[i] = 'A' + i; used[i] = 1; } while (1) { int max = -1; struct guess * gn; gn = malloc (sizeof (struct guess)); for (i=0; ig[i] = last->g[i]; } gn->next = NULL; i = col-1; while (max < 0 && i >= 0) { if (!used[i]) { max = i; break; } i--; } i = len-1; while (i>= 0 && gn->g[i]-'A' >= max) { assert (used[gn->g[i]-'A']); used[gn->g[i]-'A'] = 0; max = gn->g[i]-'A'; i--; } if (i < 0) { free (gn); break; } assert (max >gn->g[i]-'A'); assert (used[gn->g[i]-'A']); used[gn->g[i]-'A'] = 0; gn->g[i]++; while (used[gn->g[i]-'A']) gn->g[i]++; used[gn->g[i]-'A'] = 1; j=0; for (i++; ig[i] = 'A'+j; used[j] = 1; } last->next = gn; last = gn; } return ret; }