From a30347121a30994a0d26784fc751013bb06dd08b Mon Sep 17 00:00:00 2001 From: rapidarray1211 Date: Wed, 27 Nov 2024 13:59:28 -0500 Subject: [PATCH 1/4] Play against stockfish in gui.py --- gui.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/gui.py b/gui.py index 88538af..4032733 100644 --- a/gui.py +++ b/gui.py @@ -3,6 +3,7 @@ import chess import math from stockfish import generate_move +import random SQUARE_SIZE = 100 @@ -211,6 +212,9 @@ def main(BOARD): selected_piece = None selected_square = None moves = [] + + human_player = chess.WHITE if random.choice([True, False]) else chess.BLACK + ai_player = chess.BLACK if human_player == chess.WHITE else chess.WHITE Running = True while Running: @@ -224,11 +228,21 @@ def main(BOARD): highlight_moves(scrn, BOARD, moves, selected_square) pygame.display.flip() - if outcome is not None: # Game has ended display_game_over(scrn, outcome, BOARD) pygame.time.wait(3000) # Wait 3 seconds before closing + Running = False + continue + + if BOARD.turn == ai_player: + # AI's turn + move = generate_move(BOARD) + BOARD.push(move) + selected_square = None + selected_piece = None + moves = [] + continue for event in pygame.event.get(): # If event type is QUIT, exit the game @@ -247,7 +261,7 @@ def main(BOARD): # If there is no piece selected and a piece is clicked if selected_square is None: piece = BOARD.piece_at(clicked_square) - if piece: # There is a piece on the square + if piece and piece.color == human_player: # There is a piece on the square and it's the human's turn selected_piece = piece selected_square = clicked_square moves = get_legal_moves(BOARD, selected_square) From 128609967d681f26ffaa7782690c4918d967ac79 Mon Sep 17 00:00:00 2001 From: rapidarray1211 Date: Wed, 27 Nov 2024 14:25:54 -0500 Subject: [PATCH 2/4] init add chessengine support --- __pycache__/stockfish.cpython-312.pyc | Bin 4234 -> 4234 bytes engine/__pycache__/__init__.cpython-312.pyc | Bin 156 -> 164 bytes .../__pycache__/chessEngine.cpython-312.pyc | Bin 8903 -> 8863 bytes gui.py | 18 ++++++++---------- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/__pycache__/stockfish.cpython-312.pyc b/__pycache__/stockfish.cpython-312.pyc index 982db8107b567c6c2b67c23349af7c2915488774..cb0329d7a7929ae4f86a9c52bb97c5b982a9a83b 100644 GIT binary patch delta 19 YcmeBD>{8@9&CAQh00d4s8@U<;04&-BNdN!< delta 19 ZcmeBD>{8@9&CAQh00gg-HgYux001za1n2+& diff --git a/engine/__pycache__/__init__.cpython-312.pyc b/engine/__pycache__/__init__.cpython-312.pyc index cbdc4cdfd67211193ed4d5187af8fb854f8c9af8..ad86b8b11a12c042b2ec557d2d73ebce801aa1e9 100644 GIT binary patch delta 63 zcmbQkxP+1WG%qg~0}xEecb~{@tnBP;6%$&VT2vfUo|u=O72}ehT$-DjS5h1ko?nz* RT#%TYIx$j_@z%s@MF3jv6+!?2 delta 55 zcmZ3&IERt@G%qg~0}$-YwVKFnEN$&<6%$&VT2vg9n39+qQ=DH~l$;tWsE diff --git a/engine/__pycache__/chessEngine.cpython-312.pyc b/engine/__pycache__/chessEngine.cpython-312.pyc index 54e617df580cc990718ff6970941654e8fbcf4c0..5991f15e19007ce54f78907e52ef5386c30ba63c 100644 GIT binary patch delta 356 zcmX@^I^UK1G%qg~0}xEeci+f;m|5A)*(xTqIJKxaraUn(JuAi~Ke;qFHLs*NCOp3= zySN}RId$_(=1eBWTa$y?ym&Z(S{oQer;4CYy

z(_%{+7R4Uq{f3>I08lXFC!*?7TPnHVQd))8JTahccT0|OVQ z@(zw0ViFzhH&hKjF);F$FiyTBY#|R~d|_jdR=qBzb5Tm?x|HQbDa#$|m!+IOF|hI~ zPu3Oj;sDa$xq${uZV++jylkEcl;O-_ntV!R0t=WCC+f@rlm=U}dAX<)E2HG(S5m>8 z4@4w^HgQHWPIi#ikpeLuaEM;#kh#bqGb8j0hw=jv#ZL^}oMMw3q_dcT^yatHyO_Xw eO(x%wljpoaync_#ZSqQBGgiy500n4>k~aX$hkASf diff --git a/gui.py b/gui.py index 4032733..416a234 100644 --- a/gui.py +++ b/gui.py @@ -4,6 +4,7 @@ import math from stockfish import generate_move import random +from engine.chessEngine import ChessEngine SQUARE_SIZE = 100 @@ -12,9 +13,10 @@ #https://blog.devgenius.io/simple-interactive-chess-gui-in-python-c6d6569f7b6c +engine = ChessEngine("model/movepredictorV2_24.keras", 1, 3) +print("testing") - -def highlight_king_sqaure(scrn, outcome, BOARD): +def highlight_king_square(scrn, outcome, BOARD): # Find the position of the checkmated king king_square = None if outcome.winner == chess.WHITE: @@ -223,7 +225,7 @@ def main(BOARD): draw_board(scrn) if outcome is not None: - highlight_king_sqaure(scrn, outcome, BOARD) + highlight_king_square(scrn, outcome, BOARD) draw_pieces(scrn, BOARD) highlight_moves(scrn, BOARD, moves, selected_square) pygame.display.flip() @@ -232,16 +234,12 @@ def main(BOARD): # Game has ended display_game_over(scrn, outcome, BOARD) pygame.time.wait(3000) # Wait 3 seconds before closing - Running = False - continue + break if BOARD.turn == ai_player: # AI's turn - move = generate_move(BOARD) - BOARD.push(move) - selected_square = None - selected_piece = None - moves = [] + ai_move = engine.predict_best_move(BOARD) + BOARD.push(ai_move) continue for event in pygame.event.get(): From 2964d826b3907562bd04d15c6c282b2b399afd63 Mon Sep 17 00:00:00 2001 From: rapidarray1211 Date: Wed, 27 Nov 2024 14:28:15 -0500 Subject: [PATCH 3/4] fix path issue --- __pycache__/neural_net.cpython-312.pyc | Bin 14462 -> 14429 bytes engine/__pycache__/PeSTO.cpython-312.pyc | Bin 7134 -> 7137 bytes gui.py | 2 +- 3 files changed, 1 insertion(+), 1 deletion(-) diff --git a/__pycache__/neural_net.cpython-312.pyc b/__pycache__/neural_net.cpython-312.pyc index 448fa8d1289c83e1cb474db762437c64d1906785..3cec84f78b96d5827056e00956c5112517c5c8a2 100644 GIT binary patch delta 335 zcmexYaJPW_G%qg~0}xEici+hE%&ctZY!wq)oLW>IQ=XWYo)zPgpIn-onpaXB6P{m` zU0jfuoH{v^*;*+asHcJ929H3$SEpCEPlM|XF^LBM8v-KN1(YueC|?s$oqU>EkC{PG zZSx;y9Ztp@n>Bg07#XKdj^w+{d__ccvI)O02ZN~E2Wcx$t-WI7{b<>akW#v)fa9S(SW zWMhz&`^?HL#r04^b^^;JX)*B|60#py892p1FaSlra50E0FR;5NW;EGIx|D-K-1s{$ z1DE6E4bsi*K$`Iar^9Aj89xrjTa$ZK3`GRMep?Z^qIiSzhM*13D=Jn+Prj@&QHT#B b;kY7sgV9Fg6&b6%H&>`S3jww5F+T(V=>uu$ delta 360 zcmcax@UMXTG%qg~0}zCW+HT}_W|lT{wu%WYPAw{qNlZ!1jVaDAElN&}DM~HKFP>b; zY|R)mc`dUX$7KQ4PYlegCX>%G>oEiA&HtHoI2oUB*5TD+WSgk_nL(CyayZ{*j*B8d zbsU_!lXdxhIe_#BZCO^`$xZy89G7{)CYVh=$FC1G!EBSjQWi$J$;(6*b6i$-0BT}Y znd~E)$_%78ZxwB4X8g6;Rx+KDv2F5FDPylIoDLrtgjhWo4|sfFV~~{l#>yfY99JZ7FxqImB4ed@1HIQ=XWYo)zPgpIn-onpaXB6P{m` tU0jfuoVvM%saJ%NZ?cfMGSd{@$u{C`%nZT`n~#b|F)`lStRS_I5dbq~9RmOW delta 87 zcmaE8e$SlyG%qg~0}wcWvEIlX$0TjxY!wq)oLW>IlbDj28&jNLT9lj`Q Date: Wed, 27 Nov 2024 14:40:38 -0500 Subject: [PATCH 4/4] game works --- gui.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gui.py b/gui.py index dabc6e3..5423bcc 100644 --- a/gui.py +++ b/gui.py @@ -238,7 +238,7 @@ def main(BOARD): if BOARD.turn == ai_player: # AI's turn - ai_move = engine.predict_best_move(BOARD) + ai_move = engine.predict_best_move(BOARD, True) BOARD.push(ai_move) continue