围棋棋谱一般被保存为sgf格式,要想在自己的网站中实现打谱功能,必要要会解析sgf文件,取出里面的对局信息和落子,楼主现在用一种比较简单的方法来解析它
说明:楼主所用的棋谱是从新浪围棋里下载来的,示例用的是第10届中国围棋龙星战半决赛江维杰VS辜梓豪
文件内容如下(其他sgf格式也差不多,就是有些字段的名字换了一下)
(
TE[第10届中国围棋龙星战半决赛]
RD[2019-05-28]
PC[中国]
TM[0]
LT[30]
LC[10]
KO[7.5]
RE[黑中盘胜]
PB[江维杰]
BR[九段]
PW[辜梓豪]
WR[九段]
GK[1]
TC[]
;B[pd];W[dp];B[qp];W[dd];B[np];W[qc];B[qd];W[pc];B[nc];W[oc];B[od];W[nb];B[fq];W[hq]
;B[cc];W[cd];B[dc];W[ec];B[eb];W[fb];B[fc];W[ed];B[gb];W[db];B[fa];W[cb];B[fo];W[dm]
;B[jq];W[jp];B[kp];W[ip];B[ko];W[kq];B[kr];W[lq];B[ir];W[pp];B[po];W[qo];B[pq];W[hr]
;B[mq];W[in];B[bc];W[bb];B[er];W[dq];B[gn];W[il];B[gl];W[dk];B[cr];W[dr];B[ds];W[bq]
;B[el];W[dl];B[en];W[ij];B[fj];W[di];B[qn];W[fh];B[jk];W[ik];B[im];W[jm];B[hm];W[jo]
;B[jl];W[kn];B[lp];W[kl];B[kk];W[ll];B[lk];W[lo];B[lr];W[mk];B[ki];W[nl];B[hi];W[ni]
;B[mm];W[ml];B[jh];W[mc];B[nd];W[rc];B[pi];W[pk];B[ef];W[fd];B[lm];W[km];B[cg];W[ff]
;B[co];W[cn];B[cp];W[cq];B[do];W[bo];B[bk];W[bl];B[ci];W[gj];B[dh];W[gi];B[dj];W[ei]
;B[ej];W[ck];B[hh];W[gk];B[fg];W[eg];B[gg];W[eh];B[gh];W[fi];B[df];W[dg];B[ch];W[cf]
;B[bf];W[ce];B[fe];W[gf];B[ge];W[hf];B[ee];W[ig];B[hj];W[hk];B[ii];W[hc];B[gd];W[gc]
;B[hd];W[id];B[ic];W[fb];B[ie];W[jd];B[fc];W[hb];B[he];W[je];B[if];W[jf];B[hg];W[ng]
;B[qg];W[qj];B[md];W[lc];B[ob];W[mb];B[rd])
需要的属性说明如下
[TE]:比赛标题
[RD]比赛时间
[PC]比赛地点
[KO]贴目
[RE]比赛结果
[PB]黑方姓名
[BR]黑方水平(职业)
[PW]白方名字
[WR]白方水平
;B[]黑方落子坐标
;W[]白棋落子坐标
实现
1.先创建一个落子类,属性是落子的颜色、x坐标,与坐标以及步数
public class Point {
private String color;
private int x;
private int y;
private int step;
public Point() {
super();
// TODO Auto-generated constructor stub
}
public Point(String color, int x, int y,int step) {
super();
this.color = color;
this.x = x;
this.y = y;
this.step = step;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getStep() {
return step;
}
public void setStep(int step) {
this.step = step;
}
}
2.创建棋谱类,记录所有信息
//棋谱
public class ChessManual {
private int manualid;
private String title;// 比赛
private String time;// 比赛时间
private String dict;// 地点
private float tiemu;// 贴目
private String result;// 结果
private String blackName;// 黑方
private String blacklevel;// 黑方水平
private String whiteName;// 白方
private String whitelevel;// 白方水平
private List<Point> playList;// 落子顺序
public ChessManual() {
super();
// TODO Auto-generated constructor stub
}
public ChessManual(String title, String time, String dict, float tiemu, String result, String blackName,
String blacklevel, String whiteName, String whitelevel) {
super();
this.title = title;
this.time = time;
this.dict = dict;
this.tiemu = tiemu;
this.result = result;
this.blackName = blackName;
this.blacklevel = blacklevel;
this.whiteName = whiteName;
this.whitelevel = whitelevel;
}
public ChessManual(int manualid, String title, String time, String dict, float tiemu, String result,
String blackName, String blacklevel, String whiteName, String whitelevel) {
super();
this.manualid = manualid;
this.title = title;
this.time = time;
this.dict = dict;
this.tiemu = tiemu;
this.result = result;
this.blackName = blackName;
this.blacklevel = blacklevel;
this.whiteName = whiteName;
this.whitelevel = whitelevel;
}
public int getManualid() {
return manualid;
}
public void setManualid(int manualid) {
this.manualid = manualid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getDict() {
return dict;
}
public void setDict(String dict) {
this.dict = dict;
}
public float getTiemu() {
return tiemu;
}
public void setTiemu(float tiemu) {
this.tiemu = tiemu;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public String getBlackName() {
return blackName;
}
public void setBlackName(String blackName) {
this.blackName = blackName;
}
public String getBlacklevel() {
return blacklevel;
}
public void setBlacklevel(String blacklevel) {
this.blacklevel = blacklevel;
}
public String getWhiteName() {
return whiteName;
}
public void setWhiteName(String whiteName) {
this.whiteName = whiteName;
}
public String getWhitelevel() {
return whitelevel;
}
public void setWhitelevel(String whitelevel) {
this.whitelevel = whitelevel;
}
public L