-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoardWidget.py
177 lines (164 loc) · 6.07 KB
/
BoardWidget.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Copyright (C) 2013 Alexey Vyskubov ([email protected])
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License version 2 as published by the
# Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# The license is currently available on the Internet at:
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
from Tkinter import *
from Constants import Color
class BoardWidget(Canvas):
def __init__(self, parent, controller=None, pil=False, **options):
Canvas.__init__(self, parent, **options)
self._controller = controller
controller.register_board_widget(self)
self.bind('<Configure>', self.resize)
self.bind('<Motion>', self.motion)
self.bind('<Leave>', self.leave)
self.bind('<Button-1>', self.click)
self.image = None
self.texture = None
if pil:
import Image as I
import ImageTk as IT
self.image = I.open('images/wood.png')
self.texture = IT.PhotoImage(self.image)
self._resize()
def _resize(self):
w = self.winfo_width()
h = self.winfo_height()
# side
# the size of the biggest square which can be fitted into the
# canvas.
# step
# 1/20 of 'side'. The board lines are one step from each other
# and from the square border.
# r
# the radius of the stone, one half of 'step' minus 1 pixel
# points
# the array of 21 number, starting from 0 and 'step' from each
# other
self.side = min(w, h)
self.step = self.side / 20
self.r = self.step / 2 - 1
self.points = [self.step * i for i in xrange(0, 21)]
def _find_point(self, x):
"""
Return the closest to x value in self.points and its index.
"""
ix = (x + (self.step / 2)) / self.step
# Canvas may be non-square
if ix > 20: ix = 20
return self.points[ix], ix
def add_stone(self, nx, ny, color):
c = {Color.B: 'black', Color.W: 'white'}[color]
x = self.points[nx + 1]
y = self.points[ny + 1]
self.create_oval(x - self.r, y - self.r, x + self.r, y + self.r,
fill = c)
def update_board(self):
# Clean the board
self.delete(ALL)
# Draw the texture
if self.texture:
center = self.points[10]
self.create_image(center, center, image=self.texture)
# Draw the board border
start = self.points[1]
end = self.points[19]
self.create_polygon(start, start,
end, start,
end, end,
start, end,
fill='',
outline='black',
joinstyle=MITER,
width=3)
# Board lines
for i in xrange(17):
step_i = self.step * (i + 2)
self.create_line(start, step_i, end, step_i, width=1)
self.create_line(step_i, start, step_i, end, width=1)
# Hoshi points
hr = max(self.step / 10, 2)
hoshi = [4, 10, 16]
for ix in hoshi:
for iy in hoshi:
x = self.points[ix]
y = self.points[iy]
self.create_oval(x - hr, y - hr, x + hr, y + hr,
fill='black')
# Stones
if self._controller:
for nx, ny, c in self._controller.get_stones():
self.add_stone(nx, ny, c)
# Last move
lm = self._controller.last_move()
if lm:
lmx, lmy = lm
x = self.points[lmx + 1]
y = self.points[lmy + 1]
c = {Color.B: '#808080',
Color.W: '#A0A0A0'}[self._controller.to_move()]
o = {Color.B: 'black',
Color.W: 'white'}[self._controller.to_move()]
r = hr * 2
self.create_oval(x - r, y - r, x + r, y + r,
fill=c, outline='', width=1)
# Ko
self.update_ko()
def update_ko(self):
self.delete('ko')
ko = self._controller.get_ko()
if ko:
nxb, nyb = ko
ko_size = max(self.step/5, 4)
x = self.points[nxb + 1]
y = self.points[nyb + 1]
self.create_polygon(x - ko_size, y - ko_size,
x + ko_size, y - ko_size,
x + ko_size, y + ko_size,
x - ko_size, y + ko_size,
fill='',
outline='black',
joinstyle=MITER,
width = 1,
tag='ko')
def resize(self, event):
self._resize()
self.update_board()
def motion(self, event):
self.delete('ghost')
x, ix = self._find_point(event.x)
y, iy = self._find_point(event.y)
nxb = ix - 1
nyb = iy - 1
c = {Color.B: '#808080',
Color.W: '#E0E0E0'}[self._controller.to_move()]
if ix > 0 and ix < 20 and iy > 0 and iy < 20:
if self._controller.allowed(nxb, nyb):
self.create_oval(x - self.r, y - self.r, x + self.r, y +
self.r, tag='ghost', fill = c)
def leave(self, event):
self.delete('ghost')
def click(self, event):
x = event.x
y = event.y
_, ix = self._find_point(x)
_, iy = self._find_point(y)
if ix == 0 or ix == 20 or iy == 0 or iy == 20: return
nxb = ix - 1
nyb = iy - 1
if self._controller.allowed(nxb, nyb):
self.delete('ghost')
self._controller.add(nxb, nyb)