-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·180 lines (173 loc) · 9.34 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
169
170
171
172
173
174
175
176
177
178
179
180
import tkinter as tk
from tkinter import *
import chessBoard as cb
import player as com
WIDTH = 874
HEIGHT = 778
RESURSE = 'resurse/'
class App:
board = cb.chessBoard()
missingPiecesBlack = []
itemClicked = False
colors = ['White','Black']
missingPiecesWhite = []
t = 0
comOp = False
def __init__(self,master):
self.Master=master
master.title("Sah")
self.canvas = Canvas(self.Master)
self.pieces = [tk.PhotoImage(file = RESURSE + 'SquareWhite.gif'),tk.PhotoImage(file=RESURSE +'PawnBlack.gif'),tk.PhotoImage(file=RESURSE+'KnightBlack.gif'),tk.PhotoImage(file=RESURSE+'BishopBlack.gif'), tk.PhotoImage(file=RESURSE+'RookBlack.gif'),tk.PhotoImage(file=RESURSE+'QueenBlack.gif'),tk.PhotoImage(file=RESURSE+'KingBlack.gif'),tk.PhotoImage(file=RESURSE+'PawnWhite.gif'), tk.PhotoImage(file=RESURSE+'KnightWhite.gif'),tk.PhotoImage(file=RESURSE+'BishopWhite.gif'),tk.PhotoImage(file=RESURSE+'RookWhite.gif'),tk.PhotoImage(file=RESURSE+'QueenWhite.gif'), tk.PhotoImage(file=RESURSE+'KingWhite.gif')]
self.activePieces = [tk.PhotoImage(file=RESURSE+'SquareWhite.gif'),tk.PhotoImage(file=RESURSE+'PawnBlackAct.gif'),tk.PhotoImage(file=RESURSE+'KnightBlackAct.gif'), tk.PhotoImage(file=RESURSE+'BishopBlackAct.gif'), tk.PhotoImage(file=RESURSE+'RookBlackAct.gif'),tk.PhotoImage(file=RESURSE+'QueenBlackAct.gif'),tk.PhotoImage(file=RESURSE+'KingBlackAct.gif'), tk.PhotoImage(file=RESURSE+'PawnWhiteAct.gif'), tk.PhotoImage(file=RESURSE+'KnightWhiteAct.gif'),tk.PhotoImage(file=RESURSE+'BishopWhiteAct.gif'),tk.PhotoImage(file=RESURSE+'RookWhiteAct.gif'), tk.PhotoImage(file=RESURSE+'QueenWhiteAct.gif'), tk.PhotoImage(file=RESURSE+'KingWhiteAct.gif')]
self.emptySpaces = [tk.PhotoImage(file=RESURSE+'SquareWhite.gif'),tk.PhotoImage(file=RESURSE+'SquareGrey.gif'),tk.PhotoImage(file=RESURSE+'SquareActive.gif'), tk.PhotoImage(file=RESURSE+'SquareClicked.gif')]
self.frame = Frame(master); self.frame.grid()
self.play = Label(self.frame, text = "Impotriva botului sau doi jucatori?")
self.play.grid(row=0, column =0, columnspan = 6)
self.YesButton = Button(self.frame, text = "Impotriva botului", command = self.Yes)
root.bind('<Return>', (lambda e, YesButton=self.YesButton: self.YesButton.invoke()))
self.YesButton.grid(row=1,column=0)
self.NoButton = Button(self.frame, text='Doi jucatori', command=self.No)
self.NoButton.grid(row=1,column =1)
def Yes(self):
self.comOp = True
self.computer = com.player()
self.frame.destroy()
self.startGame()
def No(self):
self.frame.destroy()
self.startGame()
def startGame(self):
self.Master.minsize(WIDTH,HEIGHT)
self.canvas.config(height=HEIGHT,width=WIDTH,bg = 'black')
self.canvas.pack()
self.Master.update()
self.displayBoard()
def displayBoard(self):
for i in range(8):
for j in range(8):
lH = 10 + 96*j
lW = 10 + 96*i
self.canvas.create_image(lW,lH,image=self.emptySpaces[(i+j)%2],anchor=NW,activeimage = self.emptySpaces[2])
for r in range(8):
for c in range(8):
lW = 10 + 96*c
lH = 10 + 96*r
if self.board.grid[r][c] != 0:
self.canvas.create_image(lW,lH,image=self.pieces[self.board.grid[r][c]],anchor=NW,activeimage = self.activePieces[self.board.grid[r][c]])
for k in range(12):
lH = 10 + 61*k
self.canvas.create_image(HEIGHT + 1,lH,image=self.emptySpaces[0],anchor=NW)
for l in range(len(self.missingPiecesBlack)):
lH = 10 + 96*l
self.canvas.create_image(HEIGHT + 1,lH,image=self.pieces[self.missingPiecesBlack[l]],anchor=NW)
for n in range(len(self.missingPiecesWhite)):
lH = 394 + 96*n
self.canvas.create_image(HEIGHT + 1,lH,image=self.pieces[self.missingPiecesWhite[n]],anchor=NW)
self.Master.update()
def callback(self,event):
color = self.colors[self.t%2]
if self.itemClicked == False:
for r in range(len(self.board.grid)):
for c in range(len(self.board.grid[r])):
lH = 10 + 96*r
lW = 10 + 96*c
if event.x in range(lW,lW+90) and event.y in range(lH,lH+90) and self.board.grid[r][c] != 0 and self.board.pieces[self.board.grid[r][c]][1] == color:
self.canvas.create_image(lW,lH,image=self.emptySpaces[3],anchor=NW)
self.canvas.create_image(lW,lH,image=self.pieces[self.board.grid[r][c]],anchor=NW)
self.itemClicked = True
self.curPos = [r,c]
print (r, c)
else:
for r in range(len(self.board.grid)):
for c in range(len(self.board.grid[r])):
lH = 10 + 96*r
lW = 10 + 96*c
if event.x in range(lW,lW+90) and event.y in range(lH,lH+90):
if r == self.curPos[0] and c == self.curPos[1]:
self.canvas.create_image(lW,lH,image=self.emptySpaces[(r+c)%2],anchor=NW)
self.canvas.create_image(lW,lH,image=self.pieces[self.board.grid[r][c]],anchor=NW,activeimage = self.activePieces[self.board.grid[r][c]])
self.itemClicked = False
print (r, c, 'fail')
else:
self.finPos = [r,c]
if self.board.turnValid(self.board.grid,self.curPos,self.finPos,color):
self.itemClicked = False
self.t += 1
print (r, c)
move = self.grid_to_uci(self.curPos, self.finPos)
self.computer.user_move(move)
#Add to missing pieces list ---------------------------------------------------
if self.board.grid[self.finPos[0]][self.finPos[1]]%6 > 1 and color == 'White':
self.missingPiecesBlack.append(self.board.grid[self.finPos[0]][self.finPos[1]])
if len(self.missingPiecesBlack) > 4:
self.missingPiecesBlack.remove(min(self.missingPiecesBlack))
elif self.board.grid[self.finPos[0]][self.finPos[1]]%6 > 1 and color == 'Black':
self.missingPiecesWhite.append(self.board.grid[self.finPos[0]][self.finPos[1]])
if len(self.missingPiecesWhite) > 4:
self.missingPiecesWhite.remove(min(self.missingPiecesWhite))
# --------------------------------------------------------------
self.board.grid[self.finPos[0]][self.finPos[1]] = self.board.grid[self.curPos[0]][self.curPos[1]]
self.board.grid[self.curPos[0]][self.curPos[1]] = 0
#Pawn promotion -----------------------------------------------------------
if (self.board.grid[self.finPos[0]][self.finPos[1]] == 1 and r == 7) or (self.board.grid[self.finPos[0]][self.finPos[1]] == 7 and r == 0):
if color == 'White' and len(self.missingPiecesWhite) > 0:
self.board.grid[self.finPos[0]][self.finPos[1]] = max(self.missingPiecesWhite)
self.missingPiecesWhite.remove(max(self.missingPiecesWhite))
elif color == "Black" and len(self.missingPiecesBlack) > 0:
self.board.grid[self.finPos[0]][self.finPos[1]] = max(self.missingPiecesBlack)
self.missingPiecesBlack.remove(max(self.missingPiecesBlack))
# -----------------------------------------------------------
self.canvas.delete(ALL)
self.displayBoard()
if self.comOp == True and self.t%2 == 1:
color = self.colors[self.t%2]
choice = self.computer.makeMove2(self.board,color)
print(choice[1],choice[2],choice[3])
# move = self.computer.computer_move()
# choice = self.uci_to_grid(str(move))
# choice = [[]] + choice
print (choice[1], choice[2])
#Add to missing pieces list ---------------------------------------------------
if self.board.grid[choice[2][0]][choice[2][1]]%6 > 1 and color == 'Black':
self.missingPiecesWhite.append(self.board.grid[choice[2][0]][choice[2][1]])
if len(self.missingPiecesWhite) > 4:
self.missingPiecesWhite.remove(min(self.missingPiecesWhite))
elif self.board.grid[choice[2][0]][choice[2][1]]%6 > 1 and color == 'White':
self.missingPiecesBlack.append(self.board.grid[choice[2][0]][choice[2][1]])
if len(self.missingPiecesBlack) > 4:
self.missingPiecesBlack.remove(min(self.missingPiecesBlack))
# -----------------------------------------------------------
self.board.grid[choice[2][0]][choice[2][1]] = self.board.grid[choice[1][0]][choice[1][1]]
self.board.grid[choice[1][0]][choice[1][1]] = 0
#Pawn promotion -----------------------------------------------------------
if (self.board.grid[choice[2][0]][choice[2][1]] == 1 and r == 7) or (self.board.grid[choice[2][0]][choice[2][1]] == 7 and r == 0):
if color == 'Black' and len(self.missingPiecesBlack) > 0:
self.board.grid[choice[2][0]][choice[2][1]] = max(self.missingPiecesBlack)
self.missingPiecesBlack.remove(max(self.missingPiecesBlack))
elif color == "White" and len(self.missingPiecesWhite) > 0:
self.board.grid[choice[2][0]][choice[2][1]] = max(self.missingPiecesWhite)
self.missingPiecesWhite.remove(max(self.missingPiecesWhite))
# -----------------------------------------------------------
# print()
# print(choice[1],choice[2],choice[3])
# print()
self.t += 1
self.canvas.delete(ALL)
self.displayBoard()
self.Master.update()
def uci_to_grid(self, move):
move_from = [8 - (ord(move[1]) - ord('0')), ord(move[0]) - 97]
move_to = [8 - (ord(move[3]) - ord('0')), ord(move[2]) - 97]
return [move_from, move_to]
def grid_to_uci(self, move_from, move_to):
move = []
move.append(chr(move_from[1]+97))
move.append(chr(8-move_from[0]+ord('0')))
move.append(chr(move_to[1]+97))
move.append(chr(8-move_to[0]+ord('0')))
return ''.join(move)
if __name__ == '__main__':
root = tk.Tk()
app = App(root)
app.canvas.bind("<Button-1>", app.callback)
root.mainloop()