sgfadapter.java
来自「java 开发的围棋打谱程序 可供大家做参考」· Java 代码 · 共 257 行
JAVA
257 行
package org.nebula.goapplet;
import java.io.*;
import java.util.*;
import org.nebula.cutil.*;
/**
* SgfAdapter
*
* Description:
*
* @author harry
*
*/
public final class SgfAdapter {
static Movement root = null;
static Movement lastMove = null;
static Match match;
public static Match readMatch(InputStream input) {
Vector record = new Vector();
match = new Match();
try {
byte[] buf = new byte[4 * 8192];
int len = input.read(buf);
if (len == 4 * 8192) {
Logger.error("******** out of buffer size *************");
return null;
}
int s = 0;
if ((char) buf[s] != '(')
return null;
s++;
if ((char) buf[s] != ';')
return null;
int pos2 = 1, pos1;
while ((s < len) && ((char) buf[s] != ')')) {
s++;
s = parseItem(buf, len, s, match);
}
} catch (Exception e) {
e.printStackTrace();
}
Movement firstMovement = match.getMovement(0);
String c = firstMovement.getComment();
String hdr = "Black: " + match.blackName + "\n"
+ "White: " + match.whiteName + "\n"
+ "Event: " + match.matchName + "\n"
+ "Date : " + match.matchDate + "\n"
+ c;
firstMovement.setComment(hdr);
return match;
}
protected static int parseVariation(byte[] buf, int len, int index,
Movement movement) {
// System.out.println("====parsing var: " + (char)buf[index]);
int s = index;
if ((char) buf[s] != '(')
return -1;
s++;
if ((char) buf[s] != ';')
return -1;
Variation v = new Variation();
while ((char) buf[s] != ')') {
s++;
s = parseItem(buf, len, s, v);
}
// System.out.println("======closing var: " + (char)buf[s+1]);
movement.addVariation(v);
return s + 1;
}
// B[]LB[]C[](;...)();
// return when cursor on ';'
protected static int parseItem(byte[] buf, int len, int index,
Variation variation) {
int s = index;
Movement m = new Movement();
String attrName = null, attrValue = null;
while (s < len) {
char c = (char) buf[s];
while ((c == ' ') || (c == 0x0D) || (c == 0x0A) || (c == '\t')) {
s++;
if (s >= len) {
break;
}
c = (char) buf[s];
}
if ((c == ';') || (c == ')')) {
break;
}
if (c == '(') { // new variation
s = parseVariation(buf, len, s, m);
c = (char) buf[s];
continue;
}
// System.out.println("\t tag: " + c);
int pos = getCharIndex(buf, len, s, '[');
if (pos < 0) {
break;
}
String str = (new String(buf, s, pos - s)).trim();
if (str.length() > 0) {
attrName = str;
}
s = pos;
pos = getCharIndex(buf, len, s, ']');
if (pos < 0) {
break;
}
attrValue = new String(buf, s + 1, pos - s - 1);
parseAttribute(attrName, attrValue, m);
s = pos + 1;
}
// Logger.println("adding movement [" + m.col + "," + m.row + "," +
// match.getMovementCount() + "]");
if ((m.player != GoPlayer.UNKNOWN) || (m.getEraseCount() > 0)
|| (m.getForceCount() > 0)) {
variation.addMovement(m);
}
return s;
}
protected static void parseAttribute(String attrName, String attrValue,
Movement current) {
// System.out.println("parsing: " + attrName + "," + attrValue);
if (attrName.equals("B")) {
if (attrValue.length() != 2) {
return;
}
current.player = GoPlayer.BLACK;
char xx = attrValue.charAt(0);
char yy = attrValue.charAt(1);
current.col = xx - 'a';
current.row = yy - 'a';
} else if (attrName.equals("W")) {
if (attrValue.length() != 2) {
return;
}
current.player = GoPlayer.WHITE;
char xx = attrValue.charAt(0);
char yy = attrValue.charAt(1);
current.col = xx - 'a';
current.row = yy - 'a';
} else if (attrName.equals("AB")) {
if (attrValue.length() != 2) {
return;
}
char xx = attrValue.charAt(0);
char yy = attrValue.charAt(1);
current.addForce(xx - 'a', yy - 'a', GoPlayer.BLACK);
} else if (attrName.equals("AW")) {
if (attrValue.length() != 2) {
return;
}
char xx = attrValue.charAt(0);
char yy = attrValue.charAt(1);
current.addForce(xx - 'a', yy - 'a', GoPlayer.WHITE);
} else if (attrName.equals("AE")) {
if (attrValue.length() != 2) {
return;
}
char xx = attrValue.charAt(0);
char yy = attrValue.charAt(1);
current.addErase(xx - 'a', yy - 'a', GoPlayer.UNKNOWN);
} else if (attrName.equals("C")) {
current.setComment(attrValue);
}
else if (attrName.equals("CR")) {
if (attrValue.length() != 2) {
return;
}
char xx = attrValue.charAt(0);
char yy = attrValue.charAt(1);
current.addTip(xx - 'a', yy - 'a', GoPoint.CIRCLE);
} else if (attrName.equals("TR")) {
if (attrValue.length() != 2) {
return;
}
char xx = attrValue.charAt(0);
char yy = attrValue.charAt(1);
current.addTip(xx - 'a', yy - 'a', GoPoint.TRIANGLE);
} else if (attrName.equals("LB")) {
if (attrValue.length() != 4) {
return;
}
char xx = attrValue.charAt(0);
char yy = attrValue.charAt(1);
char tip = attrValue.charAt(3);
current.addTip(xx - 'a', yy - 'a', tip);
} else if (attrName.equals("SQ")) {
if (attrValue.length() != 4) {
return;
}
char xx = attrValue.charAt(0);
char yy = attrValue.charAt(1);
current.addTip(xx - 'a', yy - 'a', GoPoint.SQUARE);
}
else if (attrName.equals("SZ")) {
match.boardSize = Integer.parseInt(attrValue);
} else if (attrName.equals("PB")) {
match.blackName = attrValue;
} else if (attrName.equals("PW")) {
match.whiteName = attrValue;
} else if (attrName.equals("BR")) {
match.blackRank = attrValue;
} else if (attrName.equals("WR")) {
match.whiteRank = attrValue;
} else if (attrName.equals("EV")) {
match.matchName = attrValue;
} else if (attrName.equals("DT")) {
match.matchDate = attrValue;
} else if (attrName.equals("PC")) {
match.place = attrValue;
} else if (attrName.equals("RE")) {
match.result = attrValue;
} else if (attrName.equals("SO")) {
match.source = attrValue;
} else if (attrName.equals("TM")) {
match.time = attrValue;
}
}
protected static int getCharIndex(byte[] buf, int len, int fromIndex, char c) {
for (int i = fromIndex; i < len; i++) {
if (((char) buf[i]) == c) {
return i;
}
}
return -1;
}
/*
* class SGFContext { byte[] buf; int len; int startIndex;
* }
*/
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?