/* This program is by Graham Toal.

   The purpose of this program is, given a set of tiles (a rack
   leave) and the contents of the bag (unseen tiles), plus a
   particular word, calculate the probability of drawing the
   tiles which will allow you to make the word.  This will of
   course be a vanishingly small number, but in the context
   that it is used, many of these small numbers will be summed
   to generate a more reasonable number.

   This program will calculate the rack leave values for a
   single letter at the very start of play, using the entire
   TWL word list as the 'next possible play'.

 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "../spell/dawg.h"

static double totprob[256];
static int tot[256];
static int itot[27] =
  /*  a b c d  e f g h i j k l m n o p  q r s t u v w x y  z */
   {  9,2,2,4,12,2,3,2,9,1,1,4,2,6,8,2, 1,6,4,6,4,2,2,1,2, 1};

static int score[256];
static int iscore[27] = 
{ 1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10 };

static double target[256];
static double itarget[27] = 
{ 1.0, -2.0, -0.5, 0.5, 2.0, -2.0, -3.0, 1.5, -1.0, -2.0,
  0.0, 0.5, 0.5, 0.5, -0.5, -1.0, -9.0, 1.0, 8.0, 0.0, 
  -4.0, -6.0, -3.0, 2.5, -0.5, 5.0
};

#include "global_analysis/probfn.c"

#define FullRack       "aaaaaaaaabbccddddeeeeeeeeeeeeffggghhiiiiiiiiijkllllmmnnnnnnooooooooppqrrrrrrssssttttttuuuuvvwwxyyz??"
#define FullRackMinusA "aaaaaaaabbccddddeeeeeeeeeeeeffggghhiiiiiiiiijkllllmmnnnnnnooooooooppqrrrrrrssssttttttuuuuvvwwxyyz??"
#define HalfRack       "aaaaabcddeeeeeefghiiiiikllmnnnooooprrrsstttuuvwxy?"

int do_one(char *word, void *state)
{
  char saved[8];
  int i;
  double *totprob = (double *)state;

  if (strlen(word) != 7) return(TRUE);
  strcpy(saved, word);
  for (i = 0; i < 7; i++) {
    strcpy(word, saved);
    memmove(word+i, word+i+1, 6-i+1);
    totprob[saved[i]] += RackProb(/*modified*/word, FullRack, 6);
#ifdef COPIOUS_CRAP
    fprintf(stderr, "drawprob(%d, %s) + %c => %s = %1.6f\n", strlen(word), word, saved[i], saved, totprob[saved[i]]);
#endif
  }

  return(TRUE);
}

int main(int argc, char **argv)
{
  NODE *dawg;
  int nedges;
  char *s;
  char word[128];
  char saved[128];
  double p;
  int i;
  FILE *wordlist;

  for (i = 0; i < 256; i++) {
    tot[i] = 2;
    score[i] = 0;
    totprob[i] = 0.0;
    target[i] = 0.0;
  }
  for (i = 0; i < 26; i++) {
    tot['a'+i] = itot[i];
    score['a'+i] = iscore[i];
    target['a'+i] = itarget[i];
  }

  if (!dawg_init("SOWPODS", &dawg, &nedges)) {
    fprintf(stderr, "No!\n");
    exit(1);
  }
  dawg_forall(dawg, 0L, do_one, totprob);

  fprintf(stdout, "? Prob   Adjusted  Watkins  Watkins-Score\n");
  for (i = 'a'; i <= 'z'; i++) {
    double f, g, h;
#define SIGN(s) (s < 0.0 ? " " : "  ")
    f=totprob[i];
    g=f-0.0975;
    h=g*fabs(g);
    fprintf(stdout, "%c=%3.6f, %s%3.1f, %s%3.1f, %s%3.1f\n",
      toupper(i),
      totprob[i],
      SIGN(g), floor(g*80.0)/2.0,
      SIGN(target[i]), target[i],
      SIGN(target[i]-score[i]), target[i]-score[i]
    );
  }
  fflush(stdout); fflush(stderr);
  return(0);
  exit(0);
}