📄 sgfadapter1.java
字号:
package org.nebula.games.go;
import java.io.*;
import java.util.StringTokenizer;
import java.util.Stack;
/***********************************************************************************
* *
* SGF Go File Properties *
* *
***********************************************************************************/
/*
* Move Properties B, KO, MN, W
* /
static final int MV_BLACK = 101; //B[]
static final int MV_WHITE = 102; //W[]
// static final int MV_FORCE = 103; //KO[]
// static final int MV_MARKNUM = 104; //MN[]
/*
* Setup Properties AB, AE, AW, PL
* /
static final int AD_BLACK = 201; //AB[]
static final int AD_WHITE = 202; //AW[]
static final int AD_EMPTY = 203; //AE[]
// static final int AD_PLAYER = 204; //PL[]
/*
* Node Annotation Properties C, DM, GB, GW, HO, N, UC, V
* /
static final int NA_COMMENT = 301; //C[]
// static final int NA_DEUCE = 302; //DM[]
// static final int NA_BGOOD = 303; //GB[]
// static final int NA_WGOOD = 304; //GW[]
// static final int NA_HOTSPOT = 305; //HO[]
// static final int NA_NODENAME = 306; //N[]
// static final int NA_UNCLEAR = 307; //UC[]
// static final int NA_VALUE = 308; //V[]
/*
* Move Annotation Properties BM, DO, IT, TE
* /
// static final int MA_BADMOVE = 401; //BM[]
// static final int MA_DOUBTFUL = 402; //DO[]
// static final int MA_INTEREST = 403; //IT[]
// static final int MA_TESUJI = 404; //TE[] --good move
/*
* Markup Properties AR, CR, DD, LB, LN, MA, SL, SQ, TR
* /
static final int MK_CIRCLE = 501; //CR[]
static final int MK_LABEL = 502; //LB[]
static final int MK_CROSS = 503; //MA[]
static final int MK_SQURE = 504; //SQ[]
static final int MK_TRIANGLE = 505; //TR[]
// static final int MK_LINE = 506; //LN[] ---Point:Point
// static final int MK_SELECT = 507; //SL[]
// static final int MK_ARROW = 508; //AR[] ---Point:Point
// static final int MK_DIM = 509; //DD[] ---gray out points
/*
* Root Properties AP, CA, FF, GM, ST, SZ
* /
static final int RT_SIZE = 601; //SZ[]
// static final int RT_APPLICATION = 602; //AP[]
// static final int RT_CHARSET = 603; //CA[]
// static final int RT_FILEFORMAT = 604; //FF[]
// static final int RT_GAMETYPE = 605; //GM[]
// static final int RT_SHOWTYPE = 606; //ST[]
/*
* Game Info Properties AN, BR, BT, CP, DT, EV, GN, GC, ON, OT, PB, PC, PW, RE, RO, RU, SO, TM, US, WR, WT
* /
static final int GM_BLACK = 701; //PB[]
static final int GM_WHITE = 702; //PW[]
static final int GM_BLACKRANK = 703; //BR[]
static final int GM_WHITERANK = 704; //WR[]
static final int GM_EVENT = 705; //EV[]
static final int GM_DATE = 706; //DT[]
static final int GM_PLACE = 707; //PC[]
static final int GM_RESULT = 708; //RE[]
static final int GM_SOURCE = 709; //SO[]
static final int GM_TIME = 710; //TM[]
// static final int GM_WHITETEAM = 0; //WT[]
// static final int GM_USER = 0; //US[]
// static final int GM_ANNOTATOR = 0; //AN[]
// static final int GM_BLACKTEAM = 0; //BT[]
// static final int GM_COPYRIGHT = 0; //CP[]
// static final int GM_GAMENAME = 0; //GN[]
// static final int GM_GAMECONTEXT = 0; //GC[]
// static final int GM_OPENING = 0; //ON[]
// static final int GM_OVERTIME = 0; //OT[]
// static final int GM_ROUND = 0; //RO[]
// static final int GM_RULE = 0; //RU[]
/*
* Timing Properties BL, OB, OW, WL
* /
// static final int TM_BLACKLEFT = 0; //BL[]
// static final int TM_BMOVELEFT = 0; //OB[]
// static final int TM_WMOVELEFT = 0; //OW[]
// static final int TM_WHITELEFT = 0; //WL[]
/*
* Miscellaneous Properties FG, PM, VW
* /
// static final int MS_FIGURE = 0; //FG[]
// static final int MS_PRINT = 0; //PM[]
// static final int MS_VIEW = 0; //VW[]
*************************************************************************************/
public final class SgfAdapter1 {
public static void writeMatch(Match mt, OutputStream output) {
try {
PrintWriter out = new PrintWriter(output);
// write movements
out.print("(;" + movementTreeToString(mt.getRootMovement()));
out.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
public static Match readMatch(InputStream input) {
Stack stack = new Stack();
Match match = new Match();
Movement current = null;
Movement last = null;
String attr_name = "";
StringBuffer buf = new StringBuffer("");
// DataInputStream dinput = new DataInputStream(input);
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
try {
String source = reader.readLine();
int pos = 0;
boolean readingValue = false;
while(true) {
if (pos == source.length()) {
source = reader.readLine();
source += "\n";
pos = 0;
}
char c = source.charAt(pos);
pos++;
boolean stop = false;
int lc;
switch (c) {
case '(':
if (readingValue) {
buf.append(c);
break;
}
buf.setLength(0);
if (current != null) {
System.out.println("forking:");
stack.push(current);
}
break;
case ';':
if (readingValue) {
buf.append(c);
break;
}
if (current != null) {
last = current;
current = null;
}
break;
case '[':
String cid = (buf.toString()).trim();
if (!cid.equals("")) {
attr_name = cid;
}
buf.setLength(0);
stack.push(new Integer(c));
readingValue = true;
break;
case ']':
lc = (char) (((Integer)stack.pop()).intValue());
if (lc != '[') {
throw new SgfException("invalid format");
}
if (current == null) {
current = new Movement();
if (last != null) {
last.add(current);
}
}
String attr_value = (buf.toString()).trim();
parseAttribute(match, current, attr_name, attr_value);
buf.setLength(0);
readingValue = false;
break;
case ')':
if (readingValue) {
buf.append(c);
break;
}
if (!stack.empty()) {
System.out.println("closing fork:");
current = (Movement) stack.pop();
}
else {
stop = true;
}
break;
default:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -