File: bcreate.c

package info (click to toggle)
phalanx 12-3
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 664 kB
  • ctags: 504
  • sloc: ansic: 6,094; makefile: 76; sh: 50
file content (268 lines) | stat: -rw-r--r-- 5,992 bytes parent folder | download
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include "phalanx.h"

#define WIN 5
#define DRA 3
#define LOO 1

unsigned Asize=800000;
short Maxbookply=70;
short Width=1000;
short Maxdel=0;

typedef struct
{
	unsigned hashboard;
	unsigned short n;
	unsigned short move;
} tb;          /* 8 bytes */

tb *A;
int I=0;
int Sortedto=0;

int bsortkey( const void *a, const void *b )
{
	if( ((tb*)a)->hashboard > ((tb*)b)->hashboard )
		return 1;
	else
	if( ((tb*)a)->hashboard < ((tb*)b)->hashboard )
		return -1;
	else
	if( ((tb*)a)->move > ((tb*)b)->move )
		return 1;
	else
	if( ((tb*)a)->move < ((tb*)b)->move )
		return -1;
	else
	if( ((tb*)a)->n > ((tb*)b)->n )
		return 1;
	else
	if( ((tb*)a)->n < ((tb*)b)->n )
		return -1;
	else
		return 0;
}

void delmoves(short use)
{
	int i1, i2;
	if( use > Maxdel ) Maxdel = use;
	printf("deleting moves (val<=%i) ... ",use);
	i1=0; i2=0;
	while( i2 < I )
	{
		while( i1 != I && A[i1].n > use ) i1++;
		if( i2 <= i1 ) i2 = i1+1;
		while( i2 != I && A[i2].n <= use ) i2++;
		if( i2 < I )
		{ A[i1]=A[i2]; A[i2].n=0; }
	}
	Sortedto=I=i1;
	printf("done, buffer usage=%i (%i%%)\n",I,100*I/Asize);
}

void delmore(void)
{
	int i1,i2,i;
	printf("selecting moves ........... ");
	i1=0; i2=1;
	while( i2 < I )
	{
/*
#define WDTH 60
*/
#define WDTH 50
		int nbest = A[i1].n;
		if(nbest==0) nbest=1;
		i2=i1+1;
		while( i2<I && A[i2].hashboard==A[i1].hashboard )
		{ if(A[i2].n>nbest) nbest=A[i2].n; i2++; }
		for( i=i1; i!=i2; i++ )
		if( /* A[i].n!=nbest || */ ( A[i].n < Width &&
		  (   100*A[i].n / nbest < WDTH
		 || ( 100*A[i].n / nbest < WDTH+10 && A[i].n < 40 )
		 || ( 100*A[i].n / nbest < WDTH+20 && A[i].n < 20 ) ) ) )
		 A[i].n=0;
		i1=i2;
	}
	puts("done");
}

unsigned int games=0; unsigned int positions=0;

void printit(int x)
{
  printf("%8i%10i%7i%%",
          games,positions,I*100/Asize);
  fflush(stdout);
  signal(SIGALRM,printit); alarm(1);
}

void compress(void)
{
	int i1, i2, use;
	printit(0);
	alarm(0);
	printf("\nsorting buffer ............ ");
/*	qsort( A+Sortedto, Asize-Sortedto, sizeof(tb), bsortkey ); */
	qsort( A, I, sizeof(tb), bsortkey );
	puts("done");
	printf("compressing buffer ........ ");
	i1=0; i2=1;
	while( i2 != I )
	{
		while( i2 != I
		    && A[i1].hashboard == A[i2].hashboard
		    && A[i1].move == A[i2].move )
		{
			A[i1].n += A[i2].n;
			if( A[i1].n > 32000 ) A[i1].n = 30000;
			i2++;
		}
		if( i2 != I )
		{ i1++; A[i1]=A[i2]; i2++; }
	}
	I=Sortedto=i1+1;
	A[I].n = 0;
	printf("done, buffer usage=%i (%i%%)\n",I,100*I/Asize);

	use=1;
	while( 100*I/Asize > 95 ) { delmoves(use); use++; }

	alarm(1);
}

int bpoints, wpoints;

int findgame(void)
{ register char c;
  register int found=0;
  char s[64];
  bpoints=0; wpoints=0;
  while( ! found )
  { while( (c=getchar()) != EOF && c!='R' ) {}
    if( c==EOF ) return 0;
    if( fgets(s,6,stdin)==NULL ) return 0;
    if( strncmp(s,"esult",5) == 0 )
    {
      while( (c=getchar()) == ' ' ) {}
      if( fgets(s,4,stdin)==NULL ) return 0;
      if( strncmp(s,"1-0",3) == 0 ) { bpoints=LOO; wpoints=WIN; }
      else if( strncmp(s,"0-1",3) == 0 ) { bpoints=WIN; wpoints=LOO; }
      else if( strncmp(s,"1/2",3) == 0 ) { bpoints=DRA; wpoints=DRA; }
      else { bpoints=1; wpoints=1; }
      return 1;
    }
  }
  return 0;
}

extern char piece[7];
extern char file[10];
extern char row[12];

short addmove(char *move)
{ static tmove m[256];
  tmove * mf;
  static int n;
  int i;
  int p=PAWN+Color;
  for(i=2;i!=7;i++) if(piece[i]==move[0]) p=16*i+Color;
  if( move[0]=='O' )
  { p=KING+Color;
    if(move[3]=='-') { if(Color==WHITE){} }
  }

  generate_legal_moves( m, &n, checktest(Color) );
  if( (mf=sandex(move,m,n)) != NULL )
  {
    if( Color==WHITE )
    { if(wpoints)
      { A[I].hashboard=G[Counter].hashboard;
        A[I].move = smove(mf);
        A[I].n = wpoints; positions++;
        I++; if( I>=Asize ) compress();
      }
    }
    else
    { if(bpoints)
      { A[I].hashboard=G[Counter].hashboard;
        A[I].move = smove(mf);
        A[I].n = bpoints; positions++;
        I++; if( I>=Asize ) compress();
      }
    }
    do_move(mf);
    return 1;
  } else { return 0; }
}

void parsegame(void)
{ register char c='*';
  char m[128];
  int i;
  setfen("rnbqkbnr/pppppppp/////PPPPPPPP/RNBQKBNR/w");
  while( c!='[' && c!=EOF && Counter < Maxbookply )
  { while( (c=getchar()) != '.' && c!=EOF ) {}
    while( (c=getchar())==' ' || c=='\n' ) {}
    m[0]=c; i=1;
    while( (c=getchar())!=' ' && c!='\n' && c!=EOF ) { m[i]=c; i++; }
    m[i]='\0';
    if( ! addmove(m) ) break;

    /* find black's move */
    while( (c=getchar())==' ' || c=='\n' ) {}
    if( c=='[' || c==EOF ) return;
    m[0]=c; i=1;
    while( (c=getchar())!=' ' && c!='\n' && c!=EOF ) { m[i]=c; i++; }
    m[i]='\0';
    if( ! addmove(m) ) break;
  }
}

void wusage(void)
{
  printf("Usage: bcreate <buffer size in cells>
One cell is %i bytes
", sizeof(tb) );
  exit(0);
}

int main( int argc, char ** argv )
{
  FILE * f;
  int i;
  switch(argc)
  { case 2: Asize=atoi(argv[1]); break;
    default: break;
  }
  if( Asize < 1000 ) wusage();
  A=malloc(Asize*sizeof(tb));
  if( A == NULL )
  { printf("cannot alloc %i bytes of memory\n",Asize*sizeof(tb)); exit(0); }
  setvbuf(stdout, (char*)NULL, _IONBF, 0);
  printf("    Phalanx "); puts(VERSION);
  puts(  "    creating opening book");
  puts(  "-----------------------------");
  puts(  "   games positions buffer%");
  puts(  "-----------------------------");
  signal(SIGALRM,printit); alarm(1);
  while( findgame() )
  { parsegame(); games++; }
  printf("end of file, parsed %i positions in %i games\n",positions,games);
  compress(); alarm(0);
  delmore();
  delmoves(Maxdel+8);
  printf("there is %i moves in the opening book\n",I);

  if( ( f = fopen( SBOOK_FILE, "wb" ) ) == NULL )
  { printf("cannot create book file %s\n", SBOOK_FILE); return 1; }
  for( i=0; i!=I; i++ )
  {
	unsigned short m = A[i].move;
	fwrite( A+i, sizeof(unsigned int), 1, f );
	fwrite( &m, sizeof(unsigned short), 1, f );
  }
  return 0;
}