Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,324 +1,324 @@
|
|
| 1 |
-
import chess
|
| 2 |
-
import chess.svg
|
| 3 |
-
import pygame
|
| 4 |
-
import engine
|
| 5 |
-
from pygame import Vector2
|
| 6 |
-
import move_finder
|
| 7 |
-
from multiprocessing import Process,Queue
|
| 8 |
-
from final import get_board
|
| 9 |
-
|
| 10 |
-
move_width =200
|
| 11 |
-
move_height=0
|
| 12 |
-
width,height=512,512 #can be 768,768
|
| 13 |
-
dimensions = 8 #chess board is 64 squares
|
| 14 |
-
sq_size = int(height/dimensions)
|
| 15 |
-
max_fps=15
|
| 16 |
-
images ={}
|
| 17 |
-
#load images
|
| 18 |
-
#loading image sis very expensive so load only once per game
|
| 19 |
-
# board = chess.Board('rnbqkbnr/8/8/8/8/8/8/8')
|
| 20 |
-
# svg = chess.svg.board(board)
|
| 21 |
-
|
| 22 |
-
# make engine that recognize legal chess move or not
|
| 23 |
-
#hopefully 2 player game
|
| 24 |
-
# with open('b.svg', 'w', encoding="utf-8") as f:
|
| 25 |
-
# f.write(svg)
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
'''
|
| 29 |
-
load images in global dictionary .
|
| 30 |
-
called exactly on the main
|
| 31 |
-
'''
|
| 32 |
-
def load_images():
|
| 33 |
-
peices=['bQ','bK','bB','bN','bR','wQ','wK','wB','wN','wR','bp','wp']
|
| 34 |
-
for peice in peices:
|
| 35 |
-
images[peice] = pygame.transform.scale(pygame.image.load("./images/"+peice+".png"),(sq_size,sq_size)) #cenetr peice nicely
|
| 36 |
-
|
| 37 |
-
# we can access an peice by calling image['wp] we added them in the dictionary
|
| 38 |
-
|
| 39 |
-
'''
|
| 40 |
-
draw squares on board
|
| 41 |
-
always top left square is white
|
| 42 |
-
'''
|
| 43 |
-
def draw_board(screen):
|
| 44 |
-
global colors #so that we can use them globally
|
| 45 |
-
colors = [pygame.Color('white'),pygame.Color(194, 194, 194)]
|
| 46 |
-
for r in range(dimensions):
|
| 47 |
-
for c in range(dimensions):
|
| 48 |
-
parity = (r+c) & 1
|
| 49 |
-
color = colors[parity]
|
| 50 |
-
pygame.draw.rect(screen,color,pygame.Rect(c*sq_size,r*sq_size,sq_size,sq_size))
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
'''
|
| 54 |
-
draw peices using current game state (board)
|
| 55 |
-
'''
|
| 56 |
-
def draw_peices(screen,board):
|
| 57 |
-
for r in range(dimensions):
|
| 58 |
-
for c in range(dimensions):
|
| 59 |
-
peice = board[r][c]
|
| 60 |
-
if peice !='--':
|
| 61 |
-
screen.blit(images[peice],pygame.Rect(c*sq_size,r*sq_size,sq_size,sq_size))
|
| 62 |
-
|
| 63 |
-
import pygame
|
| 64 |
-
|
| 65 |
-
scroll_offset = 0 # global scroll variable
|
| 66 |
-
|
| 67 |
-
def draw_move_log(screen, gs, width, move_width, height):
|
| 68 |
-
global scroll_offset
|
| 69 |
-
font = pygame.font.SysFont('Arial', 16, False, False)
|
| 70 |
-
move_log_rect = pygame.Rect(width, 0, move_width, height)
|
| 71 |
-
|
| 72 |
-
# Draw background
|
| 73 |
-
pygame.draw.rect(screen, pygame.Color('black'), move_log_rect)
|
| 74 |
-
|
| 75 |
-
moves = gs.moveLog
|
| 76 |
-
text_y = 5 - scroll_offset # apply scroll offset here
|
| 77 |
-
|
| 78 |
-
for j, i in enumerate(moves):
|
| 79 |
-
text = f"{j+1}. {str(i)}"
|
| 80 |
-
text_object = font.render(text, True, pygame.Color('white'))
|
| 81 |
-
text_location = move_log_rect.move(5, text_y)
|
| 82 |
-
screen.blit(text_object, text_location)
|
| 83 |
-
text_y += text_object.get_height() + 5
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
def handle_scroll(event):
|
| 87 |
-
"""Handles mouse wheel scrolling"""
|
| 88 |
-
global scroll_offset
|
| 89 |
-
if event.type == pygame.MOUSEBUTTONDOWN:
|
| 90 |
-
if event.button == 4: # scroll up
|
| 91 |
-
scroll_offset = max(0, scroll_offset - 20)
|
| 92 |
-
elif event.button == 5: # scroll down
|
| 93 |
-
scroll_offset += 20
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
'''
|
| 97 |
-
rensonsible for graphics in current game state
|
| 98 |
-
'''
|
| 99 |
-
def draw_game_state(screen,gs,valid_moves,sq_selected):
|
| 100 |
-
draw_board(screen) #draw squares on board
|
| 101 |
-
high_light_squares(screen,gs,valid_moves,sq_selected)
|
| 102 |
-
draw_peices(screen,gs.board)
|
| 103 |
-
draw_move_log(screen,gs,512,200,512)
|
| 104 |
-
'''
|
| 105 |
-
hgihlight the square selected and moves for peices selected
|
| 106 |
-
'''
|
| 107 |
-
|
| 108 |
-
def high_light_squares(screen,gs,valid_moves,sqselected):
|
| 109 |
-
if sqselected != ():
|
| 110 |
-
r,c = sqselected
|
| 111 |
-
if gs.board[r][c][0] == ('w' if gs.whiteToMove else 'b'): #sq selected is a peice
|
| 112 |
-
# highlight selected square
|
| 113 |
-
# use surface
|
| 114 |
-
s = pygame.Surface((sq_size,sq_size))
|
| 115 |
-
|
| 116 |
-
s.set_alpha(100) # transparent
|
| 117 |
-
s.fill(pygame.Color('blue'))
|
| 118 |
-
screen.blit(s,(c*sq_size,r*sq_size))
|
| 119 |
-
# highlist moves from that square
|
| 120 |
-
s.fill(pygame.Color('red'))
|
| 121 |
-
|
| 122 |
-
for move in valid_moves:
|
| 123 |
-
if move.start_row == r and move.start_col==c:
|
| 124 |
-
pygame.draw.circle(screen,pygame.Color(0,255,0),( int(sq_size*(move.end_col + 0.5)),int(sq_size*(move.end_row + 0.5))),7.5)
|
| 125 |
-
if gs.board[move.end_row][move.end_col][0]== ('b' if gs.whiteToMove else 'w'):
|
| 126 |
-
screen.blit(s,(sq_size*move.end_col,sq_size*move.end_row))
|
| 127 |
-
if len(gs.moveLog)>=1:
|
| 128 |
-
prev_move= gs.moveLog[-1]
|
| 129 |
-
s.set_alpha(100) # transparent
|
| 130 |
-
s.fill(pygame.Color('dark green'))
|
| 131 |
-
r,c = prev_move.end_row,prev_move.end_col
|
| 132 |
-
screen.blit(s,(c*sq_size,r*sq_size))
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
#what the board does is redraw images when u move
|
| 137 |
-
#animation is simply slow the change such that u see every frame
|
| 138 |
-
|
| 139 |
-
def main():
|
| 140 |
-
pygame.init()
|
| 141 |
-
screen = pygame.display.set_mode((width+move_width,height+move_height))
|
| 142 |
-
clock = pygame.time.Clock() #clock
|
| 143 |
-
screen.fill(pygame.Color('white'))
|
| 144 |
-
gs = engine.GameState() #create a game state and craete variables
|
| 145 |
-
load_images() # load only once before whilw
|
| 146 |
-
running = True
|
| 147 |
-
sqselected = ()
|
| 148 |
-
player_clicks=[] #two squares of player clicks
|
| 149 |
-
valid_moves = gs.get_valid_moves()
|
| 150 |
-
game_over=False
|
| 151 |
-
player_one = True # white true , machine is playing false
|
| 152 |
-
player_two = False # similarly but for player two
|
| 153 |
-
ai_thinking = False
|
| 154 |
-
move_finder_procee = None
|
| 155 |
-
move_undone = False
|
| 156 |
-
if len(valid_moves)<=5:
|
| 157 |
-
for move in valid_moves:
|
| 158 |
-
print(move.peice_captured ,move.peice_moved, move.id)
|
| 159 |
-
move_made = False #until the valid move we need not generate valid moves
|
| 160 |
-
# make ui changes
|
| 161 |
-
animate=False
|
| 162 |
-
while running:
|
| 163 |
-
human_Turn = (gs.whiteToMove and player_one) or (not gs.whiteToMove and player_two)
|
| 164 |
-
for e in pygame.event.get():
|
| 165 |
-
#mouse handler
|
| 166 |
-
if e.type == pygame.QUIT:
|
| 167 |
-
running=False
|
| 168 |
-
elif e.type == pygame.MOUSEBUTTONDOWN:
|
| 169 |
-
if not game_over and human_Turn:
|
| 170 |
-
location =pygame.mouse.get_pos() #location of mouse
|
| 171 |
-
col = int(location[0]//sq_size)
|
| 172 |
-
row = int(location[1]//sq_size)
|
| 173 |
-
if sqselected == (row,col) or col>=8: #user click same square then unmove
|
| 174 |
-
sqselected=()
|
| 175 |
-
player_clicks=[]
|
| 176 |
-
else:
|
| 177 |
-
sqselected = (row,col)
|
| 178 |
-
player_clicks.append(sqselected) # append for both first and second cicks
|
| 179 |
-
if len(player_clicks)==2: #after the second click
|
| 180 |
-
move = engine.Move(player_clicks[0],player_clicks[1],gs.board)
|
| 181 |
-
for i in range(len(valid_moves)):
|
| 182 |
-
if move==valid_moves[i]:#move is pretty cheap
|
| 183 |
-
print("move taken",move.get_chess_notation(),"peice_moved:",gs.board[move.start_row][move.start_col])
|
| 184 |
-
gs.make_move(valid_moves[i])
|
| 185 |
-
move_made=True
|
| 186 |
-
animate=True
|
| 187 |
-
sqselected=()
|
| 188 |
-
player_clicks=[]
|
| 189 |
-
if not move_made:
|
| 190 |
-
print("invalid_move",move.get_chess_notation(),move.peice_captured,move.peice_moved)
|
| 191 |
-
player_clicks=[sqselected] #after move is doen reset squares
|
| 192 |
-
if gs.check_mate or gs.steale_mate:
|
| 193 |
-
running=False
|
| 194 |
-
|
| 195 |
-
#keyboard handlers
|
| 196 |
-
elif e.type == pygame.KEYDOWN:
|
| 197 |
-
if e.key == pygame.K_z:
|
| 198 |
-
gs.undo_move()
|
| 199 |
-
move_made=True
|
| 200 |
-
game_over=False
|
| 201 |
-
if ai_thinking:
|
| 202 |
-
move_find_process.terminate()
|
| 203 |
-
ai_thinking=False
|
| 204 |
-
move_undone=True
|
| 205 |
-
|
| 206 |
-
elif e.key == pygame.K_r:
|
| 207 |
-
gs = engine.GameState()
|
| 208 |
-
valid_moves=gs.get_valid_moves()
|
| 209 |
-
sqselected=()
|
| 210 |
-
player_clicks=[]
|
| 211 |
-
move_made=False
|
| 212 |
-
animate=True
|
| 213 |
-
game_over=False
|
| 214 |
-
if ai_thinking:
|
| 215 |
-
move_find_process.terminate()
|
| 216 |
-
ai_thinking=False
|
| 217 |
-
move_undone=True
|
| 218 |
-
#reset the board
|
| 219 |
-
# best moves
|
| 220 |
-
if not game_over and not human_Turn and not move_undone:
|
| 221 |
-
if not ai_thinking:
|
| 222 |
-
ai_thinking = True # threads wont share data
|
| 223 |
-
returnQueue = Queue() # used to pass data between threads
|
| 224 |
-
move_find_process = Process(target=move_finder.find_best_move,args=(gs,valid_moves,returnQueue)) # passing function as parameter
|
| 225 |
-
|
| 226 |
-
move_find_process.start() #creates new thread without waiting this code tun
|
| 227 |
-
if not move_find_process.is_alive():
|
| 228 |
-
print('done thinking')
|
| 229 |
-
move = returnQueue.get()
|
| 230 |
-
if move is None:
|
| 231 |
-
move = move_finder.random_move(valid_moves)
|
| 232 |
-
gs.make_move(move)
|
| 233 |
-
move_made = True
|
| 234 |
-
animate = True
|
| 235 |
-
ai_thinking = False
|
| 236 |
-
if move_made:
|
| 237 |
-
valid_moves = gs.get_valid_moves()
|
| 238 |
-
if animate:
|
| 239 |
-
animateMove(gs.moveLog[-1],screen,gs.board,clock)
|
| 240 |
-
animate=False
|
| 241 |
-
print('valid_moves:',len(valid_moves))
|
| 242 |
-
if len(valid_moves)<=5:
|
| 243 |
-
for move in valid_moves:
|
| 244 |
-
print(move.peice_captured ,move.peice_moved, move.move_id)
|
| 245 |
-
|
| 246 |
-
move_made=False
|
| 247 |
-
move_undone = False
|
| 248 |
-
|
| 249 |
-
draw_game_state(screen,gs,valid_moves,sqselected) #add mouse hnadlers
|
| 250 |
-
if gs.check_mate:
|
| 251 |
-
game_over=True
|
| 252 |
-
if gs.whiteToMove:
|
| 253 |
-
draw_end_game_text(screen,'black wins by checkmate')
|
| 254 |
-
else:
|
| 255 |
-
draw_end_game_text(screen,"white wins by checkmate")
|
| 256 |
-
elif gs.steale_mate:
|
| 257 |
-
game_over=True
|
| 258 |
-
draw_end_game_text(screen,'stealmate no moves for king and no check')
|
| 259 |
-
clock.tick(max_fps)
|
| 260 |
-
pygame.display.flip()
|
| 261 |
-
from pygame.math import Vector2
|
| 262 |
-
|
| 263 |
-
def animateMove(move, screen, board, clock):
|
| 264 |
-
start = Vector2(move.start_col, move.start_row)
|
| 265 |
-
end = Vector2(move.end_col, move.end_row)
|
| 266 |
-
distance = end.distance_to(start)
|
| 267 |
-
frames_per_sq = 10
|
| 268 |
-
frame_count = int(distance * frames_per_sq)
|
| 269 |
-
|
| 270 |
-
for frame in range(frame_count + 1):
|
| 271 |
-
t = frame / frame_count # in [0, 1]
|
| 272 |
-
current = start.lerp(end, t) # linear interpolation
|
| 273 |
-
c, r = current.x, current.y
|
| 274 |
-
|
| 275 |
-
draw_board(screen)
|
| 276 |
-
draw_peices(screen, board)
|
| 277 |
-
|
| 278 |
-
# erase ending square
|
| 279 |
-
colour = colors[(move.end_row + move.end_col) & 1]
|
| 280 |
-
end_sq = pygame.Rect(move.end_col * sq_size, move.end_row * sq_size, sq_size, sq_size)
|
| 281 |
-
pygame.draw.rect(screen, colour, end_sq)
|
| 282 |
-
|
| 283 |
-
# draw captured piece if any
|
| 284 |
-
if move.peice_captured != '--':
|
| 285 |
-
if move.is_empassant_move:
|
| 286 |
-
screen.blit(images[move.peice_captured], end_sq)
|
| 287 |
-
else:
|
| 288 |
-
screen.blit(images[move.peice_captured], end_sq)
|
| 289 |
-
|
| 290 |
-
# draw moving piece at interpolated position
|
| 291 |
-
screen.blit(images[move.peice_moved],
|
| 292 |
-
pygame.Rect(c * sq_size, r * sq_size, sq_size, sq_size))
|
| 293 |
-
|
| 294 |
-
pygame.display.flip()
|
| 295 |
-
clock.tick(120)
|
| 296 |
-
|
| 297 |
-
def draw_end_game_text(screen,text):
|
| 298 |
-
font = pygame.font.SysFont('Helvitca',32,True,False)
|
| 299 |
-
text_object = font.render(text,0,pygame.Color('Gray'))
|
| 300 |
-
text_location = pygame.Rect(0,0,width,height).move(width/2,height/2)
|
| 301 |
-
screen.blit(text_object,text_location)
|
| 302 |
-
text_object = font.render(text,0,pygame.Color('Black'))
|
| 303 |
-
screen.blit(text_object,text_location.move(2,2))
|
| 304 |
-
|
| 305 |
-
def get_moves(image_path):
|
| 306 |
-
board,fen = get_board(image_path)
|
| 307 |
-
gs= engine.GameState(board)
|
| 308 |
-
moves = move_finder.get_best_n_moves(gs)
|
| 309 |
-
|
| 310 |
-
gs.whiteToMove = not gs.whiteToMove
|
| 311 |
-
moves2 = move_finder.get_best_n_moves(gs)
|
| 312 |
-
colour = ('white_moves','black_moves') if not gs.whiteToMove else ('black_moves','white_moves')
|
| 313 |
-
return fen, {colour[0]:moves,colour[1]:moves2}
|
| 314 |
-
|
| 315 |
-
if __name__=='__main__':
|
| 316 |
-
#handle and update graphics and input
|
| 317 |
-
#whenever u import this module else where this wont run this fucntion
|
| 318 |
-
#main()
|
| 319 |
-
get_moves('./image.png')
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
|
|
|
| 1 |
+
import chess
|
| 2 |
+
import chess.svg
|
| 3 |
+
import pygame
|
| 4 |
+
import engine
|
| 5 |
+
from pygame import Vector2
|
| 6 |
+
import move_finder
|
| 7 |
+
from multiprocessing import Process,Queue
|
| 8 |
+
from final import get_board
|
| 9 |
+
|
| 10 |
+
move_width =200
|
| 11 |
+
move_height=0
|
| 12 |
+
width,height=512,512 #can be 768,768
|
| 13 |
+
dimensions = 8 #chess board is 64 squares
|
| 14 |
+
sq_size = int(height/dimensions)
|
| 15 |
+
max_fps=15
|
| 16 |
+
images ={}
|
| 17 |
+
#load images
|
| 18 |
+
#loading image sis very expensive so load only once per game
|
| 19 |
+
# board = chess.Board('rnbqkbnr/8/8/8/8/8/8/8')
|
| 20 |
+
# svg = chess.svg.board(board)
|
| 21 |
+
|
| 22 |
+
# make engine that recognize legal chess move or not
|
| 23 |
+
#hopefully 2 player game
|
| 24 |
+
# with open('b.svg', 'w', encoding="utf-8") as f:
|
| 25 |
+
# f.write(svg)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
'''
|
| 29 |
+
load images in global dictionary .
|
| 30 |
+
called exactly on the main
|
| 31 |
+
'''
|
| 32 |
+
def load_images():
|
| 33 |
+
peices=['bQ','bK','bB','bN','bR','wQ','wK','wB','wN','wR','bp','wp']
|
| 34 |
+
for peice in peices:
|
| 35 |
+
images[peice] = pygame.transform.scale(pygame.image.load("./images/"+peice+".png"),(sq_size,sq_size)) #cenetr peice nicely
|
| 36 |
+
|
| 37 |
+
# we can access an peice by calling image['wp] we added them in the dictionary
|
| 38 |
+
|
| 39 |
+
'''
|
| 40 |
+
draw squares on board
|
| 41 |
+
always top left square is white
|
| 42 |
+
'''
|
| 43 |
+
def draw_board(screen):
|
| 44 |
+
global colors #so that we can use them globally
|
| 45 |
+
colors = [pygame.Color('white'),pygame.Color(194, 194, 194)]
|
| 46 |
+
for r in range(dimensions):
|
| 47 |
+
for c in range(dimensions):
|
| 48 |
+
parity = (r+c) & 1
|
| 49 |
+
color = colors[parity]
|
| 50 |
+
pygame.draw.rect(screen,color,pygame.Rect(c*sq_size,r*sq_size,sq_size,sq_size))
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
'''
|
| 54 |
+
draw peices using current game state (board)
|
| 55 |
+
'''
|
| 56 |
+
def draw_peices(screen,board):
|
| 57 |
+
for r in range(dimensions):
|
| 58 |
+
for c in range(dimensions):
|
| 59 |
+
peice = board[r][c]
|
| 60 |
+
if peice !='--':
|
| 61 |
+
screen.blit(images[peice],pygame.Rect(c*sq_size,r*sq_size,sq_size,sq_size))
|
| 62 |
+
|
| 63 |
+
import pygame
|
| 64 |
+
|
| 65 |
+
scroll_offset = 0 # global scroll variable
|
| 66 |
+
|
| 67 |
+
def draw_move_log(screen, gs, width, move_width, height):
|
| 68 |
+
global scroll_offset
|
| 69 |
+
font = pygame.font.SysFont('Arial', 16, False, False)
|
| 70 |
+
move_log_rect = pygame.Rect(width, 0, move_width, height)
|
| 71 |
+
|
| 72 |
+
# Draw background
|
| 73 |
+
pygame.draw.rect(screen, pygame.Color('black'), move_log_rect)
|
| 74 |
+
|
| 75 |
+
moves = gs.moveLog
|
| 76 |
+
text_y = 5 - scroll_offset # apply scroll offset here
|
| 77 |
+
|
| 78 |
+
for j, i in enumerate(moves):
|
| 79 |
+
text = f"{j+1}. {str(i)}"
|
| 80 |
+
text_object = font.render(text, True, pygame.Color('white'))
|
| 81 |
+
text_location = move_log_rect.move(5, text_y)
|
| 82 |
+
screen.blit(text_object, text_location)
|
| 83 |
+
text_y += text_object.get_height() + 5
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def handle_scroll(event):
|
| 87 |
+
"""Handles mouse wheel scrolling"""
|
| 88 |
+
global scroll_offset
|
| 89 |
+
if event.type == pygame.MOUSEBUTTONDOWN:
|
| 90 |
+
if event.button == 4: # scroll up
|
| 91 |
+
scroll_offset = max(0, scroll_offset - 20)
|
| 92 |
+
elif event.button == 5: # scroll down
|
| 93 |
+
scroll_offset += 20
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
'''
|
| 97 |
+
rensonsible for graphics in current game state
|
| 98 |
+
'''
|
| 99 |
+
def draw_game_state(screen,gs,valid_moves,sq_selected):
|
| 100 |
+
draw_board(screen) #draw squares on board
|
| 101 |
+
high_light_squares(screen,gs,valid_moves,sq_selected)
|
| 102 |
+
draw_peices(screen,gs.board)
|
| 103 |
+
draw_move_log(screen,gs,512,200,512)
|
| 104 |
+
'''
|
| 105 |
+
hgihlight the square selected and moves for peices selected
|
| 106 |
+
'''
|
| 107 |
+
|
| 108 |
+
def high_light_squares(screen,gs,valid_moves,sqselected):
|
| 109 |
+
if sqselected != ():
|
| 110 |
+
r,c = sqselected
|
| 111 |
+
if gs.board[r][c][0] == ('w' if gs.whiteToMove else 'b'): #sq selected is a peice
|
| 112 |
+
# highlight selected square
|
| 113 |
+
# use surface
|
| 114 |
+
s = pygame.Surface((sq_size,sq_size))
|
| 115 |
+
|
| 116 |
+
s.set_alpha(100) # transparent
|
| 117 |
+
s.fill(pygame.Color('blue'))
|
| 118 |
+
screen.blit(s,(c*sq_size,r*sq_size))
|
| 119 |
+
# highlist moves from that square
|
| 120 |
+
s.fill(pygame.Color('red'))
|
| 121 |
+
|
| 122 |
+
for move in valid_moves:
|
| 123 |
+
if move.start_row == r and move.start_col==c:
|
| 124 |
+
pygame.draw.circle(screen,pygame.Color(0,255,0),( int(sq_size*(move.end_col + 0.5)),int(sq_size*(move.end_row + 0.5))),7.5)
|
| 125 |
+
if gs.board[move.end_row][move.end_col][0]== ('b' if gs.whiteToMove else 'w'):
|
| 126 |
+
screen.blit(s,(sq_size*move.end_col,sq_size*move.end_row))
|
| 127 |
+
if len(gs.moveLog)>=1:
|
| 128 |
+
prev_move= gs.moveLog[-1]
|
| 129 |
+
s.set_alpha(100) # transparent
|
| 130 |
+
s.fill(pygame.Color('dark green'))
|
| 131 |
+
r,c = prev_move.end_row,prev_move.end_col
|
| 132 |
+
screen.blit(s,(c*sq_size,r*sq_size))
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
#what the board does is redraw images when u move
|
| 137 |
+
#animation is simply slow the change such that u see every frame
|
| 138 |
+
|
| 139 |
+
def main():
|
| 140 |
+
pygame.init()
|
| 141 |
+
screen = pygame.display.set_mode((width+move_width,height+move_height))
|
| 142 |
+
clock = pygame.time.Clock() #clock
|
| 143 |
+
screen.fill(pygame.Color('white'))
|
| 144 |
+
gs = engine.GameState() #create a game state and craete variables
|
| 145 |
+
load_images() # load only once before whilw
|
| 146 |
+
running = True
|
| 147 |
+
sqselected = ()
|
| 148 |
+
player_clicks=[] #two squares of player clicks
|
| 149 |
+
valid_moves = gs.get_valid_moves()
|
| 150 |
+
game_over=False
|
| 151 |
+
player_one = True # white true , machine is playing false
|
| 152 |
+
player_two = False # similarly but for player two
|
| 153 |
+
ai_thinking = False
|
| 154 |
+
move_finder_procee = None
|
| 155 |
+
move_undone = False
|
| 156 |
+
if len(valid_moves)<=5:
|
| 157 |
+
for move in valid_moves:
|
| 158 |
+
print(move.peice_captured ,move.peice_moved, move.id)
|
| 159 |
+
move_made = False #until the valid move we need not generate valid moves
|
| 160 |
+
# make ui changes
|
| 161 |
+
animate=False
|
| 162 |
+
while running:
|
| 163 |
+
human_Turn = (gs.whiteToMove and player_one) or (not gs.whiteToMove and player_two)
|
| 164 |
+
for e in pygame.event.get():
|
| 165 |
+
#mouse handler
|
| 166 |
+
if e.type == pygame.QUIT:
|
| 167 |
+
running=False
|
| 168 |
+
elif e.type == pygame.MOUSEBUTTONDOWN:
|
| 169 |
+
if not game_over and human_Turn:
|
| 170 |
+
location =pygame.mouse.get_pos() #location of mouse
|
| 171 |
+
col = int(location[0]//sq_size)
|
| 172 |
+
row = int(location[1]//sq_size)
|
| 173 |
+
if sqselected == (row,col) or col>=8: #user click same square then unmove
|
| 174 |
+
sqselected=()
|
| 175 |
+
player_clicks=[]
|
| 176 |
+
else:
|
| 177 |
+
sqselected = (row,col)
|
| 178 |
+
player_clicks.append(sqselected) # append for both first and second cicks
|
| 179 |
+
if len(player_clicks)==2: #after the second click
|
| 180 |
+
move = engine.Move(player_clicks[0],player_clicks[1],gs.board)
|
| 181 |
+
for i in range(len(valid_moves)):
|
| 182 |
+
if move==valid_moves[i]:#move is pretty cheap
|
| 183 |
+
print("move taken",move.get_chess_notation(),"peice_moved:",gs.board[move.start_row][move.start_col])
|
| 184 |
+
gs.make_move(valid_moves[i])
|
| 185 |
+
move_made=True
|
| 186 |
+
animate=True
|
| 187 |
+
sqselected=()
|
| 188 |
+
player_clicks=[]
|
| 189 |
+
if not move_made:
|
| 190 |
+
print("invalid_move",move.get_chess_notation(),move.peice_captured,move.peice_moved)
|
| 191 |
+
player_clicks=[sqselected] #after move is doen reset squares
|
| 192 |
+
if gs.check_mate or gs.steale_mate:
|
| 193 |
+
running=False
|
| 194 |
+
|
| 195 |
+
#keyboard handlers
|
| 196 |
+
elif e.type == pygame.KEYDOWN:
|
| 197 |
+
if e.key == pygame.K_z:
|
| 198 |
+
gs.undo_move()
|
| 199 |
+
move_made=True
|
| 200 |
+
game_over=False
|
| 201 |
+
if ai_thinking:
|
| 202 |
+
move_find_process.terminate()
|
| 203 |
+
ai_thinking=False
|
| 204 |
+
move_undone=True
|
| 205 |
+
|
| 206 |
+
elif e.key == pygame.K_r:
|
| 207 |
+
gs = engine.GameState()
|
| 208 |
+
valid_moves=gs.get_valid_moves()
|
| 209 |
+
sqselected=()
|
| 210 |
+
player_clicks=[]
|
| 211 |
+
move_made=False
|
| 212 |
+
animate=True
|
| 213 |
+
game_over=False
|
| 214 |
+
if ai_thinking:
|
| 215 |
+
move_find_process.terminate()
|
| 216 |
+
ai_thinking=False
|
| 217 |
+
move_undone=True
|
| 218 |
+
#reset the board
|
| 219 |
+
# best moves
|
| 220 |
+
if not game_over and not human_Turn and not move_undone:
|
| 221 |
+
if not ai_thinking:
|
| 222 |
+
ai_thinking = True # threads wont share data
|
| 223 |
+
returnQueue = Queue() # used to pass data between threads
|
| 224 |
+
move_find_process = Process(target=move_finder.find_best_move,args=(gs,valid_moves,returnQueue)) # passing function as parameter
|
| 225 |
+
|
| 226 |
+
move_find_process.start() #creates new thread without waiting this code tun
|
| 227 |
+
if not move_find_process.is_alive():
|
| 228 |
+
print('done thinking')
|
| 229 |
+
move = returnQueue.get()
|
| 230 |
+
if move is None:
|
| 231 |
+
move = move_finder.random_move(valid_moves)
|
| 232 |
+
gs.make_move(move)
|
| 233 |
+
move_made = True
|
| 234 |
+
animate = True
|
| 235 |
+
ai_thinking = False
|
| 236 |
+
if move_made:
|
| 237 |
+
valid_moves = gs.get_valid_moves()
|
| 238 |
+
if animate:
|
| 239 |
+
animateMove(gs.moveLog[-1],screen,gs.board,clock)
|
| 240 |
+
animate=False
|
| 241 |
+
print('valid_moves:',len(valid_moves))
|
| 242 |
+
if len(valid_moves)<=5:
|
| 243 |
+
for move in valid_moves:
|
| 244 |
+
print(move.peice_captured ,move.peice_moved, move.move_id)
|
| 245 |
+
|
| 246 |
+
move_made=False
|
| 247 |
+
move_undone = False
|
| 248 |
+
|
| 249 |
+
draw_game_state(screen,gs,valid_moves,sqselected) #add mouse hnadlers
|
| 250 |
+
if gs.check_mate:
|
| 251 |
+
game_over=True
|
| 252 |
+
if gs.whiteToMove:
|
| 253 |
+
draw_end_game_text(screen,'black wins by checkmate')
|
| 254 |
+
else:
|
| 255 |
+
draw_end_game_text(screen,"white wins by checkmate")
|
| 256 |
+
elif gs.steale_mate:
|
| 257 |
+
game_over=True
|
| 258 |
+
draw_end_game_text(screen,'stealmate no moves for king and no check')
|
| 259 |
+
clock.tick(max_fps)
|
| 260 |
+
pygame.display.flip()
|
| 261 |
+
from pygame.math import Vector2
|
| 262 |
+
|
| 263 |
+
def animateMove(move, screen, board, clock):
|
| 264 |
+
start = Vector2(move.start_col, move.start_row)
|
| 265 |
+
end = Vector2(move.end_col, move.end_row)
|
| 266 |
+
distance = end.distance_to(start)
|
| 267 |
+
frames_per_sq = 10
|
| 268 |
+
frame_count = int(distance * frames_per_sq)
|
| 269 |
+
|
| 270 |
+
for frame in range(frame_count + 1):
|
| 271 |
+
t = frame / frame_count # in [0, 1]
|
| 272 |
+
current = start.lerp(end, t) # linear interpolation
|
| 273 |
+
c, r = current.x, current.y
|
| 274 |
+
|
| 275 |
+
draw_board(screen)
|
| 276 |
+
draw_peices(screen, board)
|
| 277 |
+
|
| 278 |
+
# erase ending square
|
| 279 |
+
colour = colors[(move.end_row + move.end_col) & 1]
|
| 280 |
+
end_sq = pygame.Rect(move.end_col * sq_size, move.end_row * sq_size, sq_size, sq_size)
|
| 281 |
+
pygame.draw.rect(screen, colour, end_sq)
|
| 282 |
+
|
| 283 |
+
# draw captured piece if any
|
| 284 |
+
if move.peice_captured != '--':
|
| 285 |
+
if move.is_empassant_move:
|
| 286 |
+
screen.blit(images[move.peice_captured], end_sq)
|
| 287 |
+
else:
|
| 288 |
+
screen.blit(images[move.peice_captured], end_sq)
|
| 289 |
+
|
| 290 |
+
# draw moving piece at interpolated position
|
| 291 |
+
screen.blit(images[move.peice_moved],
|
| 292 |
+
pygame.Rect(c * sq_size, r * sq_size, sq_size, sq_size))
|
| 293 |
+
|
| 294 |
+
pygame.display.flip()
|
| 295 |
+
clock.tick(120)
|
| 296 |
+
|
| 297 |
+
def draw_end_game_text(screen,text):
|
| 298 |
+
font = pygame.font.SysFont('Helvitca',32,True,False)
|
| 299 |
+
text_object = font.render(text,0,pygame.Color('Gray'))
|
| 300 |
+
text_location = pygame.Rect(0,0,width,height).move(width/2,height/2)
|
| 301 |
+
screen.blit(text_object,text_location)
|
| 302 |
+
text_object = font.render(text,0,pygame.Color('Black'))
|
| 303 |
+
screen.blit(text_object,text_location.move(2,2))
|
| 304 |
+
|
| 305 |
+
def get_moves(image_path):
|
| 306 |
+
board,fen = get_board(image_path)
|
| 307 |
+
gs= engine.GameState(board)
|
| 308 |
+
moves = move_finder.get_best_n_moves(gs)
|
| 309 |
+
gs = engine.GameState(board)
|
| 310 |
+
gs.whiteToMove = not gs.whiteToMove
|
| 311 |
+
moves2 = move_finder.get_best_n_moves(gs)
|
| 312 |
+
colour = ('white_moves','black_moves') if not gs.whiteToMove else ('black_moves','white_moves')
|
| 313 |
+
return fen, {colour[0]:moves,colour[1]:moves2}
|
| 314 |
+
|
| 315 |
+
if __name__=='__main__':
|
| 316 |
+
#handle and update graphics and input
|
| 317 |
+
#whenever u import this module else where this wont run this fucntion
|
| 318 |
+
#main()
|
| 319 |
+
get_moves('./image.png')
|
| 320 |
+
|
| 321 |
+
|
| 322 |
+
|
| 323 |
+
|
| 324 |
+
|