File: sgfPlay.c

package info (click to toggle)
cgoban 1.9.14-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,360 kB
  • ctags: 3,910
  • sloc: ansic: 36,036; sh: 702; makefile: 252
file content (287 lines) | stat: -rw-r--r-- 8,340 bytes parent folder | download | duplicates (7)
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
/*
 * $Source: /cvsroot/cgoban1/cgoban1/src/sgfPlay.c,v $
 * $Revision: 1.2 $
 * $Author: wmshub $
 * $Date: 2002/05/31 23:40:54 $
 *
 * src/sgfPlay.c, part of Complete Goban (game program)
 * Copyright  1995,2002 William Shubert.
 * See "configure.h.in" for more copyright information.
 */


#include <ctype.h>
#include <wms.h>
#include <wms/rnd.h>
#include <wms/str.h>
#include <but/but.h>
#include "cgoban.h"
#include "goPic.h"
#include "sgf.h"
#include "sgfPlay.h"
#include "msg.h"


/**********************************************************************
 * Forward declarations
 **********************************************************************/
static void  processMoves(SgfElem *me, GoGame *game, int *handicap,
			  bool *handicapPlayed, Cgoban *cg);
static void  processNodeMarks(SgfElem *me, GoGame *game, GoPic *pic,
			      bool *changed);
static void  eraseObsoleteMarks(GoPic *pic, GoGame *game, bool *changed);


/**********************************************************************
 * Functions
 **********************************************************************/
int  sgf_play(Sgf *mc, GoGame *game, GoPic *pic, int numNodes,
	      SgfElem *terminator)  {
  int  handicap = 0, i, currentNode = 0;
  SgfElem  *me;
  bool  handicapPlayed = FALSE;
  SgfElem  *nodeStart = mc->top.activeChild;
  bool  *changed;

  goGame_moveTo(game, 0);
  if (numNodes >= 0)
    ++numNodes;
  if (terminator != NULL)  {
    terminator = terminator->activeChild;
  }
  for (me = mc->top.activeChild;
       (me != terminator) && numNodes;  me = me->activeChild)  {
    assert(MAGIC(me));
    mc->active = me;
    if (me->type == sgfType_node)  {
      assert((me->childH == NULL) || (me->childH != me->childT) ||
	     (me->activeChild == me->childH));
      --numNodes;
      ++currentNode;
      if (numNodes)  {
	nodeStart = me->activeChild;
	/*
	 * We take out the last move at each node to make sure that
	 *   you don't see that silly ring forever.  If there's another
	 *   move coming then this will get undone when we move.
	 */
	goGame_noLastMove(game);
      } else  {
	mc->active = me->parent;
      }
    } else  {
      assert(me->parent->childH == me->parent->childT);
      assert(me->parent->activeChild == me->parent->childH);
      if (pic == NULL)
	processMoves(me, game, &handicap, &handicapPlayed, NULL);
      else
	processMoves(me, game, &handicap, &handicapPlayed, pic->cg);
    }
  }

  /*
   * Now set the clock to match the time that we had left.

  for (i = game->moveNum - 1;  i >= 0;  --i)  {
    if (game->moves[i].color == goStone_white)
      game->timers[goStone_white] = game->moves[i].time;
  }
  for (i = game->moveNum - 1;  i >= 0;  --i)  {
    if (game->moves[i].color == goStone_black)
      game->timers[goStone_black] = game->moves[i].time;
  }
   */  
  /*
   * If there are handicap stones that were added automatically and aren't
   *   in the movechain, then add them now.
   * This is really stupid.  There is no way that adding handicap stones
   *   should be in the sgf play code!  :-(
   */
  if (!handicapPlayed && handicap && !game->passive)  {
    for (i = 0;  i < goBoard_area(game->board);  ++i)  {
      if (goBoard_stone(game->board, i) == goStone_black)
	sgf_addStone(mc, goStone_black, goBoard_loc2Sgf(game->board, i));
    }
  }

  /*
   * If we were given a GoPic, then add marks to it now.
   */
  if (pic)  {
    changed = wms_malloc(goBoard_area(game->board) * sizeof(bool));
    memset(changed, 0, goBoard_area(game->board) * sizeof(bool));
    while (nodeStart)  {
      if (nodeStart->type == sgfType_node)
	break;
      processNodeMarks(nodeStart, game, pic, changed);
      nodeStart = nodeStart->activeChild;
    }
    eraseObsoleteMarks(pic, game, changed);
    wms_free(changed);
  }

  if (me)
    --currentNode;
  return(currentNode);
}


static void  processMoves(SgfElem *me, GoGame *game, int *handicap,
			  bool *handicapPlayed, Cgoban *cg)  {
  int  i;

  switch(me->type)  {
  case sgfType_node:
    assert(0);
    break;
  case sgfType_handicap:
    *handicap = me->iVal;
    break;
  case sgfType_whoseMove:
    game->setWhoseMoveNum = game->moveNum;
    game->setWhoseMoveColor = me->gVal;
    game->whoseMove = me->gVal;
    break;
  case sgfType_move:
    *handicapPlayed = TRUE;
    i = goBoard_sgf2Loc(game->board, me->lVal);
    if (!*handicap || (goBoard_stone(game->board, i) != goStone_black))  {
      assert(game->passive || (me->gVal == game->whoseMove));
      if (!goGame_isLegal(game, me->gVal, i))  {
	Str  errmsg;
	char  moveStr[5];

	str_init(&errmsg);
	goBoard_loc2Str(game->board, i, moveStr),
	str_print(&errmsg, msg_badMoveInSgf, moveStr,
		  game->moveNum);
	if (cg != NULL)
	  cgoban_createMsgWindow(cg, "CGoban: Error in SGF file",
				 str_chars(&errmsg));
	str_deinit(&errmsg);
      } else  {
	goGame_move(game, me->gVal, i, NULL);
      }
    }
    if (*handicap)
      --*handicap;
    break;
  case sgfType_pass:
    if (game->state != goGameState_selectDead)  {
      /*
       * IGS sends out games with more than two passes at the end of the
       *   game.  If you've already gone to non-play state and you get
       *   passes, then ignore them.
       */
      goGame_pass(game, me->gVal, NULL);
    }
    break;
  case sgfType_setBoard:
    i = goBoard_sgf2Loc(game->board, me->lVal);
    if ((me->gVal == goStone_empty) &&
	((game->state == goGameState_selectDead) ||
	 game->forcePlay))  {
      if (!(game->flags[i] & GOGAMEFLAGS_MARKDEAD))  {
	goGame_markDead(game, i);
      }
    } else  {
      if (goBoard_stone(game->board, i) != me->gVal)  {
	goGame_setBoard(game, me->gVal, i);
      }
      assert(goBoard_stone(game->board, i) == me->gVal);
    }
    break;
  case sgfType_timeLeft:
    if (me->iVal < 0)  {
      /*
       * This is marking a time loss.  Fake up the game structure to
       *   make it clear that this is what happened.
       */
      game->maxMoves = ++game->moveNum;
    }
    game->moves[game->moveNum - 1].time.timeLeft = me->iVal;
    break;
  case sgfType_stonesLeft:
    game->moves[game->moveNum - 1].time.aux = me->iVal;
    break;
  default:
    break;
  }
}	


static void  processNodeMarks(SgfElem *me, GoGame *game, GoPic *pic,
			      bool *changed)  {
  int  loc, i;
  GoMarkType  mark;
  bool  err;

  assert(MAGIC(me));
  switch(me->type)  {
  case sgfType_territory:
    loc = goBoard_sgf2Loc(game->board, me->lVal);
    changed[loc] = TRUE;
    mark = goMarkType_stone2sm(me->gVal);
    if (goStone_isStone(goBoard_stone(game->board, loc)))  {
      if (!grid_grey(pic->boardButs[loc]))  {
	grid_setStone(pic->boardButs[loc], goBoard_stone(game->board, loc),
		      TRUE);
      }
    }
    if (grid_markType(pic->boardButs[loc]) != mark)  {
      grid_setMark(pic->boardButs[loc], mark, 0);
    }
    break;
  case sgfType_triangle:
  case sgfType_circle:
  case sgfType_square:
  case sgfType_mark:
    if (me->type == sgfType_triangle)
      mark = goMark_triangle;
    else if (me->type == sgfType_circle)
      mark = goMark_circle;
    else if (me->type == sgfType_mark)
      mark = goMark_x;
    else  /* me->type == sgfType_square */
      mark = goMark_square;
    loc = goBoard_sgf2Loc(game->board, me->lVal);
    changed[loc] = TRUE;
    if (grid_grey(pic->boardButs[loc]))  {
      grid_setStone(pic->boardButs[loc], goBoard_stone(game->board, loc),
		    FALSE);
    }
    if (grid_markType(pic->boardButs[loc]) != mark)  {
      grid_setMark(pic->boardButs[loc], mark, 0);
    }
    break;
  case sgfType_label:
    loc = goBoard_sgf2Loc(game->board, me->lVal);
    changed[loc] = TRUE;
    /* See if it is a numeric label. */
    i = wms_atoi(str_chars(me->sVal), &err);
    if (err)
      grid_setMark(pic->boardButs[loc], goMark_letter, str_chars(me->sVal)[0]);
    else
      grid_setMark(pic->boardButs[loc], goMark_number, i);
    break;
  default:
    break;
  }
}


static void  eraseObsoleteMarks(GoPic *pic, GoGame *game, bool *changed)  {
  int  loc;

  for (loc = 0;  loc < goBoard_area(game->board);  ++loc)  {
    if (!changed[loc] && pic->boardButs[loc])  {
      if (grid_grey(pic->boardButs[loc]) ||
	  (grid_markType(pic->boardButs[loc]) != goMark_none))  {
	grid_setStone(pic->boardButs[loc], goBoard_stone(game->board, loc),
		      FALSE);
	grid_setMark(pic->boardButs[loc], goMark_none, 0);
      }
    }
  }
}