-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.py
72 lines (56 loc) · 1.99 KB
/
board.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
# -*- coding: utf-8 -*-
import numpy as np
from PyQt5 import QtWidgets, QtCore
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib import pyplot as plt
class Board(QtWidgets.QWidget):
# stone radius
radius = 0.4
size = 16
def __init__(self, size=(5.0, 4.0), dpi=80):
QtWidgets.QWidget.__init__(self)
self.fig = Figure(figsize=(11, 11), dpi=dpi)
self.canvas = FigureCanvas(self.fig)
self.canvas.setParent(self)
self.ax = self.fig.add_subplot(111)
self.ax.get_xaxis().set_visible(True)
self.ax.get_yaxis().set_visible(True)
self.vbox = QtWidgets.QVBoxLayout()
self.vbox.addWidget(self.canvas)
self.fig.tight_layout()
self.setLayout(self.vbox)
# draw 16*16 go board by default
ticks = np.arange(self.size)
self.ax.set_xticks(ticks)
self.ax.set_yticks(ticks)
self.ax.grid(True, color='black')
# make it look wooden
self.fig.set_facecolor('#DBB263')
self.ax.set_facecolor('#DBB263')
def resizeEvent(self, event):
new_size = QtCore.QSize(10, 10)
new_size.scale(event.size(), QtCore.Qt.KeepAspectRatio)
self.resize(new_size)
def get_size(self):
return self.size
def set_size(self, size):
ticks = np.arange(size)
self.ax.set_xticks(ticks)
self.ax.set_yticks(ticks)
self.ax.set_xlim((0, size-1))
self.ax.set_ylim((0, size-1))
self.size = size
self.draw()
def draw(self):
self.canvas.draw()
def place_stone_at(self, coord, color):
xidx = coord[0]
yidx = coord[1]
stone = plt.Circle((xidx, yidx), radius=self.radius,
fc=color, zorder=10)
self.ax.add_patch(stone)
self.draw()
def clear_board(self):
self.ax.patches.clear()
self.draw()