-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
168 lines (142 loc) · 6.03 KB
/
Main.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
from ChessBot import ChessBot
from tkinter import *
import tkinter.font as font
from functools import partial
import time
import numpy as np
class Window(Frame):
"""
User interface
"""
def __init__(self, master=None):
"""
Initialization
The frame basically exists of 8x8 buttons that change label and color
"""
Frame.__init__(self, master)
self.master = master
self.pack(fill=BOTH, expand=1)
# Pictograms for chess pieces, might not be supported by system
self.pieces = ['♚','♛','♝','♞', '♜', ' ♟︎', '',
'♙', '♖', '♘', '♗', '♕', '♔']
# Setup the chessbot
self.bot = ChessBot()
# Setup the buttons
self.button = []
self.button_text = []
self.colors = ['white', self._from_rgb((200, 200, 200))]*2
for i in range(8):
for j in range(8):
self.button_text.append(StringVar())
self.button.append(Button(self,
textvariable=self.button_text[-1],
command=partial(self.clickExitButton,
[i,j]),
bg = self.colors[i%2+j%2],
height = 1, width = 3))
self.button[-1].grid(row=i,column=j)
self.button[-1]['font'] = font.Font(size=20)
self.button_text[-1].set(self.pieces[-1*self.bot.board[i, j]-7])
self.textbox = Text(self, height = 1, width = 52)
self.textbox.text = ''
self.textbox.insert(END, self.textbox.text)
self.textbox.place(x=0,y=464)
# fromm and to track where the last move of the user
self.fromm = ''
self.to = ''
def set_message(self, text):
self.textbox.text = text
self.textbox.delete(1.0,"end")
self.textbox.insert(1.0, self.textbox.text)
def _from_rgb(self, rgb):
"""
translates an rgb tuple of int to a tkinter friendly color code
"""
return "#%02x%02x%02x" % rgb
def clickExitButton(self, arg):
print(self.bot.board)
"""
Action to perform on button click.
Inputs:
arg: the location of the button that is clicked.
"""
# If fromm is empty, the piece is selected. If froom is not empty,
# the user is selecting the place to go
if self.fromm == '':
self.fromm = self.bot.convert([arg[0], arg[1]])
self.fromm_loc = arg[0]*8+arg[1]
# Highlight the button
self.button[self.fromm_loc].configure(bg="yellow")
print('From ' + self.fromm)
self.set_message('You: ' + self.fromm + ' to ..')
else:
self.to = self.bot.convert([arg[0], arg[1]])
self.to_loc = arg[0]*8+arg[1]
print('to ' + self.to)
print('')
self.set_message('You: ' + self.fromm + ' to '+ self.to)
# Request the move in the bot
status = self.bot.user_move(self.fromm,
self.to, user = 'user', show = False)
# If approved, move the bot, if not, color it red
if status:
self.button[self.fromm_loc].configure(bg="green")
self.button[self.to_loc].configure(bg="yellow")
self.set_text()
self.update()
self.check_check()
status = self.bot.check_for_check('self')
if status:
self.set_message(self.bot.message)
start, self.end = self.bot.next_move()
self.set_message('Bot: ' + self.bot.convert(start)
+ ' to '+ self.bot.convert(self.end))
self.reset_background()
self.button[start[0]*8+start[1]].configure(bg="green")
self.button[self.end[0]*8+self.end[1]].configure(bg="yellow")
self.check_check()
status = self.bot.check_for_check('user')
if status:
self.set_message(self.bot.message)
else:
self.button[self.fromm_loc].configure(bg="red")
self.set_message(self.bot.message)
# Wait a bit and reset the background colors
self.update()
time.sleep(0.5)
self.reset_background()
self.fromm = ''
self.set_text()
def check_check(self):
if not np.any(self.bot.board == 6):
self.set_message('You win!')
self.button[self.to_loc].configure(bg="red")
self.update()
time.sleep(5)
self.destroy()
if not np.any(self.bot.board == -6):
self.set_message('The bot wins')
self.button[self.to_loc].configure(bg="red")
self.update()
time.sleep(5)
self.destroy()
def set_text(self):
"""
Updates the labels of all the buttons based on bot.board
"""
for i in range(8):
for j in range(8):
self.button_text[j+8*i].set(self.pieces[-1*self.bot.board[i,
j]-7])
def reset_background(self):
"""
Reset the pattern of the board
"""
for i in range(8):
for j in range(8):
self.button[j*8+i].configure(bg=self.colors[i%2+j%2])
root = Tk()
app = Window(root)
root.wm_title("Chess bot")
root.geometry("485x485")
root.mainloop()