-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChessMainOffline.py
176 lines (118 loc) · 5.01 KB
/
ChessMainOffline.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
"""
Allows to play offline against the chess engine with a GUI.
"""
import time
import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame as pyg
from src.GameState import GameState
from src.Move import Move
from src.ChessEngine import ChessEngine
width = height = 1024
dimensions = 8
sq_size = height / dimensions
maxFPS = 60
images = {} # initializes a set of images
def main():
pyg.init()
screen = pyg.display.set_mode((width, height))
clock = pyg.time.Clock()
screen.fill(pyg.Color("white"))
gs = GameState()
validMoves = gs.getValidMoves(gs.whiteToMove, False)
moveMade = False # flag variable for when a move is made
loadImages()
running = True
sqSelected = () # tuple keeping track of last square clicked
playerClicks = [] # track two mouse clicks
while running:
for i in pyg.event.get():
if i.type == pyg.QUIT: # keyboard clicks
running = False
#play against ChessEngine
if not gs.whiteToMove:
if len(validMoves) > 0:
move = ChessEngine.computerMoveProoo(gs)
print(round(ChessEngine.evaluation(gs, gs.whiteToMove), 2))
print(move.getChessNotation())
moveMade = True
if moveMade:
validMoves = gs.getValidMoves(gs.whiteToMove, False) # detects checkmate, stalemate
if gs.checkMate:
print("MATE")
moveMade = False
elif i.type == pyg.KEYDOWN:
if i.key == pyg.K_LEFT:
gs.undoMove()
moveMade = True
print("undo")
#if i.key == pyg.K_RIGHT:
#gs.redoMove()
#print("redo")
# COMPUTER MOVES
if i.key == pyg.K_e:
for i in range(0, 100):
if len(validMoves) > 0:
#gs.nodes = 0
start = time.time()
if gs.whiteToMove:
move = ChessEngine.computerMovePro(gs)
else:
move = ChessEngine.computerMove(gs)
print(move.getChessNotation())
done = time.time()
#print("nodes: " + str(gs.nodes))#str(round((gs.nodes / (done - start)))))
moveMade = True
#print(gs.evaluation())
drawGameState(screen, gs)
clock.tick(maxFPS)
pyg.display.flip()
elif i.type == pyg.MOUSEBUTTONDOWN: # mouse clicks
location = pyg.mouse.get_pos() # x, y location of mouse
col = int(location[0] // sq_size)
row = int(location[1] // sq_size)
if sqSelected == (row, col):
sqSelected == () # unselect square
playerClicks = []
else:
sqSelected = (row, col)
playerClicks.append(sqSelected)
if len(playerClicks) == 2:
move = Move(playerClicks[0], playerClicks[1], gs.board)
if move in validMoves:
print(move.getChessNotation())
gs.makeMove(move)
moveMade = True
sqSelected = ()
playerClicks = []
else: # fixes double click bug
playerClicks = [sqSelected]
if moveMade:
validMoves = gs.getValidMoves(gs.whiteToMove, False) # detects checkmate, stalemate
if gs.checkMate:
print("MATE")
moveMade = False
drawGameState(screen, gs)
clock.tick(maxFPS)
pyg.display.flip()
def loadImages():
pieces = ["wP", "wR", "wN", "wB", "wQ", "wK", "bP", "bR", "bN", "bB", "bQ", "bK"]
for piece in pieces:
images[piece] = pyg.transform.scale(pyg.image.load("images/" + piece + ".png"), (int(sq_size), int(sq_size)))
def drawGameState(screen, gs):
drawBoard(screen)
drawPieces(screen, gs.board)
def drawBoard(screen):
colors = [pyg.Color("gray"), pyg.Color("dark green")]
for i in range(dimensions): # rows
for j in range(dimensions): # columns
color = colors[((i + j) % 2)]
pyg.draw.rect(screen, color, pyg.Rect(j * sq_size, i * sq_size, sq_size, sq_size))
def drawPieces(screen, board):
for i in range(dimensions):
for j in range(dimensions):
piece = board[i][j]
if piece != "--": # not empty square
screen.blit(images[piece], pyg.Rect(j * sq_size, i * sq_size, sq_size, sq_size))
if __name__ == "__main__":
main()