From 5dafab747e0fa4aa877ea851383b985af00cfe98 Mon Sep 17 00:00:00 2001
From: H.G.Muller <hgm@hgm-xboard.(none)>
Date: Mon, 27 Oct 2014 23:23:00 +0100
Subject: [PATCH] Add infrastructure for keeping track of board position

A Xiangqi board is added, with code to set it up from a FEN, convert it
to the board part of a FEN, and apply the moves from the move list to it,
keeping track of what was the last capture. The FEN for setting it up
is always taken from the "fen <FEN>" position command ('iniPos') of the
Cyclone dialect used in variant xiangqi.
---
 UCI2WB.c |   47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 47 insertions(+), 0 deletions(-)

diff --git a/UCI2WB.c b/UCI2WB.c
index 77cb1da..d7e9671 100644
--- a/UCI2WB.c
+++ b/UCI2WB.c
@@ -34,6 +34,7 @@ char move[2000][10], checkOptions[8192], iniPos[256], hashOpt[20], pause, ponder
 int mps, tc, inc, sTime, depth, myTime, hisTime, stm, computer = NONE, memory, oldMem=0, cores, moveNr, lastDepth, lastScore, startTime, debug;
 int statDepth, statScore, statNodes, statTime, currNr, size, collect, nr, sm, inex, on[500];
 char currMove[20], moveMap[500][10], /* for analyze mode */ canPonder[20], threadOpt[20];
+char board[100];  // XQ board for UCCI
 int unit = 1;
 
 FILE *toE, *fromE, *fromF;
@@ -79,6 +80,52 @@ Sync (int action)
 }
 
 void
+FromFEN(char *fen)
+{	int i=0;
+	while(*fen) {
+	    char c = *fen++;
+	    if(c >= 'A') board[i++] = c; else
+	    if(c == '/') i++; else
+	    if(c == ' ') break; else
+	    while(c-- > '0' && i < 99) board[i++] = 0;
+	    if(i >= 99) break;
+	}
+}
+
+char *
+ToFEN(int stm)
+{
+	int i, n=0; static char fen[200]; char *p = fen;
+	for(i=0; i<99; i++) {
+	    char c = board[i];
+	    if(c >= 'A')  { if(n) *p++ = '0' + n; n = 0;  *p++ = c; } else n ++;
+	    if(i%10 == 8) { if(n) *p++ = '0' + n; n = -1; *p++ = '/'; }
+	}
+	sprintf(p-1, " %c - - 0 1", stm);
+	return fen;
+}
+
+int
+Sqr(char *m, int j)
+{
+	int n = m[j] - 'a' + 10*('9' - m[j+1]);
+	if(n < 0) n = 0; else if(n > 99) n = 99; return n;
+}
+
+int
+Play(int nr)
+{
+	int i, last = -1;
+	FromFEN(iniPos + 4); // in XQ iniPos always has just "fen " prefix
+	for(i=0; i<nr; i++) {
+	    int from=Sqr(move[i], 0), to=Sqr(move[i], 2);
+	    if(board[to]) last = i;
+	    board[to] = board[from]; board[from] = 0;
+	}
+	return last;
+}
+
+void
 StartSearch(char *ponder)
 {	// send the 'go' command to engine. Suffix by ponder.
 	int x = (ponder[0] != 0);
-- 
1.7.0.4