-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
357 lines (279 loc) · 14 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
from tkinter import *
import globals
import ai
from random import randint
from copy import deepcopy
#uncomment for logging to file
#old_stdout = sys.stdout
#log_file = open("message.log","w")
#sys.stdout = log_file
#TODO
#ai sucks
#add checkmate
class MainWindow(Frame):
def __init__(self,parent):
Frame.__init__(self,parent)
self.parent = parent
self.init()
#initialize the GUI frames and grids
def init(self):
self.MenuBar = Frame(self.parent,background="blue")
self.MenuBar.pack()
self.NewGameButton = Button(self.MenuBar,text="New Game")
self.NewGameButton.pack()
self.BlackPiecesFrame = Frame(self.parent,background="red")
self.BlackPiecesFrame.pack()
self.ChessGridFrame = Frame(self.parent,background="pink")
self.ChessGridFrame.pack()
self.CreateGrid()
self.WhitePiecesFrame = Frame(self.parent,background="red")
self.WhitePiecesFrame.pack()
#create initial chess grid configuration
def CreateGrid(self):
self.ChessCells = []
for i in range(0,8):
self.ChessCells.append([])
globals.ChessGrid.append([])
for j in range(0,8):
if((i==0 and j==0) or (i==0 and j==7)):
self.ChessCells[i].append(Button(self.ChessGridFrame,image=globals.ImageDict["BlackRook"],command = lambda i=i,j=j : self.CellSelect(i,j)))
globals.ChessGrid[i].append("22")
self.ChessCells[i][j].grid(row=i,column=j)
elif((i==0 and j==1) or (i==0 and j==6)):
self.ChessCells[i].append(Button(self.ChessGridFrame,image=globals.ImageDict["BlackKnight"],command = lambda i=i,j=j : self.CellSelect(i,j)))
globals.ChessGrid[i].append("24")
self.ChessCells[i][j].grid(row=i,column=j)
elif((i==0 and j==2) or (i==0 and j==5)):
self.ChessCells[i].append(Button(self.ChessGridFrame,image=globals.ImageDict["BlackBishop"],command = lambda i=i,j=j : self.CellSelect(i,j)))
globals.ChessGrid[i].append("23")
self.ChessCells[i][j].grid(row=i,column=j)
elif(i==0 and j==3):
self.ChessCells[i].append(Button(self.ChessGridFrame,image=globals.ImageDict["BlackKing"],command = lambda i=i,j=j : self.CellSelect(i,j)))
globals.ChessGrid[i].append("26")
self.ChessCells[i][j].grid(row=i,column=j)
elif(i==0 and j==4):
self.ChessCells[i].append(Button(self.ChessGridFrame,image=globals.ImageDict["BlackQueen"],command = lambda i=i,j=j : self.CellSelect(i,j)))
globals.ChessGrid[i].append("25")
self.ChessCells[i][j].grid(row=i,column=j)
elif(i==1):
self.ChessCells[i].append(Button(self.ChessGridFrame,image=globals.ImageDict["BlackPawn"],command = lambda i=i,j=j : self.CellSelect(i,j)))
globals.ChessGrid[i].append("21")
self.ChessCells[i][j].grid(row=i,column=j)
elif(i==6):
self.ChessCells[i].append(Button(self.ChessGridFrame,image=globals.ImageDict["WhitePawn"],command = lambda i=i,j=j : self.CellSelect(i,j)))
globals.ChessGrid[i].append("11")
self.ChessCells[i][j].grid(row=i,column=j)
elif((i==7 and j==0) or (i==7 and j==7)):
self.ChessCells[i].append(Button(self.ChessGridFrame,image=globals.ImageDict["WhiteRook"],command = lambda i=i,j=j : self.CellSelect(i,j)))
globals.ChessGrid[i].append("12")
self.ChessCells[i][j].grid(row=i,column=j)
elif((i==7 and j==1) or (i==7 and j==6)):
self.ChessCells[i].append(Button(self.ChessGridFrame,image=globals.ImageDict["WhiteKnight"],command = lambda i=i,j=j : self.CellSelect(i,j)))
globals.ChessGrid[i].append("14")
self.ChessCells[i][j].grid(row=i,column=j)
elif((i==7 and j==2) or (i==7 and j==5)):
self.ChessCells[i].append(Button(self.ChessGridFrame,image=globals.ImageDict["WhiteBishop"],command = lambda i=i,j=j : self.CellSelect(i,j)))
globals.ChessGrid[i].append("13")
self.ChessCells[i][j].grid(row=i,column=j)
elif(i==7 and j==3):
self.ChessCells[i].append(Button(self.ChessGridFrame,image=globals.ImageDict["WhiteKing"],command = lambda i=i,j=j : self.CellSelect(i,j)))
globals.ChessGrid[i].append("16")
self.ChessCells[i][j].grid(row=i,column=j)
elif(i==7 and j==4):
self.ChessCells[i].append(Button(self.ChessGridFrame,image=globals.ImageDict["WhiteQueen"],command = lambda i=i,j=j : self.CellSelect(i,j)))
globals.ChessGrid[i].append("15")
self.ChessCells[i][j].grid(row=i,column=j)
else:
self.ChessCells[i].append(Button(self.ChessGridFrame,image=globals.ImageDict["Transparent"],command = lambda i=i,j=j : self.CellSelect(i,j)))
globals.ChessGrid[i].append("0")
self.ChessCells[i][j].grid(row=i,column=j)
#chess grid background 1-gray/black 0-white
a = "10101010"
b = "01010101"
for i in range(0,8):
for j in range(0,8):
if(i%2 == 0):
seq = b
else:
seq = a
if(seq[j] == '1'):
self.ChessCells[i][j].configure(background="gray")
else:
self.ChessCells[i][j].configure(background="white")
#check if selected cell has a valid piece in it according to player and store row and column indices
def CellSelect(self,row,column):
#if(white's turn AND piece is white AND piece is valid)
if(globals.TURN == 1 and globals.ChessGrid[row][column][0] == '1' and globals.ChessGrid[row][column] in globals.ChessPieces):
globals.PieceSelected = True
globals.PieceSelectedRow = row
globals.PieceSelectedColumn = column
elif(globals.TURN == 2 and globals.ChessGrid[row][column][0] == '2' and globals.ChessGrid[row][column] in globals.ChessPieces):
globals.PieceSelected = True
globals.PieceSelectedRow = row
globals.PieceSelectedColumn = column
elif(globals.PieceSelected): #move to selected cell if a piece to move has already been selected in previous click
self.MovePiece(row,column,globals.PieceSelectedRow,globals.PieceSelectedColumn,globals.ChessGrid[globals.PieceSelectedRow][globals.PieceSelectedColumn])
#confirm valid move and move piece from (PieceSelectedRow,PieceSelectedColumn) -> (toRow,toColumn)
def MovePiece(self,toRow,toColumn,fromRow,fromCol,Piece):
print("move "+Piece+" from "+str(fromRow)+","+str(fromCol)+" to "+str(toRow)+","+str(toColumn))
#if king is checked or a move is made
#check if the selected move to make will bring the king out of check
#if yes make the move
#else dont
if(ai.kingChecked(globals.ChessGrid,Piece[0])):
#create temporary board to perform move
newChessGrid = deepcopy(globals.ChessGrid)
moves = ai.getAllMoves(newChessGrid,fromRow,fromCol,Piece[1],Piece[0])
#if move is valid perform the move on temporary board
if([toRow,toColumn] in moves):
newChessGrid[toRow][toColumn] = newChessGrid[fromRow][fromCol]
newChessGrid[fromRow][fromCol] = "0"
else:
print("move is invalid")
globals.PieceSelected = False
return
#if the king is still checked then do not allow this move
if(ai.kingChecked(newChessGrid,Piece[0])):
print("king checked cant make this move")
globals.PieceSelected = False
return
#get all possible moves of the piece
moves = ai.getAllMoves(globals.ChessGrid,fromRow,fromCol,Piece[1],Piece[0])
#if move is valid create new image at new location
if([toRow,toColumn] in moves):
globals.ChessGrid[toRow][toColumn] = globals.ChessGrid[fromRow][fromCol]
globals.ChessGrid[fromRow][fromCol] = "0"
self.CreateImage(toRow,toColumn,globals.PieceSelectedRow,globals.PieceSelectedColumn,Piece)
else:
print("invalid move")
globals.PieceSelected = False
return
#change turn of player 1 to 2 or to AI
if(globals.TURN == 1):
if(globals.AI_on):
moves = [] #stores available moves for a piece
heuristic = 10000 #stores heuristic of chosen move
makeMove = [0,0,0,0] #[fromRow,fromCol,toRow,toCol]
noMove = True #make sure one possible move gets chosen if no move gets chosen by minimax
checked = False #true if ai king is checked
moveHeuristic = None #stores heuristic of move made
if(ai.kingChecked(globals.ChessGrid,"2")): #2 => black (AI)
checked = True
for i in range(0,8):
for j in range(0,8):
#if piece belongs to AI ( AI pieces are black)
if(globals.ChessGrid[i][j][0] == "2"):
#get moves of the piece
print("main.py - Getting moves for "+globals.ChessGrid[i][j]+' at '+str(i)+','+str(j))
moves = ai.getAllMoves(globals.ChessGrid,i,j,globals.ChessGrid[i][j][1],globals.ChessGrid[i][j][0])
print("moves are :")
print(moves)
# print()
#for every possible move
for move in moves:
#copy the grid status to a new list
print("playing move "+str(move))
NewChessGrid = deepcopy(globals.ChessGrid)
#perform the move
NewChessGrid[move[0]][move[1]] = NewChessGrid[i][j]
NewChessGrid[i][j] = "0"
#if ai was previously checked and after the move is still checked then don't allow the move
if(ai.kingChecked(NewChessGrid,"2")):
continue;
#perform MINIMAX/ALPHA-BETA
#send chesssgrid and alpha/beta value
moveHeuristic = -10000
moveHeuristic = ai.AI_Move(NewChessGrid,moveHeuristic)
print("back to main.py moveHeuristic = "+str(moveHeuristic) + " checking "+globals.ChessGrid[i][j])
#set the static variable controlling depth of recursion of minimax to 0
ai.AI_Move.depth = 0
#if heuristic is same as previous then chose generated move by choosenRandomMoveChance% chance
if(moveHeuristic == heuristic):
randomInt = randint(1,100)
if(randomInt > globals.chooseRandomMoveChance):
print("MOVE CHOSEN by "+str(randomInt))
makeMove = [i,j,move[0],move[1]]
#if better heuristic from move choose this move
if(moveHeuristic < heuristic or (move == [0,0,0,0])):
print("MOVE CHOSEN")
heuristic = moveHeuristic
makeMove = [i,j,move[0],move[1]]
#chose this move if no move has been chosen till now and the king is not checked
#precautionary block in case the above code somehow does not pick a move which shouldn't happen hopefully
if(noMove and (not checked)):
print("MOVE CHOSEN")
heuristic = moveHeuristic
makeMove = [i,j,move[0],move[1]]
noMove = False
if(checked):
print("GAME OVER")
globals.ChessGrid[makeMove[2]][makeMove[3]] = globals.ChessGrid[makeMove[0]][makeMove[1]]
globals.ChessGrid[makeMove[0]][makeMove[1]] = "0"
#globals.PrintGrid(globals.ChessGrid)
self.CreateImage(makeMove[2],makeMove[3],makeMove[0],makeMove[1],globals.ChessGrid[makeMove[2]][makeMove[3]])
else:
globals.TURN = 2
else:
globals.TURN = 1
globals.PieceSelected = False
def CreateImage(self,toRow,toColumn,fromRow,fromColumn,Piece):
print('=================================================')
print("moving "+str(Piece)+" from "+str(fromRow)+','+str(fromColumn)+" to "+str(toRow)+','+str(toColumn))
print('=================================================')
#create image at new location
if(Piece == "11"):
self.ChessCells[toRow][toColumn].configure(image = globals.ImageDict["WhitePawn"])
elif(Piece == "12"):
self.ChessCells[toRow][toColumn].configure(image = globals.ImageDict["WhiteRook"])
elif(Piece == "13"):
self.ChessCells[toRow][toColumn].configure(image = globals.ImageDict["WhiteBishop"])
elif(Piece == "14"):
self.ChessCells[toRow][toColumn].configure(image = globals.ImageDict["WhiteKnight"])
elif(Piece == "15"):
self.ChessCells[toRow][toColumn].configure(image = globals.ImageDict["WhiteQueen"])
elif(Piece == "16"):
self.ChessCells[toRow][toColumn].configure(image = globals.ImageDict["WhiteKing"])
elif(Piece == "21"):
self.ChessCells[toRow][toColumn].configure(image = globals.ImageDict["BlackPawn"])
elif(Piece == "22"):
self.ChessCells[toRow][toColumn].configure(image = globals.ImageDict["BlackRook"])
elif(Piece == "23"):
self.ChessCells[toRow][toColumn].configure(image = globals.ImageDict["BlackBishop"])
elif(Piece == "24"):
self.ChessCells[toRow][toColumn].configure(image = globals.ImageDict["BlackKnight"])
elif(Piece == "25"):
self.ChessCells[toRow][toColumn].configure(image = globals.ImageDict["BlackQueen"])
elif(Piece == "26"):
self.ChessCells[toRow][toColumn].configure(image = globals.ImageDict["BlackKing"])
#remove image at old location
self.ChessCells[fromRow][fromColumn].configure(image=globals.ImageDict["Transparent"])
#initialize Image dictionary objects
def InitializePictures():
path = "img/"
globals.ImageDict["BlackPawn"] = PhotoImage(file=path+"black-pawn.png",width="60",height="60")
globals.ImageDict["WhitePawn"] = PhotoImage(file=path+"white-pawn.png",width="60",height="60")
globals.ImageDict["BlackBishop"] = PhotoImage(file=path+"black-bishop.png",width="60",height="60")
globals.ImageDict["WhiteBishop"] = PhotoImage(file=path+"white-bishop.png",width="60",height="60")
globals.ImageDict["BlackQueen"] = PhotoImage(file=path+"black-queen.png",width="60",height="60")
globals.ImageDict["WhiteQueen"] = PhotoImage(file=path+"white-queen.png",width="60",height="60")
globals.ImageDict["BlackKing"] = PhotoImage(file=path+"black-king.png",width="60",height="60")
globals.ImageDict["WhiteKing"] = PhotoImage(file=path+"white-king.png",width="60",height="60")
globals.ImageDict["BlackRook"] = PhotoImage(file=path+"black-rook.png",width="60",height="60")
globals.ImageDict["WhiteRook"] = PhotoImage(file=path+"white-rook.png",width="60",height="60")
globals.ImageDict["BlackKnight"] = PhotoImage(file=path+"black-knight.png",width="60",height="60")
globals.ImageDict["WhiteKnight"] = PhotoImage(file=path+"white-knight.png",width="60",height="60")
globals.ImageDict["WhiteBlank"] = PhotoImage(file=path+"white-blank.png",width="60",height="60")
globals.ImageDict["GrayBlank"] = PhotoImage(file=path+"gray-blank.png",width="60",height="60")
globals.ImageDict["Transparent"] = PhotoImage(file=path+"transparent.png",width="60",height="60")
def main():
#intialize global variables
globals.init()
root = Tk()
root.geometry("600x600+200+200")
#initialize image dictionary
InitializePictures()
game = MainWindow(root)
game.master.title("Chess")
root.mainloop()
if __name__ == "__main__":
main()