15年前倒是自己用 python 2. 写过一个程序,能把 sgf 文件的问题图 转换成 txt 文本,源代码分享给大家,
现在 用 python 3. 了,有些地方要改一下才能运行了。
import dircache
from os import *
m = map(None, 'abcdefghijklmnopqrs', range(1,20))
m2 = map(None, 'srqponmlkjihgfedcba', range(1,20))
EMPTY_ROW=2
EMPTY_COL=3
def convert(sgfGame,txtGame,prb):
# x="ab" pos0=1,pos1=2
moveList=[]
# black or white stone list
i=0
whoMove='B'
while (i<len(sgfGame)):
while not (sgfGame[i]=='A' and ( sgfGame[i+1]=='B' or sgfGame[i+1]=='W') ):
i=i+1
if i >len(sgfGame)-2:
break
if i >= len(sgfGame)-2:
break
color=sgfGame[i+1]
i=i+2
while( i<len(sgfGame)-3 and sgfGame[i]=='[' and sgfGame[i+3]==']' ):
moveList.append(sgfGame[i+1:i+3]+color)
i=i+4
if sgfGame[i]=='\x0d':
i=i+2
if i >= len(sgfGame)-3:
break
i=0
while (i<len(sgfGame)-3 and sgfGame[i:i+2]<>';B' and sgfGame[i:i+2]<>';W' ):
i=i+1
if sgfGame[i+1]=='B':
whoMove='B'
elif sgfGame[i+1]=='W':
whoMove='W'
else:
print('error'+sgfGame[i+1]+':'+prb)
i=0
while (i<len(sgfGame)-3 and sgfGame[i:i+2]<>'AE' ):
i=i+1
if sgfGame[i:i+2]=='AE':
print ('error AE AE AE _______:'+prb)
emptyRow=1
emptyCol=1
mset='none'
pos0_sum=0
pos1_sum=0
pos_count=0
for x in moveList:
pos0 = filter(lambda z, x=x: z[0]==x[0], m)[0][1]
pos1 = filter(lambda z, x=x: z[0]==x[1], m)[0][1]
pos0_sum=pos0_sum+pos0
pos1_sum=pos1_sum+pos1
pos_count=pos_count+1
pos0_avg=pos0_sum/pos_count
pos1_avg=pos1_sum/pos_count
if pos1_avg<10:
if pos0_avg<10:
mset='tl'
else:
mset='tr'
else:
if pos0_avg<10:
mset='dl'
else:
mset='dr'
if mset=='dr':
pos1_avg=20-pos1_avg
pos0_avg=20-pos0_avg
if mset=='dl':
pos1_avg=20-pos1_avg
if mset=='tr':
pos0_avg=20-pos0_avg
for x in moveList:
pos0 = filter(lambda z, x=x: z[0]==x[0], m)[0][1]
pos1 = filter(lambda z, x=x: z[0]==x[1], m)[0][1]
if mset=='dr':
pos1=20-pos1
pos0=20-pos0
if mset=='dl':
pos1=20-pos1
if mset=='tr':
pos0=20-pos0
if pos0_avg<pos1_avg:
posp=pos0
pos0=pos1
pos1=posp
if pos1>emptyRow:
emptyRow=pos1
if pos0>emptyCol:
emptyCol=pos0
pos=((pos1-1)*20+pos0-1)*2
if (x[2] == 'B' and whoMove =='B') or (x[2] == 'W' and whoMove =='W'):
stone='\xa1\xf1'
elif (x[2] == 'B' and whoMove =='W') or (x[2] == 'W' and whoMove =='B'):
stone='\xa1\xf0'
else:
print 'error!!!!!'
t=txtGame[0:pos]+stone+txtGame[pos+2:]
txtGame=t
return (txtGame,emptyRow,emptyCol)
dir=dircache.listdir('tlt/')
emptyGame=file("board.txt","rb")
emptyBoard=emptyGame.read()
txtFile=file("tlt.txt","w+b")
for x in dir:
txtGame=emptyBoard
string_x=str(x)
sgfFile=file("tlt/"+str(x),"rb")
sgfGame=sgfFile.read()
#txtGame=convert(sgfGame,txtGame)
(txtGame,emptyRow,emptyCol)=convert(sgfGame,txtGame,x)
endRow=9
emptyCol=14
for i_row in range(0,endRow):
end=emptyCol*2
# txtFile.write(txtGame[i_row*40:i_row*40+end])
if i_row==endRow-1:
txtFile.write(txtGame[i_row*40:i_row*40+end-4]+string_x[1:5])
else:
txtFile.write(txtGame[i_row*40:i_row*40+end])
txtFile.write('\x20\x0d\x0a')
txtFile.write('\x0d\x0a')
sgfFile.close()
txtFile.close()
|