1 /*
2     Sjeng - a chess variants playing program
3     Copyright (C) 2000 Gian-Carlo Pascutto
4 
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 
19     File: learn.c
20     Purpose: remembering of positions
21 
22 */
23 
24 #include "sjeng.h"
25 #include "protos.h"
26 #include "extvars.h"
27 
28 typedef struct
29 {
30   signed Depth:7;
31   unsigned OnMove:1;
32   unsigned char Bestmove;
33   unsigned long Hash;
34   unsigned long Hold_hash;
35   signed long Bound;
36 }
37 LearnType;
38 
Learn(int score,int best,int depth)39 void Learn(int score, int best, int depth)
40 {
41   int number = 0, next = 0;
42   LearnType draft;
43   FILE **lrnfile;
44 
45   printf("Learning score: %d  best: %d  depth:%d  hash: %X\n", score, best, depth, hash);
46 
47   if (Variant == Normal)
48     {
49       lrnfile = &lrn_standard;
50     }
51   else if ((Variant == Crazyhouse) || (Variant == Bughouse))
52     {
53       lrnfile = &lrn_zh;
54     }
55   else if (Variant == Suicide)
56   {
57       lrnfile = &lrn_suicide;
58   }
59   else if (Variant == Losers)
60   {
61       lrnfile = &lrn_losers;
62   }
63   else
64     return;
65 
66   fseek(*lrnfile, 0, SEEK_SET);
67   fread(&number, sizeof(int), 1, *lrnfile);
68   fread(&next, sizeof(int), 1, *lrnfile);
69 
70   if (number < 50000) number++;
71 
72   fseek(*lrnfile, 0, SEEK_SET);
73   fwrite(&number, sizeof(int), 1, *lrnfile);
74 
75   next++;
76   if (next == 50000) next = 1;
77 
78   fwrite(&next, sizeof(int), 1, *lrnfile);
79 
80   fseek(*lrnfile, (2*sizeof(int)) + ((next-1)*sizeof(LearnType)), SEEK_SET);
81 
82   draft.Depth = depth;
83   draft.OnMove = ToMove;
84   draft.Hash = hash;
85   draft.Hold_hash = hold_hash;
86   draft.Bound = score;
87   draft.Bestmove = best;
88 
89   fwrite(&draft, sizeof(draft), 1, *lrnfile);
90 
91   fflush(*lrnfile);
92 }
93 
LoadLearn(void)94 void LoadLearn(void)
95 {
96   int number = 0, posloop;
97   LearnType draft;
98   FILE **lrnfile;
99 
100   if (((Variant == Crazyhouse) || (Variant == Bughouse)) && (!lrn_zh))
101     return;
102   else if ((Variant == Normal) && !lrn_standard)
103     return;
104   else if (Variant == Suicide && !lrn_suicide)
105     return;
106   else if (Variant == Losers && !lrn_losers)
107     return;
108 
109   if (Variant == Normal)
110     {
111       lrnfile = &lrn_standard;
112     }
113   else if ((Variant == Crazyhouse) || (Variant == Bughouse))
114     {
115       lrnfile = &lrn_zh;
116     }
117   else if (Variant == Suicide)
118     {
119       lrnfile = &lrn_suicide;
120     }
121   else if (Variant == Losers)
122   {
123       lrnfile = &lrn_losers;
124   }
125 
126   fseek(*lrnfile, 0, SEEK_SET);
127   fread(&number, sizeof(int), 1, *lrnfile);
128   fseek(*lrnfile, 2*sizeof(int), SEEK_SET);
129 
130   for (posloop = 0; posloop < number; posloop++)
131     {
132       fread(&draft, sizeof(LearnType), 1, *lrnfile);
133       LearnStoreTT(draft.Bound, draft.Hash, draft.Hold_hash,
134 		   draft.OnMove, draft.Bestmove, draft.Depth);
135     }
136 
137   return;
138 }
139