|
| 1 | +from functools import partial |
| 2 | +from copy import deepcopy as copy |
| 3 | + |
| 4 | +space=10 |
| 5 | + |
| 6 | +fuzzy=0.0 |
| 7 | + |
| 8 | + |
| 9 | +from random import random |
| 10 | + |
| 11 | +from Tkinter import Canvas |
| 12 | + |
| 13 | +def p2l(i,j,u,v,dim): |
| 14 | + if i==u: |
| 15 | + k=0 |
| 16 | + else: |
| 17 | + k=1 |
| 18 | + |
| 19 | + return dim*min([i,j],[u,v])[0]+min([i,j],[u,v])[1]+k*dim*dim |
| 20 | + |
| 21 | + |
| 22 | +class Goban(Canvas): |
| 23 | + def __init__(self,dim,**kwargs): |
| 24 | + self.dim=dim |
| 25 | + self.space=space |
| 26 | + Canvas.__init__(self,**kwargs) |
| 27 | + self.prepare_mesh() |
| 28 | + |
| 29 | + def prepare_mesh(self): |
| 30 | + f=fuzzy |
| 31 | + self.mesh=[[[0.0,0.0] for row in range(self.dim)] for col in range(self.dim)] |
| 32 | + if f>0: |
| 33 | + for i in range(self.dim): |
| 34 | + f1=random()*f-f/2 |
| 35 | + f2=random()*f-f/2 |
| 36 | + for j in range(self.dim): |
| 37 | + self.mesh[i][j][1]=f1+random()*.08-.04 |
| 38 | + self.mesh[j][i][0]=f2+random()*.08-.04 |
| 39 | + |
| 40 | + |
| 41 | + def ij2xy(self,i,j): |
| 42 | + space=self.space |
| 43 | + dim=self.dim |
| 44 | + y=(0.5+dim-i)*space |
| 45 | + x=(0.5+1.+j)*space |
| 46 | + return x,y |
| 47 | + |
| 48 | + def xy2ij(self,x,y): |
| 49 | + dim=self.dim |
| 50 | + space=self.space |
| 51 | + return int(round(0.5+dim-1.*y/space)),int(round(1.*x/space-0.5)-1) |
| 52 | + |
| 53 | + def draw_point(self,i,j,diameter,color="black",outline="black",width=1): |
| 54 | + space=self.space |
| 55 | + x,y=self.ij2xy(i,j) |
| 56 | + radius=diameter*space/2 |
| 57 | + oval=self.create_oval(x-radius,y-radius,x+radius,y+radius,fill=color,outline=outline,width=width) |
| 58 | + return oval |
| 59 | + |
| 60 | + def draw_line(self,i1,j1,i2,j2,color="black",width=1): |
| 61 | + x1,y1=self.ij2xy(i1,j1) |
| 62 | + x2,y2=self.ij2xy(i2,j2) |
| 63 | + self.create_line(x1,y1,x2,y2,fill=color,width=width) |
| 64 | + |
| 65 | + def draw_rectangle(self,i1,j1,i2,j2,color="black"): |
| 66 | + x1,y1=self.ij2xy(i1,j1) |
| 67 | + x2,y2=self.ij2xy(i2,j2) |
| 68 | + self.create_rectangle(x1,y1,x2,y2,fill=color) |
| 69 | + |
| 70 | + |
| 71 | + def display(self,grid,markup,network,links,freeze=False): |
| 72 | + |
| 73 | + space=self.space |
| 74 | + dim=self.dim |
| 75 | + |
| 76 | + bg="#D5B359" |
| 77 | + for item in self.find_all():self.delete(item) |
| 78 | + self.config(width=space*(1+dim+1), height=space*(1+dim+1)) |
| 79 | + |
| 80 | + if freeze: |
| 81 | + black="red" |
| 82 | + else: |
| 83 | + black="black" |
| 84 | + |
| 85 | + self.draw_rectangle(-0.1-.5,-0.1-.5,dim-1+.5+0.1,dim-1+.5+0.1,black) |
| 86 | + self.draw_rectangle(0-.5,0-.5,dim-1+.5,dim-1+.5,bg) |
| 87 | + for i in range(dim): |
| 88 | + self.draw_line(i,0,i,dim-1,color=black) |
| 89 | + self.draw_line(0,i,dim-1,i,color=black) |
| 90 | + |
| 91 | + x,y=self.ij2xy(-1-0.1,i) |
| 92 | + self.create_text(x,y, text="ABCDEFGHJKLMNOPQRSTUVWXYZ"[i],font=("Arial", str(int(space/2.5)))) |
| 93 | + x,y=self.ij2xy(dim,i) |
| 94 | + self.create_text(x,y, text="ABCDEFGHJKLMNOPQRSTUVWXYZ"[i],font=("Arial", str(int(space/2.5)))) |
| 95 | + x,y=self.ij2xy(i,-1-0.1) |
| 96 | + self.create_text(x,y, text=str(i+1),font=("Arial", str(int(space/2.5)))) |
| 97 | + x,y=self.ij2xy(i,dim+0.1) |
| 98 | + self.create_text(x,y, text=str(i+1),font=("Arial", str(int(space/2.5)))) |
| 99 | + |
| 100 | + if dim==19: |
| 101 | + for i,j in [[3,3],[3,9],[9,9],[3,15],[15,15],[15,9],[9,15],[15,3],[9,3]]: |
| 102 | + self.draw_point(i,j,0.3,black) |
| 103 | + |
| 104 | + for i in range(dim): |
| 105 | + for j in range(dim): |
| 106 | + neighbors=neighborhood(i,j,dim) |
| 107 | + |
| 108 | + if network[i][j]==1: |
| 109 | + self.draw_point(i,j,0.5,"#8A80D8",outline="#8A80D8") |
| 110 | + |
| 111 | + if network[i][j]==2: |
| 112 | + self.draw_point(i,j,0.5,"#FFF88E",outline="#FFF88E") |
| 113 | + |
| 114 | + for u,v in neighbors: |
| 115 | + if links[p2l(i,j,u,v,dim)]==1: |
| 116 | + self.draw_line(i,j,u,v,color="#8A80D8",width=3) |
| 117 | + elif links[p2l(i,j,u,v,dim)]==2: |
| 118 | + self.draw_line(i,j,u,v,color="#FFF88E",width=3) |
| 119 | + |
| 120 | + |
| 121 | + for i in range(dim): |
| 122 | + for j in range(dim): |
| 123 | + |
| 124 | + markup_color='black' |
| 125 | + u=i+self.mesh[i][j][0] |
| 126 | + v=j+self.mesh[i][j][1] |
| 127 | + if grid[i][j]==1: |
| 128 | + #black |
| 129 | + self.draw_point(u,v,.95,"black") |
| 130 | + markup_color='white' |
| 131 | + if grid[i][j]==2: |
| 132 | + self.draw_point(u,v,.95,"white") |
| 133 | + |
| 134 | + if type(markup[i][j]) is int: |
| 135 | + if markup[i][j]==0: |
| 136 | + if grid[i][j]==0: |
| 137 | + self.draw_point(u,v,0.6,color=bg,outline=bg) |
| 138 | + k=0.6 |
| 139 | + |
| 140 | + self.draw_line(u+0.5*k,v-0*k,u-0.255*k,v+0.435*k,markup_color,width=2) |
| 141 | + self.draw_line(u-0.255*k,v+0.435*k,u-0.255*k,v-0.435*k,markup_color,width=2) |
| 142 | + self.draw_line(u-0.255*k,v-0.435*k,u+0.5*k,v-0*k,markup_color,width=2) |
| 143 | + else: |
| 144 | + x,y=self.ij2xy(u,v) |
| 145 | + self.create_text(x,y, text=str(markup[i][j]),font=("Arial", str(int(space/2))),fill=markup_color) |
| 146 | + |
| 147 | + |
| 148 | + elif markup[i][j]=="": |
| 149 | + #do nothing |
| 150 | + pass |
| 151 | + else: |
| 152 | + sequence=markup[i][j] |
| 153 | + markup_color=sequence[0][4] |
| 154 | + letter_color=sequence[0][5] |
| 155 | + x,y=self.ij2xy(u,v) |
| 156 | + self.draw_point(u,v,0.8,color=bg,outline=markup_color) |
| 157 | + self.create_text(x,y, text=sequence[0][2],font=("Arial", str(int(space/2))),fill=letter_color) |
| 158 | + local_area=self.draw_point(u,v,1,color="",outline="") |
| 159 | + self.tag_bind(local_area, "<Enter>", partial(show_variation,goban=self,grid=grid,markup=markup,i=i,j=j)) |
| 160 | + |
| 161 | + self.update_idletasks() |
| 162 | + |
| 163 | + |
| 164 | + |
| 165 | +def show_variation(): |
| 166 | + pass |
| 167 | + |
| 168 | + |
| 169 | + |
| 170 | +def countlib(grid,i,j,lib=0,tab=None): |
| 171 | + dim=len(grid) |
| 172 | + |
| 173 | + if not tab: |
| 174 | + start=True |
| 175 | + tab=[] |
| 176 | + for p in range(dim):tab.append([0]*dim) |
| 177 | + else:start=False |
| 178 | + color=grid[i][j] |
| 179 | + if color==0: |
| 180 | + return -1 |
| 181 | + tab[i][j]=1 |
| 182 | + |
| 183 | + for x,y in neighborhood(i,j,dim): |
| 184 | + if grid[x][y]==color and tab[x][y]==0: |
| 185 | + lib,tab=countlib(grid,x,y,lib,tab) |
| 186 | + elif grid[x][y]==0 and tab[x][y]==0: |
| 187 | + tab[x][y]=2 |
| 188 | + lib+=1 |
| 189 | + |
| 190 | + if start: |
| 191 | + return lib |
| 192 | + else: |
| 193 | + return lib,tab |
| 194 | + |
| 195 | +def remove_group(grid,i,j): |
| 196 | + color=grid[i][j] |
| 197 | + grid[i][j]=0 |
| 198 | + dim=len(grid) |
| 199 | + for x,y in neighborhood(i,j,dim): |
| 200 | + if grid[x][y]==color: |
| 201 | + remove_group(grid,x,y) |
| 202 | + |
| 203 | +def place(grid,i,j,color): |
| 204 | + grid[i][j]=color |
| 205 | + dim=len(grid) |
| 206 | + for x,y in neighborhood(i,j,dim): |
| 207 | + if grid[x][y]>0 and grid[x][y]!=color: |
| 208 | + if countlib(grid,x,y)==0: |
| 209 | + remove_group(grid,x,y) |
| 210 | + |
| 211 | + |
| 212 | +def neighborhood(i,j,dim): |
| 213 | + list=[] |
| 214 | + if 0 <= i+1 <= dim-1 and 0 <= j <= dim-1: |
| 215 | + list.append([i+1,j]) |
| 216 | + if 0 <= i-1 <= dim-1 and 0 <= j <= dim-1: |
| 217 | + list.append([i-1,j]) |
| 218 | + if 0 <= i <= dim-1 and 0 <= j-1 <= dim-1: |
| 219 | + list.append([i,j-1]) |
| 220 | + if 0 <= i <= dim-1 and 0 <= j+1 <= dim-1: |
| 221 | + list.append([i,j+1]) |
| 222 | + return list |
0 commit comments