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#include <inttypes.h>
28
29typedef struct
30{
31  signed Depth:7;
32  unsigned OnMove:1;
33  unsigned char Bestmove;
34  uint32_t 		Hash;
35  uint32_t 		Hold_hash;
36  int32_t		Bound;
37}
38LearnType;
39
40void Learn(int score, int best, int depth)
41{
42  int number = 0, next = 0;
43  LearnType draft;
44  FILE **lrnfile;
45
46  printf("Learning score: %d  best: %d  depth:%d  hash: %X\n", score, best, depth, hash);
47
48  if (Variant == Normal)
49    {
50      lrnfile = &lrn_standard;
51    }
52  else if ((Variant == Crazyhouse) || (Variant == Bughouse))
53    {
54      lrnfile = &lrn_zh;
55    }
56  else if (Variant == Suicide)
57  {
58      lrnfile = &lrn_suicide;
59  }
60  else if (Variant == Losers)
61  {
62      lrnfile = &lrn_losers;
63  }
64  else
65    return;
66
67  fseek(*lrnfile, 0, SEEK_SET);
68  fread(&number, sizeof(int), 1, *lrnfile);
69  fread(&next, sizeof(int), 1, *lrnfile);
70
71  if (number < 50000) number++;
72
73  fseek(*lrnfile, 0, SEEK_SET);
74  fwrite(&number, sizeof(int), 1, *lrnfile);
75
76  next++;
77  if (next == 50000) next = 1;
78
79  fwrite(&next, sizeof(int), 1, *lrnfile);
80
81  fseek(*lrnfile, (2*sizeof(int)) + ((next-1)*sizeof(LearnType)), SEEK_SET);
82
83  draft.Depth = depth;
84  draft.OnMove = ToMove;
85  draft.Hash = hash;
86  draft.Hold_hash = hold_hash;
87  draft.Bound = score;
88  draft.Bestmove = best;
89
90  fwrite(&draft, sizeof(draft), 1, *lrnfile);
91
92  fflush(*lrnfile);
93}
94
95void LoadLearn(void)
96{
97  int number = 0, posloop;
98  LearnType draft;
99  FILE **lrnfile;
100
101  if (((Variant == Crazyhouse) || (Variant == Bughouse)) && (!lrn_zh))
102    return;
103  else if ((Variant == Normal) && !lrn_standard)
104    return;
105  else if (Variant == Suicide && !lrn_suicide)
106    return;
107  else if (Variant == Losers && !lrn_losers)
108    return;
109
110  if (Variant == Normal)
111    {
112      lrnfile = &lrn_standard;
113    }
114  else if ((Variant == Crazyhouse) || (Variant == Bughouse))
115    {
116      lrnfile = &lrn_zh;
117    }
118  else if (Variant == Suicide)
119    {
120      lrnfile = &lrn_suicide;
121    }
122  else if (Variant == Losers)
123  {
124      lrnfile = &lrn_losers;
125  }
126
127  fseek(*lrnfile, 0, SEEK_SET);
128  fread(&number, sizeof(int), 1, *lrnfile);
129  fseek(*lrnfile, 2*sizeof(int), SEEK_SET);
130
131  for (posloop = 0; posloop < number; posloop++)
132    {
133      fread(&draft, sizeof(LearnType), 1, *lrnfile);
134      LearnStoreTT(draft.Bound, draft.Hash, draft.Hold_hash,
135		   draft.OnMove, draft.Bestmove, draft.Depth);
136    }
137
138  return;
139}
140