-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGUI.py
171 lines (147 loc) · 8.15 KB
/
GUI.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tkinter
import math
import Point
import Record
import time
import MCTS
import numpy as np
class Chess_Board_Canvas(tkinter.Canvas):
# 棋盤繪製
def __init__(self, master=None, height=0, width=0):
tkinter.Canvas.__init__(self, master, height=height, width=width)
self.step_record_chess_board = Record.Step_Record_Chess_Board()
# 初始化記步器
self.init_chess_board_points() # 畫點
self.init_chess_board_canvas() # 畫棋盤
self.board = MCTS.Board()
self.AI = MCTS.MonteCarlo(self.board, 2)
self.clicked = 0
def init_chess_board_points(self):
'''
生成棋盤點,並且對應到像素座標
保存到 chess_board_points 屬性
'''
self.chess_board_points = [[None for i in range(15)] for j in range(15)]
for i in range(15):
for j in range(15):
self.chess_board_points[i][j] = Point.Point(i, j) # 轉換棋盤座標像素座標
def init_chess_board_canvas(self):
'''
初始化棋盤
'''
for i in range(15): # 直線
self.create_line(self.chess_board_points[i][0].pixel_x, self.chess_board_points[i][0].pixel_y,
self.chess_board_points[i][14].pixel_x, self.chess_board_points[i][14].pixel_y)
for j in range(15): # 橫線
self.create_line(self.chess_board_points[0][j].pixel_x, self.chess_board_points[0][j].pixel_y,
self.chess_board_points[14][j].pixel_x, self.chess_board_points[14][j].pixel_y)
# 邊界
self.create_line(self.chess_board_points[2][2].pixel_x, self.chess_board_points[2][2].pixel_y,
self.chess_board_points[2][12].pixel_x, self.chess_board_points[2][12].pixel_y, fill="red")
self.create_line(self.chess_board_points[12][2].pixel_x, self.chess_board_points[12][2].pixel_y,
self.chess_board_points[12][12].pixel_x, self.chess_board_points[12][12].pixel_y, fill="red")
self.create_line(self.chess_board_points[2][12].pixel_x, self.chess_board_points[2][12].pixel_y,
self.chess_board_points[12][12].pixel_x, self.chess_board_points[12][12].pixel_y, fill="red")
self.create_line(self.chess_board_points[2][2].pixel_x, self.chess_board_points[2][2].pixel_y,
self.chess_board_points[12][2].pixel_x, self.chess_board_points[12][2].pixel_y, fill="red")
for i in range(15): # 交點橢圓
for j in range(15):
r = 1
self.create_oval(self.chess_board_points[i][j].pixel_x - r, self.chess_board_points[i][j].pixel_y - r,
self.chess_board_points[i][j].pixel_x + r, self.chess_board_points[i][j].pixel_y + r)
def click1(self, event): # click關鍵字重複
'''
監聽滑鼠事件,根據滑鼠位置判斷落點
'''
if (self.clicked != 1):
for i in range(15):
for j in range(15):
square_distance = math.pow((event.x - self.chess_board_points[i][j].pixel_x), 2) + math.pow(
(event.y - self.chess_board_points[i][j].pixel_y), 2)
# 計算滑鼠的位置和點的距離
# 距離小於14的點
if (square_distance <= 200) and (self.step_record_chess_board.checkState(i, j) == None): # 合法落子位置
self.clicked = 1
if self.step_record_chess_board.who_to_play() == 1:
# 奇數次,黑落子
self.create_oval(self.chess_board_points[i][j].pixel_x - 10,
self.chess_board_points[i][j].pixel_y - 10,
self.chess_board_points[i][j].pixel_x + 10,
self.chess_board_points[i][j].pixel_y + 10, fill='black')
tkinter.Canvas.update(self)
# 偶數次,白落子
elif self.step_record_chess_board.who_to_play() == 2:
self.create_oval(self.chess_board_points[i][j].pixel_x - 10,
self.chess_board_points[i][j].pixel_y - 10,
self.chess_board_points[i][j].pixel_x + 10,
self.chess_board_points[i][j].pixel_y + 10, fill='white')
result = 0
if (self.step_record_chess_board.value[1][i][j] >= 90000):
result = 1
self.clicked = 0
self.step_record_chess_board.insert_record(i, j)
# 落子,最多225
#######result = self.step_record_chess_board.check()
# 判斷是否有五子連珠
if result == 1:
self.create_text(240, 475, text='the black wins')
# 解除左键绑定
self.unbind('<Button-1>')
# """Unbind for this widget for event SEQUENCE the
# function identified with FUNCID."""
elif result == 2:
self.create_text(240, 475, text='the white wins')
# 解除左键绑定
self.unbind('<Button-1>')
# 根據價值網路落子
if (self.clicked == 1):
x = 0
y = 0
max_value = 0
for i in range(0, 15):
for j in range(0, 15):
if (self.step_record_chess_board.value[2][i][j] >= 90000):
x = i
y = j
max_value = 99999
break;
elif (self.step_record_chess_board.value[0][i][j] >= max_value):
x = i
y = j
max_value = self.step_record_chess_board.value[0][i][j]
if (self.step_record_chess_board.value[2][x][y] >= 90000):
result = 2
self.board.state = np.copy(self.step_record_chess_board.state)
self.AI.value = self.step_record_chess_board.value[0]
self.AI.update(self.board.state)
action = self.AI.bestAction()
x, y = action
self.step_record_chess_board.insert_record(x, y)
self.create_oval(self.chess_board_points[x][y].pixel_x - 10, self.chess_board_points[x][y].pixel_y - 10,
self.chess_board_points[x][y].pixel_x + 10, self.chess_board_points[x][y].pixel_y + 10,
fill='white')
#######result = self.step_record_chess_board.check()
# 判斷是否有五子連珠
if result == 1:
self.create_text(240, 475, text='the black wins')
# 解除左键绑定
self.unbind('<Button-1>')
# """Unbind for this widget for event SEQUENCE the
# function identified with FUNCID."""
elif result == 2:
self.create_text(240, 475, text='the white wins')
# 解除左键绑定
self.unbind('<Button-1>')
self.clicked = 0
class Chess_Board_Frame(tkinter.Frame):
def __init__(self, master=None):
tkinter.Frame.__init__(self, master)
self.create_widgets()
def create_widgets(self):
self.chess_board_label_frame = tkinter.LabelFrame(self, text="Chess Board", padx=5, pady=5)
self.chess_board_canvas = Chess_Board_Canvas(self.chess_board_label_frame, height=500, width=480)
self.chess_board_canvas.bind('<Button-1>', self.chess_board_canvas.click1)
self.chess_board_label_frame.pack()
self.chess_board_canvas.pack()