File: play_solo.c

package info (click to toggle)
gnugo 2.4-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,816 kB
  • ctags: 1,828
  • sloc: ansic: 22,091; tcl: 401; sh: 376; makefile: 202
file content (486 lines) | stat: -rw-r--r-- 12,247 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
 * This is GNU GO, a Go program. Contact gnugo@gnu.org, or see   *
 * http://www.gnu.org/software/gnugo/ for more information.      *
 *                                                               *
 * Copyright 1999 by the Free Software Foundation.               *
 *                                                               *
 * 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 - version 2.     *
 *                                                               *
 * 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 in file COPYING  *
 * 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, USA                                         *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/time.h>

#include "interface.h"

#include "main.h"
#include "sgf.h"
#include "sgf_properties.h"
#include "ttsgf_read.h"
#include "sgfana.h"
#include "liberty.h"



void play_solo(int moves)
{
  int pass=0; /* num. consecutive passes */
  int who=BLACK;
  int moval;
  int mymove;
  double t1, t2;
  struct timeval tv;
  int benchmark = moves;
  
  /* It tends not to be very imaginative in the opening,
   * so we scatter a few stones randomly to start with.
   * We add two random numbers to reduce the probability
   * of playing stones near the edge.
   */
  
  int n = 6 + 2*rand()%5;
  int i,j;

  int bsize, umove, handicap, seed;
  float komi;
  
  bsize=get_boardsize();
  komi=get_komi();
  seed=get_seed();
  handicap=get_handicap();
  mymove=get_computer_player();
  umove=OTHER_COLOR(mymove);
  
  mymove=OTHER_COLOR(umove); /* not needed, but kills an error message */
  sgf_write_game_info(bsize, handicap, komi, seed, "solo");
  
  /* for the randomly generated moves... */
  moval=0;
  
  if (board_size>6)
    do {
      do {
	i = (rand() % 4) + (rand() % (board_size-4));
	j = (rand() % 4) + (rand() % (board_size-4));
      } while (p[i][j] != EMPTY);
      
      updateboard(i,j,who);
      
      /*    if (sgfout)
	    fprintf(sgfout, "A%c[%c%c]", who==WHITE ? 'W' : 'B', 'a'+j, 'a'+i);
      */
      sgf_move_made(i, j, who, moval);
      sgfAddPlay(0,who,i, j);
      sgfAnalyzer(moval);
      who = OTHER_COLOR(who);
    } while (--n > 0);
  
  /*    if (sgfout)
	putc('\n', sgfout);
  */
  
  gettimeofday(&tv, NULL);
  t1 = tv.tv_sec + 1.e-6*tv.tv_usec;
  
  while (pass < 2 && --moves >= 0 && !time_to_die)
    {
      moval=genmove(&i, &j, who);
      updateboard(i,j,who);
      if (moval < 0) {
	++pass;
	printf("%s(%d): Pass\n", who==BLACK ? "Black" : "White",
	       get_movenumber());
      } else {
	pass=0;
	inc_movenumber();
	gprintf("%s(%d): %m\n", who==BLACK ? "Black" : "White",
		get_movenumber(), i,j);
      }
      sgf_move_made(i, j, who, moval);
      sgfAddPlay(0,who,i, j);
      sgfAnalyzer(moval);
      who=OTHER_COLOR(who);
    }
  gettimeofday(&tv, NULL);
  t2 = tv.tv_sec + 1.e-6*tv.tv_usec;
  
  /* two passes and it's over */
  
  who_wins(mymove, komi, stdout);
  printf("%.3f moves/sec\n", (benchmark-moves)/(t2-t1));
  
}





/* FIXME : this should be in a separate source file, but it
 * is easier to send diffs when there are no new source
 * files to add.
 */

/* fills i,j from the property value, in GNU Go co-ords.
 * Note that GNU Go
 * uses different conventions from sgf for co-ordinates
 * been called. Returns 1 for a move, 0 for a pass
 */

static int get_moveXY(SGFPropertyP property, int *i, int *j)
{
  if (!property->value[0])
    {
      *i=board_size;
      *j=board_size;
      return 0;  /* [] is pass */
    }
  *i = property->value[1] - 'a';
  *j = property->value[0] - 'a';
  
  return (*i < 19 && *j < 19);
}



/* this used to read the sgf file character by character,
 * but now we have a full sgf parser available. Variations
 * are not supported.
 *
 * The name is historical : the file has already been read into a tree. Walk the main
 * variation, actioning the properties into the playing
 * board.
 *
 * Returns the colour of the next move to be made.
 *
 * head is a sgf tree (already read from file). until is an optional string of the form
 * either 'L12' or '120' which tells it to stop loading at that move
 * or move-number - when debugging, this
 * will be a particularly bad move, and we want to know why
 */

int load_sgf_file(SGFNodeP head, const char *untilstr)
{
  int bs, handicap;
  int next=BLACK;
  
  int untilm = -1, untiln = -1;
  int until = 9999;
  int addstone=0;          /*handicap stone detector*/
  
  SGFNodeP node;
  
  if (sgfGetIntProperty(head, "SZ", &bs))
    set_boardsize(bs);
  else
    set_boardsize(19);
  
  /* now we can safely parse the until string (which depends on board size) */
  
  if (untilstr)
    {
      if (*untilstr > '0' && *untilstr <= '9')
	{
	  until = atoi(untilstr);
	  DEBUG(DEBUG_LOADSGF, "Loading until move %d\n", until);
	}
      else
	{
	  untiln = *untilstr - 'A';
	  if (*untilstr >= 'I')
	    --untiln;
	  
	  untilm = board_size - atoi(untilstr+1);
	  DEBUG(DEBUG_LOADSGF,"Loading until move at %d,%d (%m)\n", untilm, untiln, untilm, untiln);
	}
    }
  
  if (sgfGetIntProperty(head, "HA", &handicap) && handicap > 0)
    {
      set_handicap(handicap);
      next = WHITE;
    }
  
  
  /* finally, we iterate over all the properties of all the
   * nodes, actioning them. We follow only the 'child' pointers,
   * as we have no interest in variations.
   * The sgf routines map AB[aa][bb][cc] into AB[aa]AB[bb]AB[cc]
   */
  for (node=head; node; node=node->child)
    {
      SGFPropertyP prop;
      int i,j;
      
      sgfSetLastNode(node);

      for (prop=node->prop; prop; prop=prop->next)
	{
	  DEBUG(DEBUG_LOADSGF, "%c%c[%s]\n", prop->name & 0xff, (prop->name >> 8), prop->value);
	  switch(prop->name)
	    {
	    case SGFAB:
	      get_moveXY(prop, &i, &j);
	      put_stone(i,j,BLACK);
	      addstone=1;
	      break;
	      
	    case SGFAW:
	      get_moveXY(prop, &i, &j);
	      put_stone(i,j,WHITE);
	      addstone=1;
	      break;
	      
	    case SGFPL:
	      if (!get_movenumber() && !addstone)
		sethand(get_handicap());
	      
	      if (prop->value[0] == 'w' || prop->value[0] == 'W')
		next = WHITE;
	      else
		next = BLACK;
	      break;
	      
	    case SGFW:
	    case SGFB:
	      next = prop->name == SGFW ? WHITE : BLACK;
	      if (!get_movenumber() && !addstone)
		sethand(get_handicap());
	      inc_movenumber();
	      if (get_movenumber() == until)
		return next;
	      
	      if (get_moveXY(prop, &i, &j))
		{
		  if (i == untilm && j == untiln)
		    return next;
		  
		  put_move(i,j,next);
		  /* send the move to the file, move_val=0 */
		  /*  sgf_move_made(i, j, next, 0);        */
		  /* updateboard(i,j,next);                */
		}
	      
	      break;
	    }
	}
    }
  return OTHER_COLOR(next);
}

/* reads header info from sgf structure and sets the appropriate variables*/
void
load_sgf_header(SGFNodeP head)
{
  int bs;
  int handicap;
  float komi;
  
  if (sgfGetIntProperty(head, "SZ", &bs))
    set_boardsize(bs);
  else
    set_boardsize(19);
  
  if (sgfGetIntProperty(head, "HA", &handicap) && handicap > 0)
    /*    set_handicap(handicap=sethand(handicap));*/
    set_handicap(handicap);
  /*handicap stones should appear as AW,AB properties in the sgf file*/
  if (sgfGetFloatProperty(head,"KM",&komi))
    set_komi(komi);
}


/* load sgf file and run genmove() */


void 
load_and_analyze_sgf_file(SGFNodeP head, const char *untilstr, int benchmark)
{
  int i,j;
  int next;
  
  /* first we load the header to get correct board_size, 
     komi and handicap for writing */
  
  load_sgf_header(head); 
  sgf_write_game_info(board_size, get_handicap(),
		      get_komi(),get_seed(), "load and analyze");
  next = load_sgf_file(head, untilstr);
  
  if (benchmark)
    {
      int z;
      for (z=0; z < benchmark; ++z)
	{
	  genmove(&i, &j, next);
	  next = OTHER_COLOR(next);
	}
    }
  else
    {
      genmove(&i, &j, next);
      
      if (i != board_size) 
	{
	  gprintf("%s move %m\n", next == WHITE ? "white (o)" : "black (X)", i,j);
	  put_move(i, j, next);
	}
      else
	{
	  gprintf("%s move : PASS!\n", next == WHITE ? "white (o)" : "black (X)");
	  sgf_move_made(i, j, next, 0);
	}
    }
}

/* load sgf file and score the game
   untilstr:
   end  - finish the game by selfplaying from the end of the file until two passes
   last - estimate territorial balance at the end of the of the file
   move - load file until move is reached and estimate territorial balance
*/


void 
load_and_score_sgf_file(SGFNodeP head, const char *untilstr)
{
  int i, j, moval, pass, wt, bt;
  int until;
  float score;
  float komi;
  float result;
  char *tempc=NULL;
  char dummy;
  char text[250];
  char winner;
  int next;
  pass=0;

  assert(head);
  assert(sgf_root);
  load_sgf_header(head);
  sgf_write_game_info(board_size, get_handicap(),get_komi(),get_seed(), "load and score");
  next=load_sgf_file(head, untilstr);
  komi=get_komi();
  
  until=atoi(untilstr);
  if (!strcmp(untilstr,"end"))
  {
    until=9999;
    do {
      moval=genmove(&i, &j, next);
      updateboard(i,j,next);
      if (moval>=0) {
	pass=0;
	gprintf("%d %s move %m\n",get_movenumber(),
		next == WHITE ? "white (o)" : "black (X)", i,j);
      } else {
	++pass;
	gprintf("%d %s move : PASS!\n",get_movenumber(), 
		next == WHITE ? "white (o)" : "black (X)");
      }
      sgf_move_made(i, j, next, moval);
      sgfAddPlay(0,next,i, j);
      sgfAnalyzer(moval);
      inc_movenumber();
      next = OTHER_COLOR(next);
    } while ((get_movenumber()<=until)&(pass<2));
  if (pass>=2)
    {
      /*count actually owned territory*/
      evaluate_territory(&wt,&bt);
      score=white_captured+bt-black_captured-wt-komi;
      if (score>0)
	{
	  sprintf(text,"Black wins by %1.1f points\n", score);
	  winner='B';
	}
      else if (score<0)
	{
	  sprintf(text,"White wins by %1.1f points\n", -1.0 * score);
	  winner='W';
	}
      else
	{
	  sprintf(text,"Tie\n");
	  winner='0';
	}
      fputs(text,stdout);
      sgfShowTerritory(0);
      sgf_write_comment(text);
      if (sgfGetCharProperty(head, "RE", &tempc))
	{
	  if (sscanf(tempc,"%1c%f",&dummy,&result)==2)
	    {
	      fprintf(stdout,"Result from file: %1.1f\n",result);
	      fputs("GNU Go result and result from file are ",stdout);
	      if ((result==abs(score)) && (winner==dummy))
		fputs("identical\n",stdout);
	      else
		fputs("different\n",stdout);
	      
	    } else
	      if (tempc[2]=='R')
		{
		  fprintf(stdout,"Result from file: Resign\n");
		  fputs("GNU Go result and result from file are ",stdout);
		  if (tempc[0]==winner)
		    fputs("identical\n",stdout);
		  else
		    fputs("different\n",stdout);
		}
	}
      sgfWriteResult(score);
      return;
    }
  }
  moval=genmove(&i, &j, next);
  sgfAnalyzer(moval);
  score=terri_eval[BLACK]-terri_eval[WHITE]-komi;
  sprintf(text,
"Black moyo: %3i\n\
White moyo: %3i\n\
Black territory: %3i\n\
White territory: %3i\n\
Dead white stones: %3i\n\
Dead black stones: %3i\n\
%s seems to win by %1.1f points\n",
	  moyo_eval[BLACK],moyo_eval[WHITE],
	  terri_eval[BLACK],terri_eval[WHITE],
	  white_captured,
	  black_captured,
	  (score>0)?("BLACK"):((score<0)?("WHITE"):("NOBODY")),
	  (score<0)?(-1*score):(score)
	  );
  if (strcmp(untilstr,"last"))
      sgfSetLastNode(sgfStartVariant(0));
  sgfShowMoyo(SGF_SHOW_COLOR,0);
  sgf_write_comment(text);
  fputs(text,stdout);
}


/*
 * Local Variables:
 * tab-width: 8
 * c-basic-offset: 2
 * End:
 */