/*  vert.c

    Copyright (C) 1994  Lambert Klasen & Detlef Steuer
                        klasen@asterix.uni-muenster.de
                        steuer@amadeus.statistik.uni-dortmund.de

    This file is free source code; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
    COPYING for more details.
 */
#include<stdio.h>
#include<stdlib.h>

float E_Werte[54264];
long int NaufM[16][7];
long int Binomial[7][7];

long int fak(int n)
{
if (n<=1) return(1);
else return( n* fak(n-1));
}

long int binomial(int n, int m)
{
return(fak(n)/fak(m)/fak(n-m));
}


long int naufm(int n,int m)
{
if (n<m) return(0);
if (m==1) return(1);
return( naufm(n-1,m)+naufm(n-1,m-1));
}

void set_naufm()
{
 int i,j;

for (i=1;i<16;i++)
{
for (j=1;j<7;j++)
{
NaufM[i][j]=naufm(i,j);
}
}
}

void set_binom()
{
int i,j;
for (i=1;i<7;i++)
{
for (j=1;j<7;j++)
{
Binomial[i][j]=binomial(i,j);
}
}
}


long int rel_Stellungsnummer(int startpin, int p1,int p2,int p3, int p4, int p5,int p6)
{
int pin[7]={ 0,p1,p2,p3,p4,p5,p6};
int summe= 0,summanden,steine,start;
int i,nextpin;
long int nummer=0L;
for( i=startpin+1;i<7;i++)
{
 summe+=pin[i];
}
if (summe==0) return(0);

i=startpin+1;
while (pin[i]==0 && i<7)
{
 i++;
}
nextpin=i-startpin;
nummer+=nextpin;
for (start=startpin; start< startpin+nextpin; start++)
{
 for (steine =1; steine < summe; steine++)
 {
  for (summanden=1; summanden< 6-start+1;summanden++)
  {
   nummer+=Binomial[6-start][summanden]*NaufM[steine][summanden];

}
}
}

    nummer+=rel_Stellungsnummer(startpin+nextpin,p1,p2,p3,p4,p5,p6);

return(nummer);

}


long calc(int anz,int w1,int w2,int w3,int w4,int p1,int p2,int p3,int p4,int p5,int p6)
{
 int pin[7]={0,p1,p2,p3,p4,p5,p6};
 int wurf[5]={0,w1,w2,w3,w4},rest[5];
 double minewert=100.0,tmpwert;
 int wurfend,maxpin=0,i,feld,j,k;
 int start,end,wf;
 long tmpnummer,minnummer=0L;
if (p1+p2+p3+p4+p5+p6==0) return(minnummer);
if (anz==0) return((long) rel_Stellungsnummer(0,p1,p2,p3,p4,p5,p6) ); 


for (i=1;i<7;i++)
{
 if (pin[i]>0) maxpin=i;
}

wurfend=anz;
if (anz>2) wurfend=1;
if (anz==2) 
{
 if (wurf[1]==wurf[2]) wurfend=1;
 else wurfend=2;
}

for (j=1;j<wurfend+1;j++)
{

 if (wurf[j]<maxpin){ start=wurf[j];end=maxpin;}
 else {start=end=maxpin;}

 for (feld=end;feld>start-1;feld--)
 {
  if (pin[feld] > 0) 
  {
   pin[feld]--;
   if (feld>wurf[j]) pin[feld-wurf[j]]++;
   for(k=1;k<5;k++)
   {
    if (k<j) rest[k]=wurf[k];
    if (k>j) rest[k-1]=wurf[k];
   }
   rest[4]=0;
   tmpnummer=calc(anz-1,rest[1],rest[2],rest[3],rest[4],
                    pin[1],pin[2],pin[3],pin[4],pin[5],pin[6]);
   tmpwert=E_Werte[tmpnummer];
   if (tmpwert< minewert) {minewert=tmpwert;minnummer=tmpnummer;}
   pin[feld]++;                  
   if (feld>wurf[j]) pin[feld-wurf[j]]--;
  }
 }

}
return(minnummer);
}

void set_V_Werte()
{
int pins[7]={15,0,0,0,0,0,0};
long int zugnummer=0L,minstell;
float aktv[30],altv[30],schnitt=0.0;
FILE *fid;

int z1,z2,i,tmp,maxpin=0;


fid=fopen("xgammon.db","w+b");

do
{
if (maxpin<6) {
 pins[maxpin++]--;
 pins[maxpin]++;
 }
else {
i=6;
while (pins[i  - 1]==0) i--;
tmp=pins[6];
pins[6]=0;
pins[i]=1+tmp;
pins[i-1]--;
maxpin=i;
 }
zugnummer++;	
for(i=0;i<30;i++)
{
 aktv[i]=0.0;
}

for (z1=1;z1<7;z1++)
{
for (z2=z1;z2<7;z2++)
{
 if (z1==z2)
 { 
  minstell= 
  calc(4,z1,z1,z1,z1,pins[1],pins[2],pins[3],pins[4],pins[5],pins[6]);
  if (minstell==0) aktv[0]+=1.0/36.0;
  else
  {
   fseek(fid, (minstell-1)*120,0);
   fread(altv,4,30,fid);
   for(i=0;i<29;i++) aktv[i+1]+= altv[i]/36.0;
  }
 } 
 else
 { 
  minstell= 
  calc(2,z1,z2,0,0,pins[1],pins[2],pins[3],pins[4],pins[5],pins[6]); 
  if (minstell==0) aktv[0]+=1.0/18.0;
  else
  {
   fseek(fid, (minstell-1)*120,0);
   fread(altv,4,30,fid);
   for(i=0;i<29;i++){ aktv[i+1]+= altv[i]/18.0;
   }
     }
 }
}
}

schnitt=0.0;

fseek(fid,0,2);
fwrite(aktv,4,30,fid);


} while (pins[6]!=15);
close(fid);
}

void main()
{
long int a,b;

set_binom();
set_naufm();
set_V_Werte();
}
