File: crazy.c

package info (click to toggle)
sjeng 11.2-8
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, squeeze, stretch, wheezy
  • size: 952 kB
  • ctags: 710
  • sloc: ansic: 13,313; sh: 324; makefile: 83
file content (334 lines) | stat: -rw-r--r-- 7,146 bytes parent folder | download | duplicates (2)
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
    Sjeng - a chess variants playing program
    Copyright (C) 2000 Gian-Carlo Pascutto

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    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 the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                                                          
    File: crazy.c
    Purpose: bughouse/crazyhouse specific functions                  

*/ 

#include <assert.h>
#include "sjeng.h"
#include "protos.h"
#include "extvars.h"

int holding[2][16];
int num_holding[2];

char realholdings[255];
int userealholdings;

int drop_piece;

int white_hand_eval;
int black_hand_eval;

unsigned long hold_hash;

#define HHash(x,y)  (hold_hash ^= zobrist[(x)][(y)])

/* input example : holding [BPPP] [QR] */
/* based on db's parser */
void ProcessHoldings(char str[])
{
  int c, i;

  i = 0;

  memset(holding, 0, sizeof(holding));
  hold_hash = 0xC0FFEE00;

  white_hand_eval = 0;
  black_hand_eval = 0;
  reset_ecache();
  
  num_holding[WHITE] = 0;
  num_holding[BLACK] = 0;

  for(c = WHITE; c <= BLACK; c++) 
    {
      while(str[i++] != '[')
	if(str[i] == 0) return;
      
      while(str[i] != ']') {
	switch(str[i++]) {
	case 'p':
	case 'P':
	  holding[c][c == WHITE ? wpawn : bpawn]++;
	  num_holding[c]++;
	  HHash((c == WHITE ? wpawn : bpawn),
		holding[c][(c == WHITE ? wpawn : bpawn)]); 
	  break;
	case 'q':
	case 'Q':
	  holding[c][c == WHITE ? wqueen : bqueen]++;
	  num_holding[c]++;
	  HHash((c == WHITE ? wqueen : bqueen),
		holding[c][(c == WHITE ? wqueen : bqueen)]); 
	  break;
	case 'r':
	case 'R':
	  holding[c][c == WHITE ? wrook : brook]++;
	  num_holding[c]++;
	  HHash((c == WHITE ? wrook : brook),
		holding[c][(c == WHITE ? wrook : brook)]); 
	  break;
	case 'b':
	case 'B':
	  holding[c][c == WHITE ? wbishop : bbishop]++;
	  num_holding[c]++;
	  HHash((c == WHITE ? wbishop : bbishop),
		holding[c][(c == WHITE ? wbishop : bbishop)]); 
	  break;
	case 'n':
	case 'N':
	  holding[c][c == WHITE ? wknight : bknight]++;
	  num_holding[c]++;
	  HHash((c == WHITE ? wknight : bknight),
		holding[c][(c == WHITE ? wknight : bknight)]); 
	  break;
	default:
	  return;
	}
      }
    }

  /* no fake pieces in crazyhouse! */
  if (Variant == Bughouse && !userealholdings)
    {
      strncpy(realholdings, str, 200);

      if (comp_color == 1)
	{
	  /* computer is white*/
	  if (holding[BLACK][bpawn] == 0)
	    {
	      holding[BLACK][bpawn]++;
	      num_holding[BLACK]++;
	      HHash(bpawn, holding[BLACK][bpawn]);
	    }
	  if (holding[BLACK][bbishop] == 0)
	    {
	      holding[BLACK][bbishop]++;
	      num_holding[BLACK]++;
	      HHash(bpawn, holding[BLACK][bbishop]);
	    }
	  if (holding[BLACK][bknight] == 0)
	  {
	    holding[BLACK][bknight]++;
	    num_holding[BLACK]++;
	    HHash(bknight, holding[BLACK][bknight]);
	  }
	  if (holding[BLACK][brook] == 0)
	  {
	    holding[BLACK][brook]++;
	    num_holding[BLACK]++;
	    HHash(bknight, holding[BLACK][brook]);
	  }
	  if (holding[BLACK][bqueen] == 0)
	  {
	    holding[BLACK][bqueen]++;
	    num_holding[BLACK]++;
	    HHash(bknight, holding[BLACK][bqueen]);
	  }
	}
      else
	{
	  /* computer is black*/
	  if (holding[WHITE][wqueen] == 0)
	  {
	    holding[WHITE][wqueen]++;
	    num_holding[WHITE]++;
	    HHash(wqueen, holding[WHITE][wqueen]);
	  }
	  if (holding[WHITE][wrook] == 0)
	  {
	    holding[WHITE][wrook]++;
	    num_holding[WHITE]++;
	    HHash(wqueen, holding[WHITE][wrook]);
	  }
	  if (holding[WHITE][wbishop] == 0)
	  {
	    holding[WHITE][wbishop]++;
	    num_holding[WHITE]++;
	    HHash(wqueen, holding[WHITE][wbishop]);
	  }
	  if (holding[WHITE][wknight] == 0)
	  {
	    holding[WHITE][wknight]++;
	    num_holding[WHITE]++;
	    HHash(wqueen, holding[WHITE][wknight]);
	  }
	  if (holding[WHITE][wpawn] == 0)
	  {
	    holding[WHITE][wpawn]++;
	    num_holding[WHITE]++;
	    HHash(wqueen, holding[WHITE][wpawn]);
	  }
	}
    }
}

int text_to_piece(char txt, int who)
{
  switch(txt)
    {
    case 'p':
    case 'P':
      return (who == WHITE ? wpawn : bpawn);
    case 'b':
    case 'B':
      return (who == WHITE ? wbishop : bbishop);
    case 'n':
    case 'N':
      return (who == WHITE ? wknight : bknight);
    case 'r':
    case 'R':
      return (who == WHITE ? wrook : brook);
    case 'q':
    case 'Q':
      return (who == WHITE ? wqueen : bqueen);
    };

  return npiece;
}

int SwitchColor(int piece)
{
  int t[] = { 0, bpawn, wpawn, bknight, wknight, 0, 0, brook, wrook, bqueen, wqueen, bbishop, wbishop };

  assert(piece > frame  && piece < npiece);

  return(t[piece]);
}

int SwitchPromoted(int piece)
{
  int t[] = { 0, bpawn, wpawn, bpawn, wpawn, 0, 0, bpawn, wpawn, bpawn, wpawn, bpawn, wpawn };

  assert(piece > frame && piece < npiece);

  return(t[piece]);
}

void addHolding(int what, int who)
{

  if (Variant == Crazyhouse)
    {

      holding[who][what]++;
      
      num_holding[who]++;
      
      HHash(what, holding[who][what]);

    };

  if (who == WHITE)
    white_hand_eval += hand_value[what];
  else
    black_hand_eval += hand_value[what];

  Material += material[what];

  return;
    
}

void removeHolding(int what, int who)
{

  if (Variant == Crazyhouse)
    {

      assert(holding[who][what] > 0);
      assert(holding[who][what] < 20);	
      
      HHash(what, holding[who][what]);
      
      holding[who][what]--;
      
      num_holding[who]--;
      
    }

  if (who == WHITE)
  	white_hand_eval -= hand_value[what];
  else
    	black_hand_eval -= hand_value[what];

  Material -= material[what];

  return;

}

void DropaddHolding(int what, int who)
{
  holding[who][what]++;
  
  num_holding[who]++;
  
  HHash(what, holding[who][what]);
  
  if (who == WHITE)
    white_hand_eval += hand_value[what];
  else
    black_hand_eval += hand_value[what];

  Material += material[what];

  return;
}

void DropremoveHolding(int what, int who)
{
  assert(holding[who][what] > 0);

  assert(holding[who][what] < 20);	

  HHash(what, holding[who][what]);
  
  holding[who][what]--;
  
  num_holding[who]--;

  if (who == WHITE)
      white_hand_eval -= hand_value[what];
  else
      black_hand_eval -= hand_value[what];

  Material -= material[what];

  return;
}

void printHolding(void)
{

  printf("WP: %d WR: %d WB: %d WN: %d WQ: %d\n",
	 holding[WHITE][wpawn], holding[WHITE][wrook], 
	 holding[WHITE][wbishop],
	 holding[WHITE][wknight], holding[WHITE][wqueen]);
  
  printf("BP: %d BR: %d BB: %d BN: %d BQ: %d\n",
	 holding[BLACK][bpawn], holding[BLACK][brook], 
	 holding[BLACK][bbishop],
	 holding[BLACK][bknight], holding[BLACK][bqueen]);

}