Changeset - e7fafcf5b27a
[Not reviewed]
default
4 0 8
Laman - 9 years ago 2016-08-14 12:24:24

started testing
8 files changed with 65 insertions and 13 deletions:
0 comments (0 inline, 0 general)
src/diana/__init__.py
Show inline comments
 
new file 100644
src/diana/diana.py
Show inline comments
 
file renamed from src/diana.py to src/diana/diana.py
 
import sys
 
import os
 
import re
 
import go
 
import os
 
import sgf
 
import sys
 
 
import diana.sgf as sgf
 
import diana.go as go
 
 
if len(sys.argv)>1:
 
	files=sys.argv[1:]
 
else:
 
	sys.exit("no input file specified")
 
 
movesPerDiagram=75
 
minMovesPerDiagram=10
 
 
t=sys.stdout
 
c=28
 
padding=15
 
highNumbers=True
 
 
class Svg:
 
	content=""
 
	footer=""
 
	extension="svg"
 
 
	padding=15
 
	gridSize=28
 
	highNumbers=True
 
	
 
	def __init__(self):
 
		self.content='''<?xml version="1.0" standalone="no"?>
 
<svg width="{0}" height="{0}" version="1.1" xmlns="http://www.w3.org/2000/svg" alignment-baseline="center">
 
	<defs>
 
		<style type="text/css"><![CDATA[
 
		text{{font-family:"DejaVu Sans";text-anchor:middle;}}
 
		line{{stroke:black;stroke-width:0.7}}
 
		circle{{stroke:black}}
 
		.b{{fill:black}}
 
		.w{{fill:white}}
 
		]]></style>
 
	</defs>
 
	<rect width="{0}" height="{0}" x="0" y="0" style="fill:white;stroke:white"/>\n'''.format(2*self.padding+18*self.gridSize)
 
		self.footer="</svg>\n"
 
 
		grid='  <line x1="{0}" x2="{1}" y1="{2}" y2="{3}" />\n'
 
 
		# okraje desky
 
		for i in (0,18):
 
			self.content+='  <line x1="{0}" x2="{1}" y1="{2}" y2="{3}" style="stroke-width:1"/>\n'.format(self.padding, 18*self.gridSize+self.padding, self.gridSize*i+self.padding, self.gridSize*i+self.padding)
 
			self.content+='  <line x1="{0}" x2="{1}" y1="{2}" y2="{3}" style="stroke-width:1"/>\n'.format(self.gridSize*i+self.padding, self.gridSize*i+self.padding, self.padding, 18*self.gridSize+self.padding)
 
		
 
		# mřížka
 
		for i in range(1,18):
 
			self.content+=grid.format(padding, 18*c+padding, c*i+padding, c*i+padding)
 
@@ -87,97 +88,97 @@ class Svg:
 
 
 
class Tikz:
 
	content=""
 
	footer=""
 
	extension="tex"
 
	
 
	highNumbers=True
 
	
 
	def __init__(self):
 
		self.content=r'''\begin{tikzpicture}
 
	\draw[step=\boardSquare,gray,very thin] (0,0) grid (18\boardSquare,18\boardSquare);
 
	\draw (0,0) rectangle (18\boardSquare,18\boardSquare);
 
	
 
	'''
 
		
 
		# hvězdy
 
		for i in range(3):
 
			for j in range(3):
 
				self.content+=r'''  \filldraw[fill=black] ({0}\boardSquare,{1}\boardSquare) circle[radius=0.04];'''.format(6*i+3, 6*j+3)+'\n'
 
			self.content+='\n'
 
	
 
		self.footer=r'\end{tikzpicture}' '\n'
 
			
 
	def __str__(self):
 
		return self.content+self.footer
 
 
	def drawStone(self,x,y,color):
 
		fill="black" if color=="b" else "white"
 
		self.content+=r'  \filldraw[draw=black,fill={0}] ({1}\boardSquare,{2}\boardSquare) circle[radius=0.5\boardSquare];'.format(fill,x,18-y)+'\n'
 
	
 
	def drawMove(self,x,y,label,color):
 
		fill="black" if color=="b" else "white"
 
		labelColor="white" if color=="b" else "black"
 
		if (not self.highNumbers) and isinstance(label,int) and label%100!=0: label=label%100 # dost neobratná logika
 
		
 
		self.content+=r'  \filldraw[draw=black,fill={0}] ({1}\boardSquare,{2}\boardSquare) circle[radius=0.5\boardSquare] node[color={3}]{{{4}}};'.format(fill,x,18-y,labelColor,label)+'\n'
 
		
 
	def getContent(self):
 
		return self.content+self.footer
 
 
 
def processFile(fileName):
 
	shortName="".join(re.split(r'[/\\]',fileName)[-1].split('.')[:-1])
 
	
 
	game=go.Go()
 
	global t
 
	
 
	record=sgf.Sgf(open(fileName,'r',encoding="utf-8").read())
 
	record= sgf.Sgf(open(fileName, 'r', encoding="utf-8").read())
 
	moves=record.getMoves()
 
 
	localBoard=dict()
 
	overlays=""
 
	letters=dict()
 
	letter='a'
 
 
	diagramsNeeded=(len(moves)-minMovesPerDiagram)//movesPerDiagram+1
 
	moveNumber=0
 
	
 
	for i in range(diagramsNeeded):
 
		# inicializuj diagram
 
		diagram=Tikz()
 
		
 
		for lineNumber,line in enumerate(game.board):
 
			for itemNumber,item in enumerate(line):
 
				if item==1: diagram.drawStone(itemNumber,lineNumber,"b")
 
				if item==-1: diagram.drawStone(itemNumber,lineNumber,"w")
 
			localBoard={(a,b):game.board[b][a]-1 for a in range(19) for b in range(19) if game.board[b][a]!=0}
 
		
 
		for j in range(movesPerDiagram):
 
			# kresli tahy
 
			if moveNumber>=len(moves): break
 
			
 
			c,(x,y)=moves[moveNumber]
 
			c=c.lower()
 
			
 
			if not game.move(1 if c=='b' else -1,x,y):
 
				print("illegal move: {0} at {1},{2}".format(moveNumber+1,x,y))
 
				moveNumber+=1
 
				continue
 
			
 
			# zapíšu tah na volný průsečík
 
			if not (x,y) in localBoard:
 
				localBoard[(x,y)]=moveNumber+1
 
				diagram.drawMove(x,y,moveNumber+1,c)
 
			# průsečík je obsazený nepopsaným kamenem
 
			elif localBoard[(x,y)]<1:
 
				# průsečík není popsaný ani písmenem
 
				if not (x,y) in letters:
 
					letters[(x,y)]=letter
 
					color='b' if localBoard[(x,y)]==0 else 'w'
 
					diagram.drawMove(x,y,letter,color)
 
					letter=chr(ord(letter)+1)
 
				overlays+="{0} = {1}\n".format(moveNumber+1,letters[(x,y)])
 
			# průsečík je obsazený očíslovaným kamenem
 
			else: overlays+="{0} = {1}\n".format(moveNumber+1,localBoard[(x,y)])
 
			
src/diana/go.py
Show inline comments
 
file renamed from src/go.py to src/diana/go.py
src/diana/sgf.py
Show inline comments
 
file renamed from src/sgf.py to src/diana/sgf.py
 
@@ -445,99 +445,96 @@ def propValue(str):
 
 
class propValue:
 
	pass
 
 
def cValueType(str,start):
 
	matches=[real,number,double,color,simpleText]
 
	for f in matches:
 
		i,x=f(str,start)
 
		if x is not None: return i,x
 
	return 1/0
 
 
def number(str,start):
 
	r=re.compile(r"(\+|-|)\d+")
 
	m=r.match(str,start)
 
	if m is None: return (-1,None)
 
	x=int(m.group(0))
 
	return (m.end(),x)
 
 
def real(str):
 
	m=re.match(r"(\+|-|)\d+(\.\d+)?",str)
 
	if m is None: return -1
 
	return m.end()
 
 
def double(str):
 
	m=re.match(r"1|2",str)
 
	if m is None: return -1
 
	return m.end()
 
	
 
def color(str):
 
	m=re.match(r"B|W",str)
 
	if m is None: return -1
 
	return m.end()
 
	
 
def simpleText(str):
 
	res=r""
 
	esc=False
 
	for c in str:
 
		if esc:
 
			res+=c
 
			esc=False
 
		elif c=="\\":
 
			esc=True
 
		elif c=="]":
 
			break
 
		else:
 
			res+=c
 
	return res"""
 
 
sgf=open("in/1-Hora-Simara.sgf").read()
 
 
x=Collection(sgf)
 
 
# TODO:
 
# date
 
 
 
"""
 
# move
 
B	move
 
KO	none
 
MN	number
 
W	move
 
 
# setup
 
AB	list of stone
 
AE	list of point
 
AW	list of stone
 
PL	color
 
 
# node annotation
 
C	text
 
DM	double
 
GB	double
 
GW	double
 
HO	double
 
N	simpleText
 
UC	double
 
V	real
 
 
# move annotation
 
BM	double
 
DO	none
 
IT	none
 
TE	double
 
 
# markup
 
AR	list of composed point:point
 
CR	list of point
 
DD	elist of point
 
LB	list of composed point:simpleText
 
LN	list of composed point:point
 
MA	list of point
 
SL	list of point
 
SQ	list of point
 
TR	list of point
 
 
# root
 
AP	composed simpleText:simpleText
 
CA	simpleText
src/diana/sgfParser.py
Show inline comments
 
file renamed from src/sgfParser.py to src/diana/sgfParser.py
 
import re
 
	
 
 
def skipWhitespace(str,start):
 
	i=start
 
	while i<len(str) and str[i].isspace(): i+=1
 
	return i
 
 
class Collection:
 
	gameTrees=[]
 
	
 
	def __init__(self,str):
 
		self.gameTrees=[]
 
		i,x=GameTree.create(str,0)
 
		if x is None:
 
			print("error when parsing Collection")
 
			return
 
		while x is not None:
 
			self.gameTrees.append(x)
 
			i,x=GameTree.create(str,i)
 
	
 
class GameTree:
 
	nodes=[]
 
	branches=[]
 
	
 
	# def __init__(self,str,start):
 
		# self.nodes=[]
 
		# self.branches=[]
 
		# if str[start]!="(":
 
			# print("error when parsing GameTree")
 
			# return (-1,None)
 
		# i,x=Node(str,start+1)
 
		# if x is None:
 
			# print("error when parsing GameTree")
 
			# return (-1,None)
 
		# while x is not None:
 
			# self.nodes.append(x)
 
			# i,x=Node(str,i)
 
		# if str[i]!=")":
 
			# print("error when parsing GameTree")
 
			# return (-1,None)
 
		# return (i+1,self)
 
		
 
	def create(str,start):
 
		res=GameTree()
 
		i=skipWhitespace(str,start)
 
		if i>=len(str) or str[i]!="(":
 
			# print("error when parsing GameTree")
 
			return (start,None)
 
		i,x=Node.create(str,start+1)
 
		if x is None:
 
@@ -385,100 +385,96 @@ def propValue(str):
 
 
class propValue:
 
	pass
 
 
def cValueType(str,start):
 
	matches=[real,number,double,color,simpleText]
 
	for f in matches:
 
		i,x=f(str,start)
 
		if x is not None: return i,x
 
	return 1/0
 
 
def number(str,start):
 
	r=re.compile(r"(\+|-|)\d+")
 
	m=r.match(str,start)
 
	if m is None: return (-1,None)
 
	x=int(m.group(0))
 
	return (m.end(),x)
 
 
def real(str):
 
	m=re.match(r"(\+|-|)\d+(\.\d+)?",str)
 
	if m is None: return -1
 
	return m.end()
 
 
def double(str):
 
	m=re.match(r"1|2",str)
 
	if m is None: return -1
 
	return m.end()
 
	
 
def color(str):
 
	m=re.match(r"B|W",str)
 
	if m is None: return -1
 
	return m.end()
 
	
 
def simpleText(str):
 
	res=r""
 
	esc=False
 
	for c in str:
 
		if esc:
 
			res+=c
 
			esc=False
 
		elif c=="\\":
 
			esc=True
 
		elif c=="]":
 
			break
 
		else:
 
			res+=c
 
	return res"""
 
 
sgf=open("in/1-Hora-Simara.sgf").read()
 
 
x=Collection(sgf)
 
 
# TODO:
 
# date
 
 
 
"""
 
# move
 
B	move
 
KO	none
 
MN	number
 
W	move
 
 
# setup
 
AB	list of stone
 
AE	list of point
 
AW	list of stone
 
PL	color
 
 
# node annotation
 
C	text
 
DM	double
 
GB	double
 
GW	double
 
HO	double
 
N	simpleText
 
UC	double
 
V	real
 
 
# move annotation
 
BM	double
 
DO	none
 
IT	none
 
TE	double
 
 
# markup
 
AR	list of composed point:point
 
CR	list of point
 
DD	elist of point
 
LB	list of composed point:simpleText
 
LN	list of composed point:point
 
MA	list of point
 
SL	list of point
 
SQ	list of point
 
TR	list of point
 
 
# root
 
AP	composed simpleText:simpleText
 
CA	simpleText
 
FF	number
src/diana/tests/__init__.py
Show inline comments
 
new file 100644
src/diana/tests/data/simple.sgf
Show inline comments
 
new file 100755
 
(;SZ[19]FF[3]
 
PW[Kobayashi Koichi]
 
WR[9d]
 
PB[Kato Masao]
 
BR[9d]
 
EV[15th Kisei Final]
 
RO[Game 1]
 
DT[1991-01-18,19]
 
PC[Caesar Park Hotel, Sao Paulo]
 
KM[5.5]
 
RE[B+1.5]
 
US[GoGoD95]
 
;B[qd];W[dp];B[cd];W[qp];B[oc];W[ed];B[ec];W[fc];B[dc];W[gd];B[cf];W[kc]
 
;B[oq];W[lp];B[oo];W[pn];B[pl];W[on];B[mo];W[nm];B[lo];W[jp];B[kp];W[kq]
 
;B[ko];W[iq];B[pi];W[pq];B[lq];W[lr];B[mq];W[qg];B[hc];W[gb];B[jc];W[jd]
 
;B[kd];W[lc];B[je];W[id];B[hd];W[ie];B[he];W[ic];B[ib];W[if];B[hb];W[jb]
 
;B[hf];W[ff];B[ig];W[jf];B[gh];W[fh];B[fi];W[gg];B[hg];W[gi];B[hh];W[eh]
 
;B[fj];W[di];B[dj];W[cj];B[ck];W[dk];B[ej];W[ci];B[cl];W[dg];B[fp];W[cn]
 
;B[em];W[eb];B[db];W[dm];B[dl];W[en];B[hp];W[fm];B[el];W[gn];B[fo];W[fn]
 
;B[jo];W[ip];B[ho];W[hj];B[hk];W[ik];B[hl];W[il];B[hm];W[hn];B[io];W[jj]
 
;B[gj];W[hi];B[kh];W[im];B[fl];W[da];B[ca];W[ea];B[ga];W[fb];B[ee];W[de]
 
;B[dd];W[fd];B[df];W[ef];B[bb];W[oi];B[mi];W[lh];B[ki];W[lk];B[lg];W[oh]
 
;B[nl];W[ok];B[ol];W[mj];B[md];W[mf];B[ng];W[mg];B[mh];W[lf];B[nh];W[nf]
 
;B[og];W[ph];B[nj];W[nk];B[ml];W[mk];B[of];W[kg];B[li];W[pj];B[rf];W[qf]
 
;B[qe];W[mm];B[in];W[dr];B[ll];W[lm];B[kl];W[km];B[kk];W[kj];B[lj];W[jl]
 
;B[rj];W[ri];B[qm];W[qk];B[rk];W[qj];B[ql];W[sj];B[rl];W[rg];B[rn];W[po]
 
;B[sm];W[rp];B[ro];W[jr];B[hr];W[ir];B[ls];W[ms];B[mr];W[ks];B[ns];W[nr]
 
;B[ls];W[jg];B[jh];W[ms];B[nn];W[om];B[ls];W[ii];B[ih];W[ms];B[cp];W[co]
 
;B[ls];W[so];B[sk];W[ms];B[jm];W[jn];B[ls];W[gl];B[gk];W[ms];B[do];W[cq]
 
;B[ls];W[nd];B[pr];W[qr];B[op];W[pp];B[ne];W[or];B[me];W[nb];B[ob];W[ld]
 
;B[kr];W[nc];B[od];W[lr];B[bg];W[ch];B[kr];W[gr];B[hq];W[lr];B[nq];W[ms]
 
;B[os];W[rr];B[ls];W[is];B[kr];W[hs];B[fr];W[lr];B[ji];W[ij];B[kr];W[fs]
 
;B[es];W[er];B[gs];W[lr];B[fs];W[kr];B[mc];W[mb];B[re];W[le];B[oe];W[bm]
 
;B[bl];W[eo];B[ni];W[qi];B[si];W[sg];B[eq];W[dq];B[bh];W[bi];B[cm];W[an]
 
;B[gm];W[fe];B[cg];W[ei];B[ds];W[cs];B[fq];W[lb];B[na];W[ma];B[oa];W[dn]
 
;B[gf];W[fg];B[sf];W[ce];B[be];W[ee];B[ah];W[mn];B[no];W[pf];B[pe];W[go]
 
;B[gp];W[qs];B[al];W[ps];B[am];W[bn];B[ep];W[br];B[or];W[sh];B[ja];W[ka]
 
;B[ia];W[lg];B[sj];W[ms]
 
)
src/diana/tests/sgfParser.py
Show inline comments
 
new file 100644
 
import unittest
 
from unittest import TestCase
 
import os
 

	
 
from ..sgfParser import Collection
 

	
 

	
 
dataDir=os.path.join(os.path.dirname(__file__), "data")
 

	
 
class TestCollection(TestCase):
 
	def testEmptySgf(self):
 
		Collection("(;)")
 

	
 
	def testSimpleSgf(self):
 
		with open(os.path.join(dataDir, "simple.sgf")) as f:
 
			Collection(f.read())
 

	
 
if __name__ == '__main__':
 
	unittest.main()
0 comments (0 inline, 0 general)