From e1778525b8e1732032ddcd354a656a6e6f557c77 Mon Sep 17 00:00:00 2001 From: Brad Wyatt Date: Sat, 8 May 2021 22:54:41 -0700 Subject: [PATCH 01/16] Pause when click on move in pane --- Chess.py | 18 +++- pause_objects.py | 208 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 224 insertions(+), 2 deletions(-) create mode 100644 pause_objects.py diff --git a/Chess.py b/Chess.py index 1340849..db42e07 100644 --- a/Chess.py +++ b/Chess.py @@ -590,6 +590,7 @@ def update_prior_move_color(whoseturn): class Switch_Modes_Controller(): EDIT_MODE, PLAY_MODE = 0, 1 GAME_MODE = EDIT_MODE + PAUSED = False def switch_mode(game_mode, PLAY_EDIT_SWITCH_BUTTON): if game_mode == Switch_Modes_Controller.EDIT_MODE: log.info("\nEditing Mode Activated\n") @@ -617,6 +618,9 @@ def placed_to_play(placed_list, class_obj, color): # Play pieces spawn where their placed piece correspondents are located for placed_obj in placed_list: class_obj(placed_obj.coordinate, color) + def pause_game(paused): + Switch_Modes_Controller.PAUSED = paused + print("Pause status: " + str(Switch_Modes_Controller.PAUSED)) class Move_Tracker(): df_moves = pd.DataFrame(columns=["white_move", "black_move"]) @@ -1284,6 +1288,7 @@ def record_move(game_controller, grid, piece, prior_moves_dict, captured_abb, sp prior_moves_dict['move_notation'] = Move_Controller.move_translator(grid.occupied_piece, piece_in_funcs, captured_abb, special_abb, check_abb) Move_Tracker.selected_move = (Move_Tracker.move_counter(), "white_move") Move_Tracker.df_prior_moves.loc[Move_Tracker.move_counter(), "white_move"] = str(prior_moves_dict) + Switch_Modes_Controller.pause_game(False) log.info(move_text) if game_controller.result_abb != "*": log.info(game_controller.result_abb) @@ -1444,6 +1449,14 @@ def mouse_coordinate(mousepos): for piece_move_rect in PieceMoveRectangle.rectangle_list: if piece_move_rect.rect.collidepoint(MOUSEPOS) and piece_move_rect.text_is_visible: Move_Tracker.selected_move = (piece_move_rect.move_number, piece_move_rect.move_color) + if Move_Tracker.selected_move[0] == len(Move_Tracker.df_moves): + if Move_Tracker.df_moves.loc[len(Move_Tracker.df_moves), "black_move"] != "" \ + and piece_move_rect.move_color == "white_move": + Switch_Modes_Controller.pause_game(True) + else: + Switch_Modes_Controller.pause_game(False) + else: + Switch_Modes_Controller.pause_game(True) # Editing mode only if Switch_Modes_Controller.GAME_MODE == Switch_Modes_Controller.EDIT_MODE: #BUTTONS @@ -1582,8 +1595,9 @@ def test_grid_str(): placed_objects.PLACED_SPRITES.draw(SCREEN) elif(Switch_Modes_Controller.GAME_MODE == Switch_Modes_Controller.PLAY_MODE): #Only draw play sprites in play mode FLIP_BOARD_BUTTON.draw(SCREEN) - play_objects.PLAY_SPRITES.update() - play_objects.PLAY_SPRITES.draw(SCREEN) + if Switch_Modes_Controller.PAUSED == False: + play_objects.PLAY_SPRITES.update() + play_objects.PLAY_SPRITES.draw(SCREEN) PGN_SAVE_FILE_BUTTON.draw(SCREEN) PLAY_PANEL_SPRITES.draw(SCREEN) diff --git a/pause_objects.py b/pause_objects.py new file mode 100644 index 0000000..2d4ff7e --- /dev/null +++ b/pause_objects.py @@ -0,0 +1,208 @@ +import pygame +from load_images_sounds import * +import board + +PLACED_SPRITES = pygame.sprite.Group() + +def remove_placed_object(mousepos): + for placed_item_list in (PlacedPawn.white_pawn_list, PlacedBishop.white_bishop_list, + PlacedKnight.white_knight_list, PlacedRook.white_rook_list, + PlacedQueen.white_queen_list, PlacedKing.white_king_list, + PlacedPawn.black_pawn_list, PlacedBishop.black_bishop_list, + PlacedKnight.black_knight_list, PlacedRook.black_rook_list, + PlacedQueen.black_queen_list, PlacedKing.black_king_list): + for placed_item in placed_item_list: + if placed_item.rect.collidepoint(mousepos): + PLACED_SPRITES.remove(placed_item) + placed_item_list.remove(placed_item) + return + +def remove_all_placed(): + for spr_list in [PlacedPawn.white_pawn_list, PlacedBishop.white_bishop_list, + PlacedKnight.white_knight_list, PlacedRook.white_rook_list, + PlacedQueen.white_queen_list, PlacedKing.white_king_list, + PlacedPawn.black_pawn_list, PlacedBishop.black_bishop_list, + PlacedKnight.black_knight_list, PlacedRook.black_rook_list, + PlacedQueen.black_queen_list, PlacedKing.black_king_list]: + for obj in spr_list: + obj.kill() + PlacedPawn.white_pawn_list = [] + PlacedBishop.white_bishop_list = [] + PlacedKnight.white_knight_list = [] + PlacedRook.white_rook_list = [] + PlacedQueen.white_queen_list = [] + PlacedKing.white_king_list = [] + PlacedPawn.black_pawn_list = [] + PlacedBishop.black_bishop_list = [] + PlacedKnight.black_knight_list = [] + PlacedRook.black_rook_list = [] + PlacedQueen.black_queen_list = [] + PlacedKing.black_king_list = [] + +class PausedPawn(pygame.sprite.Sprite): + white_pawn_list = [] + black_pawn_list = [] + def __init__(self, coord, col): + pygame.sprite.Sprite.__init__(self) + self.col = col + if self.col == "white": + self.image = IMAGES["SPR_WHITE_PAWN"] + PLACED_SPRITES.add(self) + PlacedPawn.white_pawn_list.append(self) + elif self.col == "black": + self.image = IMAGES["SPR_BLACK_PAWN"] + PLACED_SPRITES.add(self) + PlacedPawn.black_pawn_list.append(self) + self.coordinate = coord + self.rect = self.image.get_rect() + for grid in board.Grid.grid_list: + if grid.coordinate == self.coordinate: + self.rect.topleft = grid.rect.topleft + def update(self): + self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft + def destroy(self): + if self.col == "white": + PlacedPawn.white_pawn_list.remove(self) + elif self.col == "black": + PlacedPawn.black_pawn_list.remove(self) + self.kill() + +class PausedBishop(pygame.sprite.Sprite): + white_bishop_list = [] + black_bishop_list = [] + def __init__(self, coord, col): + pygame.sprite.Sprite.__init__(self) + self.col = col + if self.col == "white": + self.image = IMAGES["SPR_WHITE_BISHOP"] + PLACED_SPRITES.add(self) + PlacedBishop.white_bishop_list.append(self) + elif self.col == "black": + self.image = IMAGES["SPR_BLACK_BISHOP"] + PLACED_SPRITES.add(self) + PlacedBishop.black_bishop_list.append(self) + self.coordinate = coord + self.rect = self.image.get_rect() + for grid in board.Grid.grid_list: + if grid.coordinate == self.coordinate: + self.rect.topleft = grid.rect.topleft + def update(self): + self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft + def destroy(self): + if self.col == "white": + PlacedBishop.white_bishop_list.remove(self) + elif self.col == "black": + PlacedBishop.black_bishop_list.remove(self) + self.kill() + +class PausedKnight(pygame.sprite.Sprite): + white_knight_list = [] + black_knight_list = [] + def __init__(self, coord, col): + pygame.sprite.Sprite.__init__(self) + self.col = col + if self.col == "white": + self.image = IMAGES["SPR_WHITE_KNIGHT"] + PLACED_SPRITES.add(self) + PlacedKnight.white_knight_list.append(self) + elif self.col == "black": + self.image = IMAGES["SPR_BLACK_KNIGHT"] + PLACED_SPRITES.add(self) + PlacedKnight.black_knight_list.append(self) + self.coordinate = coord + self.rect = self.image.get_rect() + for grid in board.Grid.grid_list: + if grid.coordinate == self.coordinate: + self.rect.topleft = grid.rect.topleft + def update(self): + self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft + def destroy(self): + if self.col == "white": + PlacedKnight.white_knight_list.remove(self) + elif self.col == "black": + PlacedKnight.black_knight_list.remove(self) + self.kill() + +class PausedRook(pygame.sprite.Sprite): + white_rook_list = [] + black_rook_list = [] + def __init__(self, coord, col): + pygame.sprite.Sprite.__init__(self) + self.col = col + if self.col == "white": + self.image = IMAGES["SPR_WHITE_ROOK"] + PLACED_SPRITES.add(self) + PlacedRook.white_rook_list.append(self) + elif self.col == "black": + self.image = IMAGES["SPR_BLACK_ROOK"] + PLACED_SPRITES.add(self) + PlacedRook.black_rook_list.append(self) + self.coordinate = coord + self.rect = self.image.get_rect() + for grid in board.Grid.grid_list: + if grid.coordinate == self.coordinate: + self.rect.topleft = grid.rect.topleft + def update(self): + self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft + def destroy(self): + if self.col == "white": + PlacedRook.white_rook_list.remove(self) + elif self.col == "black": + PlacedRook.black_rook_list.remove(self) + self.kill() + +class PausedQueen(pygame.sprite.Sprite): + white_queen_list = [] + black_queen_list = [] + def __init__(self, coord, col): + pygame.sprite.Sprite.__init__(self) + self.col = col + if self.col == "white": + self.image = IMAGES["SPR_WHITE_QUEEN"] + PLACED_SPRITES.add(self) + PlacedQueen.white_queen_list.append(self) + elif self.col == "black": + self.image = IMAGES["SPR_BLACK_QUEEN"] + PLACED_SPRITES.add(self) + PlacedQueen.black_queen_list.append(self) + self.coordinate = coord + self.rect = self.image.get_rect() + for grid in board.Grid.grid_list: + if grid.coordinate == self.coordinate: + self.rect.topleft = grid.rect.topleft + def update(self): + self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft + def destroy(self): + if self.col == "white": + PlacedQueen.white_queen_list.remove(self) + elif self.col == "black": + PlacedQueen.black_queen_list.remove(self) + self.kill() + +class PausedKing(pygame.sprite.Sprite): + white_king_list = [] + black_king_list = [] + def __init__(self, coord, col): + pygame.sprite.Sprite.__init__(self) + self.col = col + if self.col == "white": + self.image = IMAGES["SPR_WHITE_KING"] + PLACED_SPRITES.add(self) + PlacedKing.white_king_list.append(self) + elif self.col == "black": + self.image = IMAGES["SPR_BLACK_KING"] + PLACED_SPRITES.add(self) + PlacedKing.black_king_list.append(self) + self.coordinate = coord + self.rect = self.image.get_rect() + for grid in board.Grid.grid_list: + if grid.coordinate == self.coordinate: + self.rect.topleft = grid.rect.topleft + def update(self): + self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft + def destroy(self): + if self.col == "white": + PlacedKing.white_king_list.remove(self) + elif self.col == "black": + PlacedKing.black_king_list.remove(self) + self.kill() \ No newline at end of file From 20604994887a3d612c6889bf65d5c84c76f2bb9a Mon Sep 17 00:00:00 2001 From: Brad Wyatt Date: Sat, 8 May 2021 23:00:43 -0700 Subject: [PATCH 02/16] set it so that it is not possible to click piece when paused --- Chess.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Chess.py b/Chess.py index db42e07..47b95c6 100644 --- a/Chess.py +++ b/Chess.py @@ -893,7 +893,7 @@ def select_piece_unselect_all_others(mousepos, game_controller): for piece_list in play_objects.Piece_Lists_Shortcut.white_pieces(): for piece in piece_list: # Selects piece - if (piece.rect.collidepoint(mousepos) and piece.select == False): + if (piece.rect.collidepoint(mousepos) and piece.select == False and Switch_Modes_Controller.PAUSED == False): clicked_piece = piece else: # Unselects piece @@ -904,7 +904,7 @@ def select_piece_unselect_all_others(mousepos, game_controller): elif game_controller.WHOSETURN == "black": for piece_list in play_objects.Piece_Lists_Shortcut.black_pieces(): for piece in piece_list: - if (piece.rect.collidepoint(mousepos) and piece.select == False): + if (piece.rect.collidepoint(mousepos) and piece.select == False and Switch_Modes_Controller.PAUSED == False): clicked_piece = piece else: piece.no_highlight() From 1df14bbc6f0c25ed605e2f3a022bbb54b53a606c Mon Sep 17 00:00:00 2001 From: Brad Wyatt Date: Sun, 9 May 2021 09:55:14 -0700 Subject: [PATCH 03/16] Renaming all references of "placed" to "paused" for all pause-related components --- Chess.py | 23 +++++++++++- pause_objects.py | 96 ++++++++++++++++++++++++------------------------ 2 files changed, 69 insertions(+), 50 deletions(-) diff --git a/Chess.py b/Chess.py index 47b95c6..7791636 100644 --- a/Chess.py +++ b/Chess.py @@ -618,9 +618,28 @@ def placed_to_play(placed_list, class_obj, color): # Play pieces spawn where their placed piece correspondents are located for placed_obj in placed_list: class_obj(placed_obj.coordinate, color) + def play_to_paused(play_list, class_obj, color): + # Play pieces spawn where their placed piece correspondents are located + for play_obj in play_list: + class_obj(color, play_obj.rect.topleft) def pause_game(paused): Switch_Modes_Controller.PAUSED = paused - print("Pause status: " + str(Switch_Modes_Controller.PAUSED)) + if Switch_Modes_Controller.PAUSED == True: + log.info("Paused") + Switch_Modes_Controller.play_to_paused(play_objects.PlayPawn.white_pawn_list, play_objects.PlayPawn, "white") + Switch_Modes_Controller.play_to_paused(play_objects.PlayBishop.white_bishop_list, play_objects.PlayBishop, "white") + Switch_Modes_Controller.play_to_paused(play_objects.PlayKnight.white_knight_list, play_objects.PlayKnight, "white") + Switch_Modes_Controller.play_to_paused(play_objects.PlayRook.white_rook_list, play_objects.PlayRook, "white") + Switch_Modes_Controller.play_to_paused(play_objects.PlayQueen.white_queen_list, play_objects.PlayQueen, "white") + Switch_Modes_Controller.play_to_paused(play_objects.PlayKing.white_king_list, play_objects.PlayKing, "white") + Switch_Modes_Controller.play_to_paused(play_objects.PlayPawn.black_pawn_list, play_objects.PlayPawn, "black") + Switch_Modes_Controller.play_to_paused(play_objects.PlayBishop.black_bishop_list, play_objects.PlayBishop, "black") + Switch_Modes_Controller.play_to_paused(play_objects.PlayKnight.black_knight_list, play_objects.PlayKnight, "black") + Switch_Modes_Controller.play_to_paused(play_objects.PlayRook.black_rook_list, play_objects.PlayRook, "black") + Switch_Modes_Controller.play_to_paused(play_objects.PlayQueen.black_queen_list, play_objects.PlayQueen, "black") + Switch_Modes_Controller.play_to_paused(play_objects.PlayKing.black_king_list, play_objects.PlayKing, "black") + else: + log.info("Resume") class Move_Tracker(): df_moves = pd.DataFrame(columns=["white_move", "black_move"]) @@ -1658,7 +1677,7 @@ def test_grid_str(): if debug_message == 1: log.info("Entering debug mode") debug_message = 0 - # USE BREAKPOINT HERE + #%% Testing (space) print(str(Move_Tracker.selected_move)) #print(str(Move_Tracker.df_moves)) log.info("Use breakpoint here") diff --git a/pause_objects.py b/pause_objects.py index 2d4ff7e..11d7b79 100644 --- a/pause_objects.py +++ b/pause_objects.py @@ -2,56 +2,56 @@ from load_images_sounds import * import board -PLACED_SPRITES = pygame.sprite.Group() +PAUSED_SPRITES = pygame.sprite.Group() def remove_placed_object(mousepos): - for placed_item_list in (PlacedPawn.white_pawn_list, PlacedBishop.white_bishop_list, - PlacedKnight.white_knight_list, PlacedRook.white_rook_list, - PlacedQueen.white_queen_list, PlacedKing.white_king_list, - PlacedPawn.black_pawn_list, PlacedBishop.black_bishop_list, - PlacedKnight.black_knight_list, PlacedRook.black_rook_list, - PlacedQueen.black_queen_list, PlacedKing.black_king_list): - for placed_item in placed_item_list: - if placed_item.rect.collidepoint(mousepos): - PLACED_SPRITES.remove(placed_item) - placed_item_list.remove(placed_item) + for paused_item_list in (PausedPawn.white_pawn_list, PausedBishop.white_bishop_list, + PausedKnight.white_knight_list, PausedRook.white_rook_list, + PausedQueen.white_queen_list, PausedKing.white_king_list, + PausedPawn.black_pawn_list, PausedBishop.black_bishop_list, + PausedKnight.black_knight_list, PausedRook.black_rook_list, + PausedQueen.black_queen_list, PausedKing.black_king_list): + for paused_item in paused_item_list: + if paused_item.rect.collidepoint(mousepos): + PAUSED_SPRITES.remove(paused_item) + paused_item_list.remove(paused_item) return -def remove_all_placed(): - for spr_list in [PlacedPawn.white_pawn_list, PlacedBishop.white_bishop_list, - PlacedKnight.white_knight_list, PlacedRook.white_rook_list, - PlacedQueen.white_queen_list, PlacedKing.white_king_list, - PlacedPawn.black_pawn_list, PlacedBishop.black_bishop_list, - PlacedKnight.black_knight_list, PlacedRook.black_rook_list, - PlacedQueen.black_queen_list, PlacedKing.black_king_list]: +def remove_all_paused(): + for spr_list in [PausedPawn.white_pawn_list, PausedBishop.white_bishop_list, + PausedKnight.white_knight_list, PausedRook.white_rook_list, + PausedQueen.white_queen_list, PausedKing.white_king_list, + PausedPawn.black_pawn_list, PausedBishop.black_bishop_list, + PausedKnight.black_knight_list, PausedRook.black_rook_list, + PausedQueen.black_queen_list, PausedKing.black_king_list]: for obj in spr_list: obj.kill() - PlacedPawn.white_pawn_list = [] - PlacedBishop.white_bishop_list = [] - PlacedKnight.white_knight_list = [] - PlacedRook.white_rook_list = [] - PlacedQueen.white_queen_list = [] - PlacedKing.white_king_list = [] - PlacedPawn.black_pawn_list = [] - PlacedBishop.black_bishop_list = [] - PlacedKnight.black_knight_list = [] - PlacedRook.black_rook_list = [] - PlacedQueen.black_queen_list = [] - PlacedKing.black_king_list = [] + PausedPawn.white_pawn_list = [] + PausedBishop.white_bishop_list = [] + PausedKnight.white_knight_list = [] + PausedRook.white_rook_list = [] + PausedQueen.white_queen_list = [] + PausedKing.white_king_list = [] + PausedPawn.black_pawn_list = [] + PausedBishop.black_bishop_list = [] + PausedKnight.black_knight_list = [] + PausedRook.black_rook_list = [] + PausedQueen.black_queen_list = [] + PausedKing.black_king_list = [] class PausedPawn(pygame.sprite.Sprite): white_pawn_list = [] black_pawn_list = [] - def __init__(self, coord, col): + def __init__(self, col, coord=None): pygame.sprite.Sprite.__init__(self) self.col = col if self.col == "white": self.image = IMAGES["SPR_WHITE_PAWN"] - PLACED_SPRITES.add(self) + PAUSED_SPRITES.add(self) PlacedPawn.white_pawn_list.append(self) elif self.col == "black": self.image = IMAGES["SPR_BLACK_PAWN"] - PLACED_SPRITES.add(self) + PAUSED_SPRITES.add(self) PlacedPawn.black_pawn_list.append(self) self.coordinate = coord self.rect = self.image.get_rect() @@ -70,16 +70,16 @@ def destroy(self): class PausedBishop(pygame.sprite.Sprite): white_bishop_list = [] black_bishop_list = [] - def __init__(self, coord, col): + def __init__(self, col, coord=None): pygame.sprite.Sprite.__init__(self) self.col = col if self.col == "white": self.image = IMAGES["SPR_WHITE_BISHOP"] - PLACED_SPRITES.add(self) + PAUSED_SPRITES.add(self) PlacedBishop.white_bishop_list.append(self) elif self.col == "black": self.image = IMAGES["SPR_BLACK_BISHOP"] - PLACED_SPRITES.add(self) + PAUSED_SPRITES.add(self) PlacedBishop.black_bishop_list.append(self) self.coordinate = coord self.rect = self.image.get_rect() @@ -98,16 +98,16 @@ def destroy(self): class PausedKnight(pygame.sprite.Sprite): white_knight_list = [] black_knight_list = [] - def __init__(self, coord, col): + def __init__(self, col, coord=None): pygame.sprite.Sprite.__init__(self) self.col = col if self.col == "white": self.image = IMAGES["SPR_WHITE_KNIGHT"] - PLACED_SPRITES.add(self) + PAUSED_SPRITES.add(self) PlacedKnight.white_knight_list.append(self) elif self.col == "black": self.image = IMAGES["SPR_BLACK_KNIGHT"] - PLACED_SPRITES.add(self) + PAUSED_SPRITES.add(self) PlacedKnight.black_knight_list.append(self) self.coordinate = coord self.rect = self.image.get_rect() @@ -126,16 +126,16 @@ def destroy(self): class PausedRook(pygame.sprite.Sprite): white_rook_list = [] black_rook_list = [] - def __init__(self, coord, col): + def __init__(self, col, coord=None): pygame.sprite.Sprite.__init__(self) self.col = col if self.col == "white": self.image = IMAGES["SPR_WHITE_ROOK"] - PLACED_SPRITES.add(self) + PAUSED_SPRITES.add(self) PlacedRook.white_rook_list.append(self) elif self.col == "black": self.image = IMAGES["SPR_BLACK_ROOK"] - PLACED_SPRITES.add(self) + PAUSED_SPRITES.add(self) PlacedRook.black_rook_list.append(self) self.coordinate = coord self.rect = self.image.get_rect() @@ -154,16 +154,16 @@ def destroy(self): class PausedQueen(pygame.sprite.Sprite): white_queen_list = [] black_queen_list = [] - def __init__(self, coord, col): + def __init__(self, col, coord=None): pygame.sprite.Sprite.__init__(self) self.col = col if self.col == "white": self.image = IMAGES["SPR_WHITE_QUEEN"] - PLACED_SPRITES.add(self) + PAUSED_SPRITES.add(self) PlacedQueen.white_queen_list.append(self) elif self.col == "black": self.image = IMAGES["SPR_BLACK_QUEEN"] - PLACED_SPRITES.add(self) + PAUSED_SPRITES.add(self) PlacedQueen.black_queen_list.append(self) self.coordinate = coord self.rect = self.image.get_rect() @@ -182,16 +182,16 @@ def destroy(self): class PausedKing(pygame.sprite.Sprite): white_king_list = [] black_king_list = [] - def __init__(self, coord, col): + def __init__(self, col, coord=None): pygame.sprite.Sprite.__init__(self) self.col = col if self.col == "white": self.image = IMAGES["SPR_WHITE_KING"] - PLACED_SPRITES.add(self) + PAUSED_SPRITES.add(self) PlacedKing.white_king_list.append(self) elif self.col == "black": self.image = IMAGES["SPR_BLACK_KING"] - PLACED_SPRITES.add(self) + PAUSED_SPRITES.add(self) PlacedKing.black_king_list.append(self) self.coordinate = coord self.rect = self.image.get_rect() From a134241a7d9f6bb47bea4d06b66c92e8d9dd8595 Mon Sep 17 00:00:00 2001 From: Brad Wyatt Date: Sun, 9 May 2021 10:44:25 -0700 Subject: [PATCH 04/16] Function to create a list of elements that were in dataframe until selected move --- Chess.py | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/Chess.py b/Chess.py index 7791636..20a537a 100644 --- a/Chess.py +++ b/Chess.py @@ -623,9 +623,12 @@ def play_to_paused(play_list, class_obj, color): for play_obj in play_list: class_obj(color, play_obj.rect.topleft) def pause_game(paused): + #%% Working on currently Switch_Modes_Controller.PAUSED = paused if Switch_Modes_Controller.PAUSED == True: log.info("Paused") + print(str(Switch_Modes_Controller.list_of_moves_backwards(Move_Tracker.df_prior_moves))) + """ Switch_Modes_Controller.play_to_paused(play_objects.PlayPawn.white_pawn_list, play_objects.PlayPawn, "white") Switch_Modes_Controller.play_to_paused(play_objects.PlayBishop.white_bishop_list, play_objects.PlayBishop, "white") Switch_Modes_Controller.play_to_paused(play_objects.PlayKnight.white_knight_list, play_objects.PlayKnight, "white") @@ -638,8 +641,27 @@ def pause_game(paused): Switch_Modes_Controller.play_to_paused(play_objects.PlayRook.black_rook_list, play_objects.PlayRook, "black") Switch_Modes_Controller.play_to_paused(play_objects.PlayQueen.black_queen_list, play_objects.PlayQueen, "black") Switch_Modes_Controller.play_to_paused(play_objects.PlayKing.black_king_list, play_objects.PlayKing, "black") + """ else: log.info("Resume") + def list_of_moves_backwards(df_prior_moves): + moves_backwards_list = [] + limit_moves = Move_Tracker.selected_move[0] + limit_color = Move_Tracker.selected_move[1] + #Move_Tracker.selected_move = (0, "") + for move_num in range(Move_Tracker.move_counter(), limit_moves-1, -1): + if limit_color == 'black_move' and move_num == limit_moves: + # Selected move is black, so ignore the white move on that same move number and break + moves_backwards_list.append(df_prior_moves.loc[move_num, 'black_move']) + break + elif df_prior_moves.loc[move_num, 'black_move'] == '': + # Current move has no black move yet, so ignore adding that to list + pass + else: + moves_backwards_list.append(df_prior_moves.loc[move_num, 'black_move']) + moves_backwards_list.append(df_prior_moves.loc[move_num, 'white_move']) + return moves_backwards_list + class Move_Tracker(): df_moves = pd.DataFrame(columns=["white_move", "black_move"]) @@ -653,7 +675,8 @@ def restart(): Move_Tracker.df_moves.index = np.arange(1, len(Move_Tracker.df_moves)+1) Move_Tracker.df_prior_moves = pd.DataFrame(columns=["white_move", "black_move"]) Move_Tracker.df_prior_moves.index = np.arange(1, len(Move_Tracker.df_prior_moves)+1) - Move_Tracker.move_counter = lambda : len(Move_Tracker.df_moves) + #%% The below 1 line may need to be deleted + Move_Tracker.move_counter = lambda : len(Move_Tracker.df_moves) Move_Tracker.selected_move = (0, "") def undo_move_in_dfs(undo_color): if undo_color == "black": @@ -1052,7 +1075,9 @@ def make_move(grid, piece, game_controller): prior_moves_dict = {} # Update df_moves dictionary with a new record for the new move (when white's turn) if piece.color == "white": - Move_Tracker.df_moves.loc[Move_Tracker.move_counter()+1] = ["", ""] + next_move = Move_Tracker.move_counter()+1 + Move_Tracker.df_moves.loc[next_move] = ["", ""] + Move_Tracker.df_prior_moves.loc[next_move] = ["", ""] # Taking a piece by checking if available grid is opposite color of piece # And iterating through all pieces to check if coordinates of that grid # are the same as any of the pieces From c0269cdc76a49a89a30b5f3431c8b19a057f0d07 Mon Sep 17 00:00:00 2001 From: Brad Wyatt Date: Sun, 9 May 2021 11:22:53 -0700 Subject: [PATCH 05/16] Paused objects sprites now show up --- Chess.py | 33 +++++++++--------- pause_objects.py => paused_objects.py | 48 +++++++++++++-------------- 2 files changed, 42 insertions(+), 39 deletions(-) rename pause_objects.py => paused_objects.py (84%) diff --git a/Chess.py b/Chess.py index 20a537a..547ce5a 100644 --- a/Chess.py +++ b/Chess.py @@ -37,6 +37,7 @@ import start_objects import placed_objects import play_objects +import paused_objects from load_images_sounds import * from menu_buttons import * import random @@ -621,27 +622,26 @@ def placed_to_play(placed_list, class_obj, color): def play_to_paused(play_list, class_obj, color): # Play pieces spawn where their placed piece correspondents are located for play_obj in play_list: - class_obj(color, play_obj.rect.topleft) + if play_obj.coordinate is not None: + class_obj(color, play_obj.coordinate) def pause_game(paused): #%% Working on currently Switch_Modes_Controller.PAUSED = paused if Switch_Modes_Controller.PAUSED == True: log.info("Paused") print(str(Switch_Modes_Controller.list_of_moves_backwards(Move_Tracker.df_prior_moves))) - """ - Switch_Modes_Controller.play_to_paused(play_objects.PlayPawn.white_pawn_list, play_objects.PlayPawn, "white") - Switch_Modes_Controller.play_to_paused(play_objects.PlayBishop.white_bishop_list, play_objects.PlayBishop, "white") - Switch_Modes_Controller.play_to_paused(play_objects.PlayKnight.white_knight_list, play_objects.PlayKnight, "white") - Switch_Modes_Controller.play_to_paused(play_objects.PlayRook.white_rook_list, play_objects.PlayRook, "white") - Switch_Modes_Controller.play_to_paused(play_objects.PlayQueen.white_queen_list, play_objects.PlayQueen, "white") - Switch_Modes_Controller.play_to_paused(play_objects.PlayKing.white_king_list, play_objects.PlayKing, "white") - Switch_Modes_Controller.play_to_paused(play_objects.PlayPawn.black_pawn_list, play_objects.PlayPawn, "black") - Switch_Modes_Controller.play_to_paused(play_objects.PlayBishop.black_bishop_list, play_objects.PlayBishop, "black") - Switch_Modes_Controller.play_to_paused(play_objects.PlayKnight.black_knight_list, play_objects.PlayKnight, "black") - Switch_Modes_Controller.play_to_paused(play_objects.PlayRook.black_rook_list, play_objects.PlayRook, "black") - Switch_Modes_Controller.play_to_paused(play_objects.PlayQueen.black_queen_list, play_objects.PlayQueen, "black") - Switch_Modes_Controller.play_to_paused(play_objects.PlayKing.black_king_list, play_objects.PlayKing, "black") - """ + Switch_Modes_Controller.play_to_paused(play_objects.PlayPawn.white_pawn_list, paused_objects.PausedPawn, "white") + Switch_Modes_Controller.play_to_paused(play_objects.PlayBishop.white_bishop_list, paused_objects.PausedBishop, "white") + Switch_Modes_Controller.play_to_paused(play_objects.PlayKnight.white_knight_list, paused_objects.PausedKnight, "white") + Switch_Modes_Controller.play_to_paused(play_objects.PlayRook.white_rook_list, paused_objects.PausedRook, "white") + Switch_Modes_Controller.play_to_paused(play_objects.PlayQueen.white_queen_list, paused_objects.PausedQueen, "white") + Switch_Modes_Controller.play_to_paused(play_objects.PlayKing.white_king_list, paused_objects.PausedKing, "white") + Switch_Modes_Controller.play_to_paused(play_objects.PlayPawn.black_pawn_list, paused_objects.PausedPawn, "black") + Switch_Modes_Controller.play_to_paused(play_objects.PlayBishop.black_bishop_list, paused_objects.PausedBishop, "black") + Switch_Modes_Controller.play_to_paused(play_objects.PlayKnight.black_knight_list, paused_objects.PausedKnight, "black") + Switch_Modes_Controller.play_to_paused(play_objects.PlayRook.black_rook_list, paused_objects.PausedRook, "black") + Switch_Modes_Controller.play_to_paused(play_objects.PlayQueen.black_queen_list, paused_objects.PausedQueen, "black") + Switch_Modes_Controller.play_to_paused(play_objects.PlayKing.black_king_list, paused_objects.PausedKing, "black") else: log.info("Resume") def list_of_moves_backwards(df_prior_moves): @@ -1642,6 +1642,9 @@ def test_grid_str(): if Switch_Modes_Controller.PAUSED == False: play_objects.PLAY_SPRITES.update() play_objects.PLAY_SPRITES.draw(SCREEN) + else: + paused_objects.PAUSED_SPRITES.update() + paused_objects.PAUSED_SPRITES.draw(SCREEN) PGN_SAVE_FILE_BUTTON.draw(SCREEN) PLAY_PANEL_SPRITES.draw(SCREEN) diff --git a/pause_objects.py b/paused_objects.py similarity index 84% rename from pause_objects.py rename to paused_objects.py index 11d7b79..1ad4ce6 100644 --- a/pause_objects.py +++ b/paused_objects.py @@ -48,11 +48,11 @@ def __init__(self, col, coord=None): if self.col == "white": self.image = IMAGES["SPR_WHITE_PAWN"] PAUSED_SPRITES.add(self) - PlacedPawn.white_pawn_list.append(self) + PausedPawn.white_pawn_list.append(self) elif self.col == "black": self.image = IMAGES["SPR_BLACK_PAWN"] PAUSED_SPRITES.add(self) - PlacedPawn.black_pawn_list.append(self) + PausedPawn.black_pawn_list.append(self) self.coordinate = coord self.rect = self.image.get_rect() for grid in board.Grid.grid_list: @@ -62,9 +62,9 @@ def update(self): self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft def destroy(self): if self.col == "white": - PlacedPawn.white_pawn_list.remove(self) + PausedPawn.white_pawn_list.remove(self) elif self.col == "black": - PlacedPawn.black_pawn_list.remove(self) + PausedPawn.black_pawn_list.remove(self) self.kill() class PausedBishop(pygame.sprite.Sprite): @@ -76,11 +76,11 @@ def __init__(self, col, coord=None): if self.col == "white": self.image = IMAGES["SPR_WHITE_BISHOP"] PAUSED_SPRITES.add(self) - PlacedBishop.white_bishop_list.append(self) + PausedBishop.white_bishop_list.append(self) elif self.col == "black": self.image = IMAGES["SPR_BLACK_BISHOP"] PAUSED_SPRITES.add(self) - PlacedBishop.black_bishop_list.append(self) + PausedBishop.black_bishop_list.append(self) self.coordinate = coord self.rect = self.image.get_rect() for grid in board.Grid.grid_list: @@ -90,9 +90,9 @@ def update(self): self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft def destroy(self): if self.col == "white": - PlacedBishop.white_bishop_list.remove(self) + PausedBishop.white_bishop_list.remove(self) elif self.col == "black": - PlacedBishop.black_bishop_list.remove(self) + PausedBishop.black_bishop_list.remove(self) self.kill() class PausedKnight(pygame.sprite.Sprite): @@ -104,11 +104,11 @@ def __init__(self, col, coord=None): if self.col == "white": self.image = IMAGES["SPR_WHITE_KNIGHT"] PAUSED_SPRITES.add(self) - PlacedKnight.white_knight_list.append(self) + PausedKnight.white_knight_list.append(self) elif self.col == "black": self.image = IMAGES["SPR_BLACK_KNIGHT"] PAUSED_SPRITES.add(self) - PlacedKnight.black_knight_list.append(self) + PausedKnight.black_knight_list.append(self) self.coordinate = coord self.rect = self.image.get_rect() for grid in board.Grid.grid_list: @@ -118,9 +118,9 @@ def update(self): self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft def destroy(self): if self.col == "white": - PlacedKnight.white_knight_list.remove(self) + PausedKnight.white_knight_list.remove(self) elif self.col == "black": - PlacedKnight.black_knight_list.remove(self) + PausedKnight.black_knight_list.remove(self) self.kill() class PausedRook(pygame.sprite.Sprite): @@ -132,11 +132,11 @@ def __init__(self, col, coord=None): if self.col == "white": self.image = IMAGES["SPR_WHITE_ROOK"] PAUSED_SPRITES.add(self) - PlacedRook.white_rook_list.append(self) + PausedRook.white_rook_list.append(self) elif self.col == "black": self.image = IMAGES["SPR_BLACK_ROOK"] PAUSED_SPRITES.add(self) - PlacedRook.black_rook_list.append(self) + PausedRook.black_rook_list.append(self) self.coordinate = coord self.rect = self.image.get_rect() for grid in board.Grid.grid_list: @@ -146,9 +146,9 @@ def update(self): self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft def destroy(self): if self.col == "white": - PlacedRook.white_rook_list.remove(self) + PausedRook.white_rook_list.remove(self) elif self.col == "black": - PlacedRook.black_rook_list.remove(self) + PausedRook.black_rook_list.remove(self) self.kill() class PausedQueen(pygame.sprite.Sprite): @@ -160,11 +160,11 @@ def __init__(self, col, coord=None): if self.col == "white": self.image = IMAGES["SPR_WHITE_QUEEN"] PAUSED_SPRITES.add(self) - PlacedQueen.white_queen_list.append(self) + PausedQueen.white_queen_list.append(self) elif self.col == "black": self.image = IMAGES["SPR_BLACK_QUEEN"] PAUSED_SPRITES.add(self) - PlacedQueen.black_queen_list.append(self) + PausedQueen.black_queen_list.append(self) self.coordinate = coord self.rect = self.image.get_rect() for grid in board.Grid.grid_list: @@ -174,9 +174,9 @@ def update(self): self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft def destroy(self): if self.col == "white": - PlacedQueen.white_queen_list.remove(self) + PausedQueen.white_queen_list.remove(self) elif self.col == "black": - PlacedQueen.black_queen_list.remove(self) + PausedQueen.black_queen_list.remove(self) self.kill() class PausedKing(pygame.sprite.Sprite): @@ -188,11 +188,11 @@ def __init__(self, col, coord=None): if self.col == "white": self.image = IMAGES["SPR_WHITE_KING"] PAUSED_SPRITES.add(self) - PlacedKing.white_king_list.append(self) + PausedKing.white_king_list.append(self) elif self.col == "black": self.image = IMAGES["SPR_BLACK_KING"] PAUSED_SPRITES.add(self) - PlacedKing.black_king_list.append(self) + PausedKing.black_king_list.append(self) self.coordinate = coord self.rect = self.image.get_rect() for grid in board.Grid.grid_list: @@ -202,7 +202,7 @@ def update(self): self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft def destroy(self): if self.col == "white": - PlacedKing.white_king_list.remove(self) + PausedKing.white_king_list.remove(self) elif self.col == "black": - PlacedKing.black_king_list.remove(self) + PausedKing.black_king_list.remove(self) self.kill() \ No newline at end of file From 79a4fe0337abf5603ce459e7017f0cfa4672ef6e Mon Sep 17 00:00:00 2001 From: Brad Wyatt Date: Sun, 9 May 2021 13:29:15 -0700 Subject: [PATCH 06/16] Some pieces move back, but it's certainly NOT smooth --- Chess.py | 31 +++++++++++++++++++++++------- paused_objects.py | 48 ++++++++++++++++++++++++++++------------------- 2 files changed, 53 insertions(+), 26 deletions(-) diff --git a/Chess.py b/Chess.py index 547ce5a..04c7f49 100644 --- a/Chess.py +++ b/Chess.py @@ -49,7 +49,7 @@ from tkinter.colorchooser import askcolor from tkinter.filedialog import * from tkinter import ttk -from ast import * +import ast import pygame import datetime import logging @@ -107,7 +107,7 @@ def pos_load_file(reset=False): log.info("File not found") return loaded_file = open_file.read() - loaded_dict = literal_eval(loaded_file) + loaded_dict = ast.literal_eval(loaded_file) for obj_list in play_objects.Piece_Lists_Shortcut.all_pieces(): for obj in obj_list: @@ -623,13 +623,23 @@ def play_to_paused(play_list, class_obj, color): # Play pieces spawn where their placed piece correspondents are located for play_obj in play_list: if play_obj.coordinate is not None: - class_obj(color, play_obj.coordinate) + class_obj(color, play_obj.coordinate_history, play_obj.coordinate) + def rewind_moves(): + list_of_moves_backwards = Switch_Modes_Controller.list_of_moves_backwards(Move_Tracker.df_prior_moves) + print("LIST OF MOVES BACKWARDS " + str(list_of_moves_backwards)) + # list_of_moves_backwards list is ordered in descending order to the selected move + for move_dict in list_of_moves_backwards: + for paused_obj_list in paused_objects.Piece_Lists_Shortcut.all_pieces(): + for paused_obj in paused_obj_list: + for piece_history in paused_obj.coordinate_history: + if piece_history in dict(move_dict): + paused_obj.coordinate = ast.literal_eval(move_dict[piece_history])['before'] def pause_game(paused): #%% Working on currently Switch_Modes_Controller.PAUSED = paused if Switch_Modes_Controller.PAUSED == True: log.info("Paused") - print(str(Switch_Modes_Controller.list_of_moves_backwards(Move_Tracker.df_prior_moves))) + #print(str(Switch_Modes_Controller.list_of_moves_backwards(Move_Tracker.df_prior_moves))) Switch_Modes_Controller.play_to_paused(play_objects.PlayPawn.white_pawn_list, paused_objects.PausedPawn, "white") Switch_Modes_Controller.play_to_paused(play_objects.PlayBishop.white_bishop_list, paused_objects.PausedBishop, "white") Switch_Modes_Controller.play_to_paused(play_objects.PlayKnight.white_knight_list, paused_objects.PausedKnight, "white") @@ -642,24 +652,31 @@ def pause_game(paused): Switch_Modes_Controller.play_to_paused(play_objects.PlayRook.black_rook_list, paused_objects.PausedRook, "black") Switch_Modes_Controller.play_to_paused(play_objects.PlayQueen.black_queen_list, paused_objects.PausedQueen, "black") Switch_Modes_Controller.play_to_paused(play_objects.PlayKing.black_king_list, paused_objects.PausedKing, "black") + Switch_Modes_Controller.rewind_moves() else: log.info("Resume") + paused_objects.remove_all_paused() def list_of_moves_backwards(df_prior_moves): moves_backwards_list = [] limit_moves = Move_Tracker.selected_move[0] limit_color = Move_Tracker.selected_move[1] #Move_Tracker.selected_move = (0, "") for move_num in range(Move_Tracker.move_counter(), limit_moves-1, -1): + moves_backwards_dict = {} if limit_color == 'black_move' and move_num == limit_moves: # Selected move is black, so ignore the white move on that same move number and break - moves_backwards_list.append(df_prior_moves.loc[move_num, 'black_move']) + moves_backwards_dict[move_num] = df_prior_moves.loc[move_num, 'black_move'] + moves_backwards_list.append(moves_backwards_dict) break elif df_prior_moves.loc[move_num, 'black_move'] == '': # Current move has no black move yet, so ignore adding that to list pass else: - moves_backwards_list.append(df_prior_moves.loc[move_num, 'black_move']) - moves_backwards_list.append(df_prior_moves.loc[move_num, 'white_move']) + moves_backwards_dict[move_num] = df_prior_moves.loc[move_num, 'black_move'] + moves_backwards_list.append(moves_backwards_dict) + moves_backwards_dict = {} + moves_backwards_dict[move_num] = df_prior_moves.loc[move_num, 'white_move'] + moves_backwards_list.append(moves_backwards_dict) return moves_backwards_list diff --git a/paused_objects.py b/paused_objects.py index 1ad4ce6..3004035 100644 --- a/paused_objects.py +++ b/paused_objects.py @@ -4,19 +4,6 @@ PAUSED_SPRITES = pygame.sprite.Group() -def remove_placed_object(mousepos): - for paused_item_list in (PausedPawn.white_pawn_list, PausedBishop.white_bishop_list, - PausedKnight.white_knight_list, PausedRook.white_rook_list, - PausedQueen.white_queen_list, PausedKing.white_king_list, - PausedPawn.black_pawn_list, PausedBishop.black_bishop_list, - PausedKnight.black_knight_list, PausedRook.black_rook_list, - PausedQueen.black_queen_list, PausedKing.black_king_list): - for paused_item in paused_item_list: - if paused_item.rect.collidepoint(mousepos): - PAUSED_SPRITES.remove(paused_item) - paused_item_list.remove(paused_item) - return - def remove_all_paused(): for spr_list in [PausedPawn.white_pawn_list, PausedBishop.white_bishop_list, PausedKnight.white_knight_list, PausedRook.white_rook_list, @@ -38,11 +25,28 @@ def remove_all_paused(): PausedRook.black_rook_list = [] PausedQueen.black_queen_list = [] PausedKing.black_king_list = [] + +class Piece_Lists_Shortcut(): + def all_pieces(): + return [PausedPawn.white_pawn_list, PausedBishop.white_bishop_list, + PausedKnight.white_knight_list, PausedRook.white_rook_list, + PausedQueen.white_queen_list, PausedKing.white_king_list, + PausedPawn.black_pawn_list, PausedBishop.black_bishop_list, + PausedKnight.black_knight_list, PausedRook.black_rook_list, + PausedQueen.black_queen_list, PausedKing.black_king_list] + def white_pieces(): + return [PausedPawn.white_pawn_list, PausedBishop.white_bishop_list, + PausedKnight.white_knight_list, PausedRook.white_rook_list, + PausedQueen.white_queen_list, PausedKing.white_king_list] + def black_pieces(): + return [PausedPawn.black_pawn_list, PausedBishop.black_bishop_list, + PausedKnight.black_knight_list, PausedRook.black_rook_list, + PausedQueen.black_queen_list, PausedKing.black_king_list] class PausedPawn(pygame.sprite.Sprite): white_pawn_list = [] black_pawn_list = [] - def __init__(self, col, coord=None): + def __init__(self, col, coordinate_history, coord=None): pygame.sprite.Sprite.__init__(self) self.col = col if self.col == "white": @@ -54,6 +58,7 @@ def __init__(self, col, coord=None): PAUSED_SPRITES.add(self) PausedPawn.black_pawn_list.append(self) self.coordinate = coord + self.coordinate_history = coordinate_history self.rect = self.image.get_rect() for grid in board.Grid.grid_list: if grid.coordinate == self.coordinate: @@ -70,7 +75,7 @@ def destroy(self): class PausedBishop(pygame.sprite.Sprite): white_bishop_list = [] black_bishop_list = [] - def __init__(self, col, coord=None): + def __init__(self, col, coordinate_history, coord=None): pygame.sprite.Sprite.__init__(self) self.col = col if self.col == "white": @@ -82,6 +87,7 @@ def __init__(self, col, coord=None): PAUSED_SPRITES.add(self) PausedBishop.black_bishop_list.append(self) self.coordinate = coord + self.coordinate_history = coordinate_history self.rect = self.image.get_rect() for grid in board.Grid.grid_list: if grid.coordinate == self.coordinate: @@ -98,7 +104,7 @@ def destroy(self): class PausedKnight(pygame.sprite.Sprite): white_knight_list = [] black_knight_list = [] - def __init__(self, col, coord=None): + def __init__(self, col, coordinate_history, coord=None): pygame.sprite.Sprite.__init__(self) self.col = col if self.col == "white": @@ -110,6 +116,7 @@ def __init__(self, col, coord=None): PAUSED_SPRITES.add(self) PausedKnight.black_knight_list.append(self) self.coordinate = coord + self.coordinate_history = coordinate_history self.rect = self.image.get_rect() for grid in board.Grid.grid_list: if grid.coordinate == self.coordinate: @@ -126,7 +133,7 @@ def destroy(self): class PausedRook(pygame.sprite.Sprite): white_rook_list = [] black_rook_list = [] - def __init__(self, col, coord=None): + def __init__(self, col, coordinate_history, coord=None): pygame.sprite.Sprite.__init__(self) self.col = col if self.col == "white": @@ -138,6 +145,7 @@ def __init__(self, col, coord=None): PAUSED_SPRITES.add(self) PausedRook.black_rook_list.append(self) self.coordinate = coord + self.coordinate_history = coordinate_history self.rect = self.image.get_rect() for grid in board.Grid.grid_list: if grid.coordinate == self.coordinate: @@ -154,7 +162,7 @@ def destroy(self): class PausedQueen(pygame.sprite.Sprite): white_queen_list = [] black_queen_list = [] - def __init__(self, col, coord=None): + def __init__(self, col, coordinate_history, coord=None): pygame.sprite.Sprite.__init__(self) self.col = col if self.col == "white": @@ -166,6 +174,7 @@ def __init__(self, col, coord=None): PAUSED_SPRITES.add(self) PausedQueen.black_queen_list.append(self) self.coordinate = coord + self.coordinate_history = coordinate_history self.rect = self.image.get_rect() for grid in board.Grid.grid_list: if grid.coordinate == self.coordinate: @@ -182,7 +191,7 @@ def destroy(self): class PausedKing(pygame.sprite.Sprite): white_king_list = [] black_king_list = [] - def __init__(self, col, coord=None): + def __init__(self, col, coordinate_history, coord=None): pygame.sprite.Sprite.__init__(self) self.col = col if self.col == "white": @@ -194,6 +203,7 @@ def __init__(self, col, coord=None): PAUSED_SPRITES.add(self) PausedKing.black_king_list.append(self) self.coordinate = coord + self.coordinate_history = coordinate_history self.rect = self.image.get_rect() for grid in board.Grid.grid_list: if grid.coordinate == self.coordinate: From ab213626bdd9dd363d139bda5c5478a3fb00cfc8 Mon Sep 17 00:00:00 2001 From: Brad Wyatt Date: Sun, 9 May 2021 13:34:44 -0700 Subject: [PATCH 07/16] record move of 'before' after the yellow box --- Chess.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Chess.py b/Chess.py index 04c7f49..cb75606 100644 --- a/Chess.py +++ b/Chess.py @@ -677,7 +677,8 @@ def list_of_moves_backwards(df_prior_moves): moves_backwards_dict = {} moves_backwards_dict[move_num] = df_prior_moves.loc[move_num, 'white_move'] moves_backwards_list.append(moves_backwards_dict) - return moves_backwards_list + # When select a move on pane, we take back the move right after that + return moves_backwards_list[:-1] class Move_Tracker(): From 98857d8a6ab6aa24de1a145a427d76de48f8189e Mon Sep 17 00:00:00 2001 From: Brad Wyatt Date: Sun, 9 May 2021 13:39:21 -0700 Subject: [PATCH 08/16] This works!!! --- Chess.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Chess.py b/Chess.py index cb75606..072518e 100644 --- a/Chess.py +++ b/Chess.py @@ -633,12 +633,14 @@ def rewind_moves(): for paused_obj in paused_obj_list: for piece_history in paused_obj.coordinate_history: if piece_history in dict(move_dict): - paused_obj.coordinate = ast.literal_eval(move_dict[piece_history])['before'] + if paused_obj.coordinate_history[piece_history] == ast.literal_eval(move_dict[piece_history]): + paused_obj.coordinate = ast.literal_eval(move_dict[piece_history])['before'] def pause_game(paused): #%% Working on currently Switch_Modes_Controller.PAUSED = paused if Switch_Modes_Controller.PAUSED == True: log.info("Paused") + paused_objects.remove_all_paused() #print(str(Switch_Modes_Controller.list_of_moves_backwards(Move_Tracker.df_prior_moves))) Switch_Modes_Controller.play_to_paused(play_objects.PlayPawn.white_pawn_list, paused_objects.PausedPawn, "white") Switch_Modes_Controller.play_to_paused(play_objects.PlayBishop.white_bishop_list, paused_objects.PausedBishop, "white") From 28f41b87f8168c84d950820e211a3200cd677417 Mon Sep 17 00:00:00 2001 From: Brad Wyatt Date: Sun, 9 May 2021 14:10:44 -0700 Subject: [PATCH 09/16] prior move color on grid works, still working on prior move color for piece --- Chess.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Chess.py b/Chess.py index 072518e..3b57998 100644 --- a/Chess.py +++ b/Chess.py @@ -598,6 +598,7 @@ def switch_mode(game_mode, PLAY_EDIT_SWITCH_BUTTON): Switch_Modes_Controller.GAME_MODE = Switch_Modes_Controller.EDIT_MODE PLAY_EDIT_SWITCH_BUTTON.image = PLAY_EDIT_SWITCH_BUTTON.game_mode_button(Switch_Modes_Controller.GAME_MODE) Text_Controller.check_checkmate_text = "" + Switch_Modes_Controller.pause_game(False) elif game_mode == Switch_Modes_Controller.PLAY_MODE: log.info("Play Mode Activated\n") Switch_Modes_Controller.GAME_MODE = Switch_Modes_Controller.PLAY_MODE @@ -625,7 +626,7 @@ def play_to_paused(play_list, class_obj, color): if play_obj.coordinate is not None: class_obj(color, play_obj.coordinate_history, play_obj.coordinate) def rewind_moves(): - list_of_moves_backwards = Switch_Modes_Controller.list_of_moves_backwards(Move_Tracker.df_prior_moves) + list_of_moves_backwards = Switch_Modes_Controller.list_of_moves_backwards(Move_Tracker.df_prior_moves)[:-1] print("LIST OF MOVES BACKWARDS " + str(list_of_moves_backwards)) # list_of_moves_backwards list is ordered in descending order to the selected move for move_dict in list_of_moves_backwards: @@ -635,11 +636,16 @@ def rewind_moves(): if piece_history in dict(move_dict): if paused_obj.coordinate_history[piece_history] == ast.literal_eval(move_dict[piece_history]): paused_obj.coordinate = ast.literal_eval(move_dict[piece_history])['before'] + Grid_Controller.prior_move_color(paused_obj.coordinate, paused_obj) + prior_move_grid_and_piece_highlight_dict = Switch_Modes_Controller.list_of_moves_backwards(Move_Tracker.df_prior_moves)[-1] + old_grid_coordinate_before = ast.literal_eval(list(prior_move_grid_and_piece_highlight_dict.values())[0])['before'] + old_grid_coordinate_after = ast.literal_eval(list(prior_move_grid_and_piece_highlight_dict.values())[0])['after'] + old_piece = Grid_Controller.piece_on_grid(old_grid_coordinate_after) + Grid_Controller.prior_move_color(old_grid_coordinate_before, old_piece) def pause_game(paused): #%% Working on currently Switch_Modes_Controller.PAUSED = paused if Switch_Modes_Controller.PAUSED == True: - log.info("Paused") paused_objects.remove_all_paused() #print(str(Switch_Modes_Controller.list_of_moves_backwards(Move_Tracker.df_prior_moves))) Switch_Modes_Controller.play_to_paused(play_objects.PlayPawn.white_pawn_list, paused_objects.PausedPawn, "white") @@ -656,7 +662,6 @@ def pause_game(paused): Switch_Modes_Controller.play_to_paused(play_objects.PlayKing.black_king_list, paused_objects.PausedKing, "black") Switch_Modes_Controller.rewind_moves() else: - log.info("Resume") paused_objects.remove_all_paused() def list_of_moves_backwards(df_prior_moves): moves_backwards_list = [] @@ -680,7 +685,7 @@ def list_of_moves_backwards(df_prior_moves): moves_backwards_dict[move_num] = df_prior_moves.loc[move_num, 'white_move'] moves_backwards_list.append(moves_backwards_dict) # When select a move on pane, we take back the move right after that - return moves_backwards_list[:-1] + return moves_backwards_list class Move_Tracker(): From e69780f0775377f3aa59f9440f509544345c521a Mon Sep 17 00:00:00 2001 From: Brad Wyatt Date: Sun, 9 May 2021 14:23:01 -0700 Subject: [PATCH 10/16] rename col to color on paused objects --- Chess.py | 25 ++++++-- paused_objects.py | 158 +++++++++++++++++++++++++++++++++++----------- 2 files changed, 139 insertions(+), 44 deletions(-) diff --git a/Chess.py b/Chess.py index 3b57998..72c5cd7 100644 --- a/Chess.py +++ b/Chess.py @@ -553,13 +553,24 @@ def prior_move_color(grid_coordinate, prior_move_piece): else: grid.prior_move_color = False grid.no_highlight() - for piece_list in play_objects.Piece_Lists_Shortcut.all_pieces(): - for piece in piece_list: - if piece == prior_move_piece: - piece.prior_move_color = True - else: - piece.prior_move_color = False - piece.no_highlight() + if Switch_Modes_Controller.PAUSED == False: + # Updating prior move sprites for play objects + for piece_list in play_objects.Piece_Lists_Shortcut.all_pieces(): + for piece in piece_list: + if piece == prior_move_piece: + piece.prior_move_color = True + else: + piece.prior_move_color = False + piece.no_highlight() + else: + # Updating prior move sprites for pause objects + for piece_list in paused_objects.Piece_Lists_Shortcut.all_pieces(): + for piece in piece_list: + if piece == prior_move_piece: + piece.prior_move_color = True + else: + piece.prior_move_color = False + piece.prior_move_update() def piece_on_grid(grid_coordinate): for piece_list in play_objects.Piece_Lists_Shortcut.all_pieces(): for piece in piece_list: diff --git a/paused_objects.py b/paused_objects.py index 3004035..0482a80 100644 --- a/paused_objects.py +++ b/paused_objects.py @@ -46,14 +46,14 @@ def black_pieces(): class PausedPawn(pygame.sprite.Sprite): white_pawn_list = [] black_pawn_list = [] - def __init__(self, col, coordinate_history, coord=None): + def __init__(self, color, coordinate_history, coord=None): pygame.sprite.Sprite.__init__(self) - self.col = col - if self.col == "white": + self.color = color + if self.color == "white": self.image = IMAGES["SPR_WHITE_PAWN"] PAUSED_SPRITES.add(self) PausedPawn.white_pawn_list.append(self) - elif self.col == "black": + elif self.color == "black": self.image = IMAGES["SPR_BLACK_PAWN"] PAUSED_SPRITES.add(self) PausedPawn.black_pawn_list.append(self) @@ -63,26 +63,40 @@ def __init__(self, col, coordinate_history, coord=None): for grid in board.Grid.grid_list: if grid.coordinate == self.coordinate: self.rect.topleft = grid.rect.topleft + self.prior_move_color = False + self.taken_off_board = False def update(self): self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft def destroy(self): - if self.col == "white": + if self.color == "white": PausedPawn.white_pawn_list.remove(self) - elif self.col == "black": + elif self.color == "black": PausedPawn.black_pawn_list.remove(self) self.kill() + def prior_move_update(self): + if self.taken_off_board != True: + if(self.color == "white"): + if(self.prior_move_color == True): + self.image = IMAGES["SPR_WHITE_PAWN_PRIORMOVE"] + else: + self.image = IMAGES["SPR_WHITE_PAWN"] + elif(self.color == "black"): + if(self.prior_move_color == True): + self.image = IMAGES["SPR_BLACK_PAWN_PRIORMOVE"] + else: + self.image = IMAGES["SPR_BLACK_PAWN"] class PausedBishop(pygame.sprite.Sprite): white_bishop_list = [] black_bishop_list = [] - def __init__(self, col, coordinate_history, coord=None): + def __init__(self, color, coordinate_history, coord=None): pygame.sprite.Sprite.__init__(self) - self.col = col - if self.col == "white": + self.color = color + if self.color == "white": self.image = IMAGES["SPR_WHITE_BISHOP"] PAUSED_SPRITES.add(self) PausedBishop.white_bishop_list.append(self) - elif self.col == "black": + elif self.color == "black": self.image = IMAGES["SPR_BLACK_BISHOP"] PAUSED_SPRITES.add(self) PausedBishop.black_bishop_list.append(self) @@ -92,26 +106,40 @@ def __init__(self, col, coordinate_history, coord=None): for grid in board.Grid.grid_list: if grid.coordinate == self.coordinate: self.rect.topleft = grid.rect.topleft + self.prior_move_color = False + self.taken_off_board = False def update(self): self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft def destroy(self): - if self.col == "white": + if self.color == "white": PausedBishop.white_bishop_list.remove(self) - elif self.col == "black": + elif self.color == "black": PausedBishop.black_bishop_list.remove(self) self.kill() + def prior_move_update(self): + if self.taken_off_board != True: + if(self.color == "white"): + if(self.prior_move_color == True): + self.image = IMAGES["SPR_WHITE_BISHOP_PRIORMOVE"] + else: + self.image = IMAGES["SPR_WHITE_BISHOP"] + elif(self.color == "black"): + if(self.prior_move_color == True): + self.image = IMAGES["SPR_BLACK_BISHOP_PRIORMOVE"] + else: + self.image = IMAGES["SPR_BLACK_BISHOP"] class PausedKnight(pygame.sprite.Sprite): white_knight_list = [] black_knight_list = [] - def __init__(self, col, coordinate_history, coord=None): + def __init__(self, color, coordinate_history, coord=None): pygame.sprite.Sprite.__init__(self) - self.col = col - if self.col == "white": + self.color = color + if self.color == "white": self.image = IMAGES["SPR_WHITE_KNIGHT"] PAUSED_SPRITES.add(self) PausedKnight.white_knight_list.append(self) - elif self.col == "black": + elif self.color == "black": self.image = IMAGES["SPR_BLACK_KNIGHT"] PAUSED_SPRITES.add(self) PausedKnight.black_knight_list.append(self) @@ -121,26 +149,40 @@ def __init__(self, col, coordinate_history, coord=None): for grid in board.Grid.grid_list: if grid.coordinate == self.coordinate: self.rect.topleft = grid.rect.topleft + self.prior_move_color = False + self.taken_off_board = False def update(self): self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft def destroy(self): - if self.col == "white": + if self.color == "white": PausedKnight.white_knight_list.remove(self) - elif self.col == "black": + elif self.color == "black": PausedKnight.black_knight_list.remove(self) self.kill() + def prior_move_update(self): + if self.taken_off_board != True: + if(self.color == "white"): + if(self.prior_move_color == True): + self.image = IMAGES["SPR_WHITE_KNIGHT_PRIORMOVE"] + else: + self.image = IMAGES["SPR_WHITE_KNIGHT"] + elif(self.color == "black"): + if(self.prior_move_color == True): + self.image = IMAGES["SPR_BLACK_KNIGHT_PRIORMOVE"] + else: + self.image = IMAGES["SPR_BLACK_KNIGHT"] class PausedRook(pygame.sprite.Sprite): white_rook_list = [] black_rook_list = [] - def __init__(self, col, coordinate_history, coord=None): + def __init__(self, color, coordinate_history, coord=None): pygame.sprite.Sprite.__init__(self) - self.col = col - if self.col == "white": + self.color = color + if self.color == "white": self.image = IMAGES["SPR_WHITE_ROOK"] PAUSED_SPRITES.add(self) PausedRook.white_rook_list.append(self) - elif self.col == "black": + elif self.color == "black": self.image = IMAGES["SPR_BLACK_ROOK"] PAUSED_SPRITES.add(self) PausedRook.black_rook_list.append(self) @@ -150,26 +192,40 @@ def __init__(self, col, coordinate_history, coord=None): for grid in board.Grid.grid_list: if grid.coordinate == self.coordinate: self.rect.topleft = grid.rect.topleft + self.prior_move_color = False + self.taken_off_board = False def update(self): self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft def destroy(self): - if self.col == "white": + if self.color == "white": PausedRook.white_rook_list.remove(self) - elif self.col == "black": + elif self.color == "black": PausedRook.black_rook_list.remove(self) self.kill() + def prior_move_update(self): + if self.taken_off_board != True: + if(self.color == "white"): + if(self.prior_move_color == True): + self.image = IMAGES["SPR_WHITE_ROOK_PRIORMOVE"] + else: + self.image = IMAGES["SPR_WHITE_ROOK"] + elif(self.color == "black"): + if(self.prior_move_color == True): + self.image = IMAGES["SPR_BLACK_ROOK_PRIORMOVE"] + else: + self.image = IMAGES["SPR_BLACK_ROOK"] class PausedQueen(pygame.sprite.Sprite): white_queen_list = [] black_queen_list = [] - def __init__(self, col, coordinate_history, coord=None): + def __init__(self, color, coordinate_history, coord=None): pygame.sprite.Sprite.__init__(self) - self.col = col - if self.col == "white": + self.color = color + if self.color == "white": self.image = IMAGES["SPR_WHITE_QUEEN"] PAUSED_SPRITES.add(self) PausedQueen.white_queen_list.append(self) - elif self.col == "black": + elif self.color == "black": self.image = IMAGES["SPR_BLACK_QUEEN"] PAUSED_SPRITES.add(self) PausedQueen.black_queen_list.append(self) @@ -179,26 +235,40 @@ def __init__(self, col, coordinate_history, coord=None): for grid in board.Grid.grid_list: if grid.coordinate == self.coordinate: self.rect.topleft = grid.rect.topleft + self.prior_move_color = False + self.taken_off_board = False def update(self): self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft def destroy(self): - if self.col == "white": + if self.color == "white": PausedQueen.white_queen_list.remove(self) - elif self.col == "black": + elif self.color == "black": PausedQueen.black_queen_list.remove(self) self.kill() + def prior_move_update(self): + if self.taken_off_board != True: + if(self.color == "white"): + if(self.prior_move_color == True): + self.image = IMAGES["SPR_WHITE_QUEEN_PRIORMOVE"] + else: + self.image = IMAGES["SPR_WHITE_QUEEN"] + elif(self.color == "black"): + if(self.prior_move_color == True): + self.image = IMAGES["SPR_BLACK_QUEEN_PRIORMOVE"] + else: + self.image = IMAGES["SPR_BLACK_QUEEN"] class PausedKing(pygame.sprite.Sprite): white_king_list = [] black_king_list = [] - def __init__(self, col, coordinate_history, coord=None): + def __init__(self, color, coordinate_history, coord=None): pygame.sprite.Sprite.__init__(self) - self.col = col - if self.col == "white": + self.color = color + if self.color == "white": self.image = IMAGES["SPR_WHITE_KING"] PAUSED_SPRITES.add(self) PausedKing.white_king_list.append(self) - elif self.col == "black": + elif self.color == "black": self.image = IMAGES["SPR_BLACK_KING"] PAUSED_SPRITES.add(self) PausedKing.black_king_list.append(self) @@ -208,11 +278,25 @@ def __init__(self, col, coordinate_history, coord=None): for grid in board.Grid.grid_list: if grid.coordinate == self.coordinate: self.rect.topleft = grid.rect.topleft + self.prior_move_color = False + self.taken_off_board = False def update(self): self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft def destroy(self): - if self.col == "white": + if self.color == "white": PausedKing.white_king_list.remove(self) - elif self.col == "black": + elif self.color == "black": PausedKing.black_king_list.remove(self) - self.kill() \ No newline at end of file + self.kill() + def prior_move_update(self): + if self.taken_off_board != True: + if(self.color == "white"): + if(self.prior_move_color == True): + self.image = IMAGES["SPR_WHITE_KING_PRIORMOVE"] + else: + self.image = IMAGES["SPR_WHITE_KING"] + elif(self.color == "black"): + if(self.prior_move_color == True): + self.image = IMAGES["SPR_BLACK_KING_PRIORMOVE"] + else: + self.image = IMAGES["SPR_BLACK_KING"] \ No newline at end of file From 6fbd0d86ae0db47206c1c0aa20d0ebdb8412b73a Mon Sep 17 00:00:00 2001 From: Brad Wyatt Date: Sun, 9 May 2021 15:06:20 -0700 Subject: [PATCH 11/16] prior move highlighted piece works now --- Chess.py | 34 +++++++++++++--------- placed_objects.py | 72 +++++++++++++++++++++++------------------------ start_objects.py | 48 +++++++++++++++---------------- 3 files changed, 81 insertions(+), 73 deletions(-) diff --git a/Chess.py b/Chess.py index 72c5cd7..f5dd4d3 100644 --- a/Chess.py +++ b/Chess.py @@ -563,6 +563,8 @@ def prior_move_color(grid_coordinate, prior_move_piece): piece.prior_move_color = False piece.no_highlight() else: + #%% Prior move color func + #print("PIECE: " + str(prior_move_piece) + " with coordinate " + str(prior_move_piece.coordinate)) # Updating prior move sprites for pause objects for piece_list in paused_objects.Piece_Lists_Shortcut.all_pieces(): for piece in piece_list: @@ -571,11 +573,18 @@ def prior_move_color(grid_coordinate, prior_move_piece): else: piece.prior_move_color = False piece.prior_move_update() + #print("Does piece have red color? " + str(prior_move_piece.prior_move_color)) def piece_on_grid(grid_coordinate): - for piece_list in play_objects.Piece_Lists_Shortcut.all_pieces(): - for piece in piece_list: - if grid_coordinate == piece.coordinate: - return piece + if Switch_Modes_Controller.PAUSED == False: + for piece_list in play_objects.Piece_Lists_Shortcut.all_pieces(): + for piece in piece_list: + if grid_coordinate == piece.coordinate: + return piece + else: + for piece_list in paused_objects.Piece_Lists_Shortcut.all_pieces(): + for piece in piece_list: + if grid_coordinate == piece.coordinate: + return piece def update_prior_move_color(whoseturn): if Move_Tracker.move_counter() == 0: for grid in board.Grid.grid_list: @@ -609,7 +618,7 @@ def switch_mode(game_mode, PLAY_EDIT_SWITCH_BUTTON): Switch_Modes_Controller.GAME_MODE = Switch_Modes_Controller.EDIT_MODE PLAY_EDIT_SWITCH_BUTTON.image = PLAY_EDIT_SWITCH_BUTTON.game_mode_button(Switch_Modes_Controller.GAME_MODE) Text_Controller.check_checkmate_text = "" - Switch_Modes_Controller.pause_game(False) + Switch_Modes_Controller.pause_game(False, game_controller) elif game_mode == Switch_Modes_Controller.PLAY_MODE: log.info("Play Mode Activated\n") Switch_Modes_Controller.GAME_MODE = Switch_Modes_Controller.PLAY_MODE @@ -638,7 +647,6 @@ def play_to_paused(play_list, class_obj, color): class_obj(color, play_obj.coordinate_history, play_obj.coordinate) def rewind_moves(): list_of_moves_backwards = Switch_Modes_Controller.list_of_moves_backwards(Move_Tracker.df_prior_moves)[:-1] - print("LIST OF MOVES BACKWARDS " + str(list_of_moves_backwards)) # list_of_moves_backwards list is ordered in descending order to the selected move for move_dict in list_of_moves_backwards: for paused_obj_list in paused_objects.Piece_Lists_Shortcut.all_pieces(): @@ -653,12 +661,11 @@ def rewind_moves(): old_grid_coordinate_after = ast.literal_eval(list(prior_move_grid_and_piece_highlight_dict.values())[0])['after'] old_piece = Grid_Controller.piece_on_grid(old_grid_coordinate_after) Grid_Controller.prior_move_color(old_grid_coordinate_before, old_piece) - def pause_game(paused): + def pause_game(paused, game_controller): #%% Working on currently Switch_Modes_Controller.PAUSED = paused if Switch_Modes_Controller.PAUSED == True: paused_objects.remove_all_paused() - #print(str(Switch_Modes_Controller.list_of_moves_backwards(Move_Tracker.df_prior_moves))) Switch_Modes_Controller.play_to_paused(play_objects.PlayPawn.white_pawn_list, paused_objects.PausedPawn, "white") Switch_Modes_Controller.play_to_paused(play_objects.PlayBishop.white_bishop_list, paused_objects.PausedBishop, "white") Switch_Modes_Controller.play_to_paused(play_objects.PlayKnight.white_knight_list, paused_objects.PausedKnight, "white") @@ -674,11 +681,11 @@ def pause_game(paused): Switch_Modes_Controller.rewind_moves() else: paused_objects.remove_all_paused() + Grid_Controller.update_prior_move_color(game_controller.WHOSETURN) def list_of_moves_backwards(df_prior_moves): moves_backwards_list = [] limit_moves = Move_Tracker.selected_move[0] limit_color = Move_Tracker.selected_move[1] - #Move_Tracker.selected_move = (0, "") for move_num in range(Move_Tracker.move_counter(), limit_moves-1, -1): moves_backwards_dict = {} if limit_color == 'black_move' and move_num == limit_moves: @@ -994,6 +1001,7 @@ def select_piece_unselect_all_others(mousepos, game_controller): clicked_piece.spaces_available(game_controller) clicked_piece = None def undo_move(game_controller): + Switch_Modes_Controller.pause_game(False, game_controller) pieces_to_undo = [] # Using pieces_to_undo as a list for castling if Move_Tracker.move_counter() >= 1: @@ -1368,7 +1376,7 @@ def record_move(game_controller, grid, piece, prior_moves_dict, captured_abb, sp prior_moves_dict['move_notation'] = Move_Controller.move_translator(grid.occupied_piece, piece_in_funcs, captured_abb, special_abb, check_abb) Move_Tracker.selected_move = (Move_Tracker.move_counter(), "white_move") Move_Tracker.df_prior_moves.loc[Move_Tracker.move_counter(), "white_move"] = str(prior_moves_dict) - Switch_Modes_Controller.pause_game(False) + Switch_Modes_Controller.pause_game(False, game_controller) log.info(move_text) if game_controller.result_abb != "*": log.info(game_controller.result_abb) @@ -1532,11 +1540,11 @@ def mouse_coordinate(mousepos): if Move_Tracker.selected_move[0] == len(Move_Tracker.df_moves): if Move_Tracker.df_moves.loc[len(Move_Tracker.df_moves), "black_move"] != "" \ and piece_move_rect.move_color == "white_move": - Switch_Modes_Controller.pause_game(True) + Switch_Modes_Controller.pause_game(True, game_controller) else: - Switch_Modes_Controller.pause_game(False) + Switch_Modes_Controller.pause_game(False, game_controller) else: - Switch_Modes_Controller.pause_game(True) + Switch_Modes_Controller.pause_game(True, game_controller) # Editing mode only if Switch_Modes_Controller.GAME_MODE == Switch_Modes_Controller.EDIT_MODE: #BUTTONS diff --git a/placed_objects.py b/placed_objects.py index b027f83..beddf8b 100644 --- a/placed_objects.py +++ b/placed_objects.py @@ -42,14 +42,14 @@ def remove_all_placed(): class PlacedPawn(pygame.sprite.Sprite): white_pawn_list = [] black_pawn_list = [] - def __init__(self, coord, col): + def __init__(self, coord, color): pygame.sprite.Sprite.__init__(self) - self.col = col - if self.col == "white": + self.color = color + if self.color == "white": self.image = IMAGES["SPR_WHITE_PAWN"] PLACED_SPRITES.add(self) PlacedPawn.white_pawn_list.append(self) - elif self.col == "black": + elif self.color == "black": self.image = IMAGES["SPR_BLACK_PAWN"] PLACED_SPRITES.add(self) PlacedPawn.black_pawn_list.append(self) @@ -61,23 +61,23 @@ def __init__(self, coord, col): def update(self): self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft def destroy(self): - if self.col == "white": + if self.color == "white": PlacedPawn.white_pawn_list.remove(self) - elif self.col == "black": + elif self.color == "black": PlacedPawn.black_pawn_list.remove(self) self.kill() class PlacedBishop(pygame.sprite.Sprite): white_bishop_list = [] black_bishop_list = [] - def __init__(self, coord, col): + def __init__(self, coord, color): pygame.sprite.Sprite.__init__(self) - self.col = col - if self.col == "white": + self.color = color + if self.color == "white": self.image = IMAGES["SPR_WHITE_BISHOP"] PLACED_SPRITES.add(self) PlacedBishop.white_bishop_list.append(self) - elif self.col == "black": + elif self.color == "black": self.image = IMAGES["SPR_BLACK_BISHOP"] PLACED_SPRITES.add(self) PlacedBishop.black_bishop_list.append(self) @@ -89,23 +89,23 @@ def __init__(self, coord, col): def update(self): self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft def destroy(self): - if self.col == "white": + if self.color == "white": PlacedBishop.white_bishop_list.remove(self) - elif self.col == "black": + elif self.color == "black": PlacedBishop.black_bishop_list.remove(self) self.kill() class PlacedKnight(pygame.sprite.Sprite): white_knight_list = [] black_knight_list = [] - def __init__(self, coord, col): + def __init__(self, coord, color): pygame.sprite.Sprite.__init__(self) - self.col = col - if self.col == "white": + self.color = color + if self.color == "white": self.image = IMAGES["SPR_WHITE_KNIGHT"] PLACED_SPRITES.add(self) PlacedKnight.white_knight_list.append(self) - elif self.col == "black": + elif self.color == "black": self.image = IMAGES["SPR_BLACK_KNIGHT"] PLACED_SPRITES.add(self) PlacedKnight.black_knight_list.append(self) @@ -117,23 +117,23 @@ def __init__(self, coord, col): def update(self): self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft def destroy(self): - if self.col == "white": + if self.color == "white": PlacedKnight.white_knight_list.remove(self) - elif self.col == "black": + elif self.color == "black": PlacedKnight.black_knight_list.remove(self) self.kill() class PlacedRook(pygame.sprite.Sprite): white_rook_list = [] black_rook_list = [] - def __init__(self, coord, col): + def __init__(self, coord, color): pygame.sprite.Sprite.__init__(self) - self.col = col - if self.col == "white": + self.color = color + if self.color == "white": self.image = IMAGES["SPR_WHITE_ROOK"] PLACED_SPRITES.add(self) PlacedRook.white_rook_list.append(self) - elif self.col == "black": + elif self.color == "black": self.image = IMAGES["SPR_BLACK_ROOK"] PLACED_SPRITES.add(self) PlacedRook.black_rook_list.append(self) @@ -145,23 +145,23 @@ def __init__(self, coord, col): def update(self): self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft def destroy(self): - if self.col == "white": + if self.color == "white": PlacedRook.white_rook_list.remove(self) - elif self.col == "black": + elif self.color == "black": PlacedRook.black_rook_list.remove(self) self.kill() class PlacedQueen(pygame.sprite.Sprite): white_queen_list = [] black_queen_list = [] - def __init__(self, coord, col): + def __init__(self, coord, color): pygame.sprite.Sprite.__init__(self) - self.col = col - if self.col == "white": + self.color = color + if self.color == "white": self.image = IMAGES["SPR_WHITE_QUEEN"] PLACED_SPRITES.add(self) PlacedQueen.white_queen_list.append(self) - elif self.col == "black": + elif self.color == "black": self.image = IMAGES["SPR_BLACK_QUEEN"] PLACED_SPRITES.add(self) PlacedQueen.black_queen_list.append(self) @@ -173,23 +173,23 @@ def __init__(self, coord, col): def update(self): self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft def destroy(self): - if self.col == "white": + if self.color == "white": PlacedQueen.white_queen_list.remove(self) - elif self.col == "black": + elif self.color == "black": PlacedQueen.black_queen_list.remove(self) self.kill() class PlacedKing(pygame.sprite.Sprite): white_king_list = [] black_king_list = [] - def __init__(self, coord, col): + def __init__(self, coord, color): pygame.sprite.Sprite.__init__(self) - self.col = col - if self.col == "white": + self.color = color + if self.color == "white": self.image = IMAGES["SPR_WHITE_KING"] PLACED_SPRITES.add(self) PlacedKing.white_king_list.append(self) - elif self.col == "black": + elif self.color == "black": self.image = IMAGES["SPR_BLACK_KING"] PLACED_SPRITES.add(self) PlacedKing.black_king_list.append(self) @@ -201,8 +201,8 @@ def __init__(self, coord, col): def update(self): self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft def destroy(self): - if self.col == "white": + if self.color == "white": PlacedKing.white_king_list.remove(self) - elif self.col == "black": + elif self.color == "black": PlacedKing.black_king_list.remove(self) self.kill() \ No newline at end of file diff --git a/start_objects.py b/start_objects.py index 8635aab..3655820 100644 --- a/start_objects.py +++ b/start_objects.py @@ -32,12 +32,12 @@ def draw(self, screen): StartPiecesBehind(IMAGES["SPR_BLACK_KING"], initvar.STARTPOS['black_king']) class StartPawn(pygame.sprite.Sprite): - def __init__(self, col, pos): + def __init__(self, color, pos): pygame.sprite.Sprite.__init__(self) - self.col = col - if self.col == "white": + self.color = color + if self.color == "white": self.image = IMAGES["SPR_WHITE_PAWN"] - elif self.col == "black": + elif self.color == "black": self.image = IMAGES["SPR_BLACK_PAWN"] self.rect = self.image.get_rect() self.rect.topleft = pos @@ -46,12 +46,12 @@ def update(self): pass class StartBishop(pygame.sprite.Sprite): - def __init__(self, col, pos): + def __init__(self, color, pos): pygame.sprite.Sprite.__init__(self) - self.col = col - if self.col == "white": + self.color = color + if self.color == "white": self.image = IMAGES["SPR_WHITE_BISHOP"] - elif self.col == "black": + elif self.color == "black": self.image = IMAGES["SPR_BLACK_BISHOP"] self.rect = self.image.get_rect() self.rect.topleft = pos @@ -60,12 +60,12 @@ def update(self): pass class StartKnight(pygame.sprite.Sprite): - def __init__(self, col, pos): + def __init__(self, color, pos): pygame.sprite.Sprite.__init__(self) - self.col = col - if self.col == "white": + self.color = color + if self.color == "white": self.image = IMAGES["SPR_WHITE_KNIGHT"] - elif self.col == "black": + elif self.color == "black": self.image = IMAGES["SPR_BLACK_KNIGHT"] self.rect = self.image.get_rect() self.rect.topleft = pos @@ -74,12 +74,12 @@ def update(self): pass class StartRook(pygame.sprite.Sprite): - def __init__(self, col, pos): + def __init__(self, color, pos): pygame.sprite.Sprite.__init__(self) - self.col = col - if self.col == "white": + self.color = color + if self.color == "white": self.image = IMAGES["SPR_WHITE_ROOK"] - elif self.col == "black": + elif self.color == "black": self.image = IMAGES["SPR_BLACK_ROOK"] self.rect = self.image.get_rect() self.rect.topleft = pos @@ -88,12 +88,12 @@ def update(self): pass class StartQueen(pygame.sprite.Sprite): - def __init__(self, col, pos): + def __init__(self, color, pos): pygame.sprite.Sprite.__init__(self) - self.col = col - if self.col == "white": + self.color = color + if self.color == "white": self.image = IMAGES["SPR_WHITE_QUEEN"] - elif self.col == "black": + elif self.color == "black": self.image = IMAGES["SPR_BLACK_QUEEN"] self.rect = self.image.get_rect() self.rect.topleft = pos @@ -102,12 +102,12 @@ def update(self): pass class StartKing(pygame.sprite.Sprite): - def __init__(self, col, pos): + def __init__(self, color, pos): pygame.sprite.Sprite.__init__(self) - self.col = col - if self.col == "white": + self.color = color + if self.color == "white": self.image = IMAGES["SPR_WHITE_KING"] - elif self.col == "black": + elif self.color == "black": self.image = IMAGES["SPR_BLACK_KING"] self.rect = self.image.get_rect() self.rect.topleft = pos From 09332d8f07654b40f4aec58b59088becfbcae033 Mon Sep 17 00:00:00 2001 From: Brad Wyatt Date: Sun, 9 May 2021 15:21:03 -0700 Subject: [PATCH 12/16] including castling when pausing --- Chess.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Chess.py b/Chess.py index f5dd4d3..a4b2eac 100644 --- a/Chess.py +++ b/Chess.py @@ -618,7 +618,6 @@ def switch_mode(game_mode, PLAY_EDIT_SWITCH_BUTTON): Switch_Modes_Controller.GAME_MODE = Switch_Modes_Controller.EDIT_MODE PLAY_EDIT_SWITCH_BUTTON.image = PLAY_EDIT_SWITCH_BUTTON.game_mode_button(Switch_Modes_Controller.GAME_MODE) Text_Controller.check_checkmate_text = "" - Switch_Modes_Controller.pause_game(False, game_controller) elif game_mode == Switch_Modes_Controller.PLAY_MODE: log.info("Play Mode Activated\n") Switch_Modes_Controller.GAME_MODE = Switch_Modes_Controller.PLAY_MODE @@ -656,6 +655,16 @@ def rewind_moves(): if paused_obj.coordinate_history[piece_history] == ast.literal_eval(move_dict[piece_history]): paused_obj.coordinate = ast.literal_eval(move_dict[piece_history])['before'] Grid_Controller.prior_move_color(paused_obj.coordinate, paused_obj) + if ast.literal_eval(move_dict[piece_history])['move_notation'] == "O-O": + if paused_obj.color == "white": + Grid_Controller.piece_on_grid('f1').coordinate = 'h1' + elif paused_obj.color == "black": + Grid_Controller.piece_on_grid('f8').coordinate = 'h8' + elif ast.literal_eval(move_dict[piece_history])['move_notation'] == "O-O-O": + if paused_obj.color == "white": + Grid_Controller.piece_on_grid('d1').coordinate = 'a1' + elif paused_obj.color == "black": + Grid_Controller.piece_on_grid('d8').coordinate = 'a8' prior_move_grid_and_piece_highlight_dict = Switch_Modes_Controller.list_of_moves_backwards(Move_Tracker.df_prior_moves)[-1] old_grid_coordinate_before = ast.literal_eval(list(prior_move_grid_and_piece_highlight_dict.values())[0])['before'] old_grid_coordinate_after = ast.literal_eval(list(prior_move_grid_and_piece_highlight_dict.values())[0])['after'] @@ -799,6 +808,7 @@ def __del__(self): play_objects.PlayKing.black_king_list = [] for grid in board.Grid.grid_list: grid.reset_play_interaction_vars() + Switch_Modes_Controller.pause_game(False, self) # Reset Moves Panel MoveNumberRectangle.rectangle_list = [] PieceMoveRectangle.rectangle_list = [] @@ -1638,6 +1648,7 @@ def update_pieces_and_board(): # MIDDLE MOUSE DEBUGGER if event.type == pygame.MOUSEBUTTONDOWN and pygame.mouse.get_pressed()[1]: + #%% Middle mouse debugger def test_piece(): for piece_list in play_objects.Piece_Lists_Shortcut.all_pieces(): for piece in piece_list: From ef1dc630bcc3df090bf1bd968085f911bf40b641 Mon Sep 17 00:00:00 2001 From: Brad Wyatt Date: Sun, 9 May 2021 15:26:40 -0700 Subject: [PATCH 13/16] change name of everything paused to replayed --- Chess.py | 94 +++++++------- paused_objects.py => replayed_objects.py | 148 +++++++++++------------ 2 files changed, 121 insertions(+), 121 deletions(-) rename paused_objects.py => replayed_objects.py (69%) diff --git a/Chess.py b/Chess.py index a4b2eac..e23df0d 100644 --- a/Chess.py +++ b/Chess.py @@ -4,7 +4,7 @@ Features To-Do (short-term): Play back moves Be able to click on a move in the pane to view it ---> One idea I had before was pause mode (mode between edit and play) +--> One idea I had before was replayed mode (mode between edit and play) --> I think the right thing to do would take the df_prior_moves to get the move. The tricky part is differentiating this from Undo move (through a conditional variable checking within each class for example). And coloring prior_move_color appropriately. Menu objects are still invisible yet clickable @@ -37,7 +37,7 @@ import start_objects import placed_objects import play_objects -import paused_objects +import replayed_objects from load_images_sounds import * from menu_buttons import * import random @@ -553,7 +553,7 @@ def prior_move_color(grid_coordinate, prior_move_piece): else: grid.prior_move_color = False grid.no_highlight() - if Switch_Modes_Controller.PAUSED == False: + if Switch_Modes_Controller.REPLAYED == False: # Updating prior move sprites for play objects for piece_list in play_objects.Piece_Lists_Shortcut.all_pieces(): for piece in piece_list: @@ -565,8 +565,8 @@ def prior_move_color(grid_coordinate, prior_move_piece): else: #%% Prior move color func #print("PIECE: " + str(prior_move_piece) + " with coordinate " + str(prior_move_piece.coordinate)) - # Updating prior move sprites for pause objects - for piece_list in paused_objects.Piece_Lists_Shortcut.all_pieces(): + # Updating prior move sprites for replayed objects + for piece_list in replayed_objects.Piece_Lists_Shortcut.all_pieces(): for piece in piece_list: if piece == prior_move_piece: piece.prior_move_color = True @@ -575,13 +575,13 @@ def prior_move_color(grid_coordinate, prior_move_piece): piece.prior_move_update() #print("Does piece have red color? " + str(prior_move_piece.prior_move_color)) def piece_on_grid(grid_coordinate): - if Switch_Modes_Controller.PAUSED == False: + if Switch_Modes_Controller.REPLAYED == False: for piece_list in play_objects.Piece_Lists_Shortcut.all_pieces(): for piece in piece_list: if grid_coordinate == piece.coordinate: return piece else: - for piece_list in paused_objects.Piece_Lists_Shortcut.all_pieces(): + for piece_list in replayed_objects.Piece_Lists_Shortcut.all_pieces(): for piece in piece_list: if grid_coordinate == piece.coordinate: return piece @@ -611,7 +611,7 @@ def update_prior_move_color(whoseturn): class Switch_Modes_Controller(): EDIT_MODE, PLAY_MODE = 0, 1 GAME_MODE = EDIT_MODE - PAUSED = False + REPLAYED = False def switch_mode(game_mode, PLAY_EDIT_SWITCH_BUTTON): if game_mode == Switch_Modes_Controller.EDIT_MODE: log.info("\nEditing Mode Activated\n") @@ -639,7 +639,7 @@ def placed_to_play(placed_list, class_obj, color): # Play pieces spawn where their placed piece correspondents are located for placed_obj in placed_list: class_obj(placed_obj.coordinate, color) - def play_to_paused(play_list, class_obj, color): + def play_to_replayed(play_list, class_obj, color): # Play pieces spawn where their placed piece correspondents are located for play_obj in play_list: if play_obj.coordinate is not None: @@ -648,48 +648,48 @@ def rewind_moves(): list_of_moves_backwards = Switch_Modes_Controller.list_of_moves_backwards(Move_Tracker.df_prior_moves)[:-1] # list_of_moves_backwards list is ordered in descending order to the selected move for move_dict in list_of_moves_backwards: - for paused_obj_list in paused_objects.Piece_Lists_Shortcut.all_pieces(): - for paused_obj in paused_obj_list: - for piece_history in paused_obj.coordinate_history: + for replayed_obj_list in replayed_objects.Piece_Lists_Shortcut.all_pieces(): + for replayed_obj in replayed_obj_list: + for piece_history in replayed_obj.coordinate_history: if piece_history in dict(move_dict): - if paused_obj.coordinate_history[piece_history] == ast.literal_eval(move_dict[piece_history]): - paused_obj.coordinate = ast.literal_eval(move_dict[piece_history])['before'] - Grid_Controller.prior_move_color(paused_obj.coordinate, paused_obj) + if replayed_obj.coordinate_history[piece_history] == ast.literal_eval(move_dict[piece_history]): + replayed_obj.coordinate = ast.literal_eval(move_dict[piece_history])['before'] + Grid_Controller.prior_move_color(replayed_obj.coordinate, replayed_obj) if ast.literal_eval(move_dict[piece_history])['move_notation'] == "O-O": - if paused_obj.color == "white": + if replayed_obj.color == "white": Grid_Controller.piece_on_grid('f1').coordinate = 'h1' - elif paused_obj.color == "black": + elif replayed_obj.color == "black": Grid_Controller.piece_on_grid('f8').coordinate = 'h8' elif ast.literal_eval(move_dict[piece_history])['move_notation'] == "O-O-O": - if paused_obj.color == "white": + if replayed_obj.color == "white": Grid_Controller.piece_on_grid('d1').coordinate = 'a1' - elif paused_obj.color == "black": + elif replayed_obj.color == "black": Grid_Controller.piece_on_grid('d8').coordinate = 'a8' prior_move_grid_and_piece_highlight_dict = Switch_Modes_Controller.list_of_moves_backwards(Move_Tracker.df_prior_moves)[-1] old_grid_coordinate_before = ast.literal_eval(list(prior_move_grid_and_piece_highlight_dict.values())[0])['before'] old_grid_coordinate_after = ast.literal_eval(list(prior_move_grid_and_piece_highlight_dict.values())[0])['after'] old_piece = Grid_Controller.piece_on_grid(old_grid_coordinate_after) Grid_Controller.prior_move_color(old_grid_coordinate_before, old_piece) - def pause_game(paused, game_controller): + def replayed_game(replayed, game_controller): #%% Working on currently - Switch_Modes_Controller.PAUSED = paused - if Switch_Modes_Controller.PAUSED == True: - paused_objects.remove_all_paused() - Switch_Modes_Controller.play_to_paused(play_objects.PlayPawn.white_pawn_list, paused_objects.PausedPawn, "white") - Switch_Modes_Controller.play_to_paused(play_objects.PlayBishop.white_bishop_list, paused_objects.PausedBishop, "white") - Switch_Modes_Controller.play_to_paused(play_objects.PlayKnight.white_knight_list, paused_objects.PausedKnight, "white") - Switch_Modes_Controller.play_to_paused(play_objects.PlayRook.white_rook_list, paused_objects.PausedRook, "white") - Switch_Modes_Controller.play_to_paused(play_objects.PlayQueen.white_queen_list, paused_objects.PausedQueen, "white") - Switch_Modes_Controller.play_to_paused(play_objects.PlayKing.white_king_list, paused_objects.PausedKing, "white") - Switch_Modes_Controller.play_to_paused(play_objects.PlayPawn.black_pawn_list, paused_objects.PausedPawn, "black") - Switch_Modes_Controller.play_to_paused(play_objects.PlayBishop.black_bishop_list, paused_objects.PausedBishop, "black") - Switch_Modes_Controller.play_to_paused(play_objects.PlayKnight.black_knight_list, paused_objects.PausedKnight, "black") - Switch_Modes_Controller.play_to_paused(play_objects.PlayRook.black_rook_list, paused_objects.PausedRook, "black") - Switch_Modes_Controller.play_to_paused(play_objects.PlayQueen.black_queen_list, paused_objects.PausedQueen, "black") - Switch_Modes_Controller.play_to_paused(play_objects.PlayKing.black_king_list, paused_objects.PausedKing, "black") + Switch_Modes_Controller.REPLAYED = replayed + if Switch_Modes_Controller.REPLAYED == True: + replayed_objects.remove_all_replayed() + Switch_Modes_Controller.play_to_replayed(play_objects.PlayPawn.white_pawn_list, replayed_objects.ReplayedPawn, "white") + Switch_Modes_Controller.play_to_replayed(play_objects.PlayBishop.white_bishop_list, replayed_objects.ReplayedBishop, "white") + Switch_Modes_Controller.play_to_replayed(play_objects.PlayKnight.white_knight_list, replayed_objects.ReplayedKnight, "white") + Switch_Modes_Controller.play_to_replayed(play_objects.PlayRook.white_rook_list, replayed_objects.ReplayedRook, "white") + Switch_Modes_Controller.play_to_replayed(play_objects.PlayQueen.white_queen_list, replayed_objects.ReplayedQueen, "white") + Switch_Modes_Controller.play_to_replayed(play_objects.PlayKing.white_king_list, replayed_objects.ReplayedKing, "white") + Switch_Modes_Controller.play_to_replayed(play_objects.PlayPawn.black_pawn_list, replayed_objects.ReplayedPawn, "black") + Switch_Modes_Controller.play_to_replayed(play_objects.PlayBishop.black_bishop_list, replayed_objects.ReplayedBishop, "black") + Switch_Modes_Controller.play_to_replayed(play_objects.PlayKnight.black_knight_list, replayed_objects.ReplayedKnight, "black") + Switch_Modes_Controller.play_to_replayed(play_objects.PlayRook.black_rook_list, replayed_objects.ReplayedRook, "black") + Switch_Modes_Controller.play_to_replayed(play_objects.PlayQueen.black_queen_list, replayed_objects.ReplayedQueen, "black") + Switch_Modes_Controller.play_to_replayed(play_objects.PlayKing.black_king_list, replayed_objects.ReplayedKing, "black") Switch_Modes_Controller.rewind_moves() else: - paused_objects.remove_all_paused() + replayed_objects.remove_all_replayed() Grid_Controller.update_prior_move_color(game_controller.WHOSETURN) def list_of_moves_backwards(df_prior_moves): moves_backwards_list = [] @@ -808,7 +808,7 @@ def __del__(self): play_objects.PlayKing.black_king_list = [] for grid in board.Grid.grid_list: grid.reset_play_interaction_vars() - Switch_Modes_Controller.pause_game(False, self) + Switch_Modes_Controller.replayed_game(False, self) # Reset Moves Panel MoveNumberRectangle.rectangle_list = [] PieceMoveRectangle.rectangle_list = [] @@ -988,7 +988,7 @@ def select_piece_unselect_all_others(mousepos, game_controller): for piece_list in play_objects.Piece_Lists_Shortcut.white_pieces(): for piece in piece_list: # Selects piece - if (piece.rect.collidepoint(mousepos) and piece.select == False and Switch_Modes_Controller.PAUSED == False): + if (piece.rect.collidepoint(mousepos) and piece.select == False and Switch_Modes_Controller.REPLAYED == False): clicked_piece = piece else: # Unselects piece @@ -999,7 +999,7 @@ def select_piece_unselect_all_others(mousepos, game_controller): elif game_controller.WHOSETURN == "black": for piece_list in play_objects.Piece_Lists_Shortcut.black_pieces(): for piece in piece_list: - if (piece.rect.collidepoint(mousepos) and piece.select == False and Switch_Modes_Controller.PAUSED == False): + if (piece.rect.collidepoint(mousepos) and piece.select == False and Switch_Modes_Controller.REPLAYED == False): clicked_piece = piece else: piece.no_highlight() @@ -1011,7 +1011,7 @@ def select_piece_unselect_all_others(mousepos, game_controller): clicked_piece.spaces_available(game_controller) clicked_piece = None def undo_move(game_controller): - Switch_Modes_Controller.pause_game(False, game_controller) + Switch_Modes_Controller.replayed_game(False, game_controller) pieces_to_undo = [] # Using pieces_to_undo as a list for castling if Move_Tracker.move_counter() >= 1: @@ -1386,7 +1386,7 @@ def record_move(game_controller, grid, piece, prior_moves_dict, captured_abb, sp prior_moves_dict['move_notation'] = Move_Controller.move_translator(grid.occupied_piece, piece_in_funcs, captured_abb, special_abb, check_abb) Move_Tracker.selected_move = (Move_Tracker.move_counter(), "white_move") Move_Tracker.df_prior_moves.loc[Move_Tracker.move_counter(), "white_move"] = str(prior_moves_dict) - Switch_Modes_Controller.pause_game(False, game_controller) + Switch_Modes_Controller.replayed_game(False, game_controller) log.info(move_text) if game_controller.result_abb != "*": log.info(game_controller.result_abb) @@ -1550,11 +1550,11 @@ def mouse_coordinate(mousepos): if Move_Tracker.selected_move[0] == len(Move_Tracker.df_moves): if Move_Tracker.df_moves.loc[len(Move_Tracker.df_moves), "black_move"] != "" \ and piece_move_rect.move_color == "white_move": - Switch_Modes_Controller.pause_game(True, game_controller) + Switch_Modes_Controller.replayed_game(True, game_controller) else: - Switch_Modes_Controller.pause_game(False, game_controller) + Switch_Modes_Controller.replayed_game(False, game_controller) else: - Switch_Modes_Controller.pause_game(True, game_controller) + Switch_Modes_Controller.replayed_game(True, game_controller) # Editing mode only if Switch_Modes_Controller.GAME_MODE == Switch_Modes_Controller.EDIT_MODE: #BUTTONS @@ -1694,12 +1694,12 @@ def test_grid_str(): placed_objects.PLACED_SPRITES.draw(SCREEN) elif(Switch_Modes_Controller.GAME_MODE == Switch_Modes_Controller.PLAY_MODE): #Only draw play sprites in play mode FLIP_BOARD_BUTTON.draw(SCREEN) - if Switch_Modes_Controller.PAUSED == False: + if Switch_Modes_Controller.REPLAYED == False: play_objects.PLAY_SPRITES.update() play_objects.PLAY_SPRITES.draw(SCREEN) else: - paused_objects.PAUSED_SPRITES.update() - paused_objects.PAUSED_SPRITES.draw(SCREEN) + replayed_objects.REPLAYED_SPRITES.update() + replayed_objects.REPLAYED_SPRITES.draw(SCREEN) PGN_SAVE_FILE_BUTTON.draw(SCREEN) PLAY_PANEL_SPRITES.draw(SCREEN) diff --git a/paused_objects.py b/replayed_objects.py similarity index 69% rename from paused_objects.py rename to replayed_objects.py index 0482a80..3e5147c 100644 --- a/paused_objects.py +++ b/replayed_objects.py @@ -2,48 +2,48 @@ from load_images_sounds import * import board -PAUSED_SPRITES = pygame.sprite.Group() +REPLAYED_SPRITES = pygame.sprite.Group() -def remove_all_paused(): - for spr_list in [PausedPawn.white_pawn_list, PausedBishop.white_bishop_list, - PausedKnight.white_knight_list, PausedRook.white_rook_list, - PausedQueen.white_queen_list, PausedKing.white_king_list, - PausedPawn.black_pawn_list, PausedBishop.black_bishop_list, - PausedKnight.black_knight_list, PausedRook.black_rook_list, - PausedQueen.black_queen_list, PausedKing.black_king_list]: +def remove_all_replayed(): + for spr_list in [ReplayedPawn.white_pawn_list, ReplayedBishop.white_bishop_list, + ReplayedKnight.white_knight_list, ReplayedRook.white_rook_list, + ReplayedQueen.white_queen_list, ReplayedKing.white_king_list, + ReplayedPawn.black_pawn_list, ReplayedBishop.black_bishop_list, + ReplayedKnight.black_knight_list, ReplayedRook.black_rook_list, + ReplayedQueen.black_queen_list, ReplayedKing.black_king_list]: for obj in spr_list: obj.kill() - PausedPawn.white_pawn_list = [] - PausedBishop.white_bishop_list = [] - PausedKnight.white_knight_list = [] - PausedRook.white_rook_list = [] - PausedQueen.white_queen_list = [] - PausedKing.white_king_list = [] - PausedPawn.black_pawn_list = [] - PausedBishop.black_bishop_list = [] - PausedKnight.black_knight_list = [] - PausedRook.black_rook_list = [] - PausedQueen.black_queen_list = [] - PausedKing.black_king_list = [] + ReplayedPawn.white_pawn_list = [] + ReplayedBishop.white_bishop_list = [] + ReplayedKnight.white_knight_list = [] + ReplayedRook.white_rook_list = [] + ReplayedQueen.white_queen_list = [] + ReplayedKing.white_king_list = [] + ReplayedPawn.black_pawn_list = [] + ReplayedBishop.black_bishop_list = [] + ReplayedKnight.black_knight_list = [] + ReplayedRook.black_rook_list = [] + ReplayedQueen.black_queen_list = [] + ReplayedKing.black_king_list = [] class Piece_Lists_Shortcut(): def all_pieces(): - return [PausedPawn.white_pawn_list, PausedBishop.white_bishop_list, - PausedKnight.white_knight_list, PausedRook.white_rook_list, - PausedQueen.white_queen_list, PausedKing.white_king_list, - PausedPawn.black_pawn_list, PausedBishop.black_bishop_list, - PausedKnight.black_knight_list, PausedRook.black_rook_list, - PausedQueen.black_queen_list, PausedKing.black_king_list] + return [ReplayedPawn.white_pawn_list, ReplayedBishop.white_bishop_list, + ReplayedKnight.white_knight_list, ReplayedRook.white_rook_list, + ReplayedQueen.white_queen_list, ReplayedKing.white_king_list, + ReplayedPawn.black_pawn_list, ReplayedBishop.black_bishop_list, + ReplayedKnight.black_knight_list, ReplayedRook.black_rook_list, + ReplayedQueen.black_queen_list, ReplayedKing.black_king_list] def white_pieces(): - return [PausedPawn.white_pawn_list, PausedBishop.white_bishop_list, - PausedKnight.white_knight_list, PausedRook.white_rook_list, - PausedQueen.white_queen_list, PausedKing.white_king_list] + return [ReplayedPawn.white_pawn_list, ReplayedBishop.white_bishop_list, + ReplayedKnight.white_knight_list, ReplayedRook.white_rook_list, + ReplayedQueen.white_queen_list, ReplayedKing.white_king_list] def black_pieces(): - return [PausedPawn.black_pawn_list, PausedBishop.black_bishop_list, - PausedKnight.black_knight_list, PausedRook.black_rook_list, - PausedQueen.black_queen_list, PausedKing.black_king_list] + return [ReplayedPawn.black_pawn_list, ReplayedBishop.black_bishop_list, + ReplayedKnight.black_knight_list, ReplayedRook.black_rook_list, + ReplayedQueen.black_queen_list, ReplayedKing.black_king_list] -class PausedPawn(pygame.sprite.Sprite): +class ReplayedPawn(pygame.sprite.Sprite): white_pawn_list = [] black_pawn_list = [] def __init__(self, color, coordinate_history, coord=None): @@ -51,12 +51,12 @@ def __init__(self, color, coordinate_history, coord=None): self.color = color if self.color == "white": self.image = IMAGES["SPR_WHITE_PAWN"] - PAUSED_SPRITES.add(self) - PausedPawn.white_pawn_list.append(self) + REPLAYED_SPRITES.add(self) + ReplayedPawn.white_pawn_list.append(self) elif self.color == "black": self.image = IMAGES["SPR_BLACK_PAWN"] - PAUSED_SPRITES.add(self) - PausedPawn.black_pawn_list.append(self) + REPLAYED_SPRITES.add(self) + ReplayedPawn.black_pawn_list.append(self) self.coordinate = coord self.coordinate_history = coordinate_history self.rect = self.image.get_rect() @@ -69,9 +69,9 @@ def update(self): self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft def destroy(self): if self.color == "white": - PausedPawn.white_pawn_list.remove(self) + ReplayedPawn.white_pawn_list.remove(self) elif self.color == "black": - PausedPawn.black_pawn_list.remove(self) + ReplayedPawn.black_pawn_list.remove(self) self.kill() def prior_move_update(self): if self.taken_off_board != True: @@ -86,7 +86,7 @@ def prior_move_update(self): else: self.image = IMAGES["SPR_BLACK_PAWN"] -class PausedBishop(pygame.sprite.Sprite): +class ReplayedBishop(pygame.sprite.Sprite): white_bishop_list = [] black_bishop_list = [] def __init__(self, color, coordinate_history, coord=None): @@ -94,12 +94,12 @@ def __init__(self, color, coordinate_history, coord=None): self.color = color if self.color == "white": self.image = IMAGES["SPR_WHITE_BISHOP"] - PAUSED_SPRITES.add(self) - PausedBishop.white_bishop_list.append(self) + REPLAYED_SPRITES.add(self) + ReplayedBishop.white_bishop_list.append(self) elif self.color == "black": self.image = IMAGES["SPR_BLACK_BISHOP"] - PAUSED_SPRITES.add(self) - PausedBishop.black_bishop_list.append(self) + REPLAYED_SPRITES.add(self) + ReplayedBishop.black_bishop_list.append(self) self.coordinate = coord self.coordinate_history = coordinate_history self.rect = self.image.get_rect() @@ -112,9 +112,9 @@ def update(self): self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft def destroy(self): if self.color == "white": - PausedBishop.white_bishop_list.remove(self) + ReplayedBishop.white_bishop_list.remove(self) elif self.color == "black": - PausedBishop.black_bishop_list.remove(self) + ReplayedBishop.black_bishop_list.remove(self) self.kill() def prior_move_update(self): if self.taken_off_board != True: @@ -129,7 +129,7 @@ def prior_move_update(self): else: self.image = IMAGES["SPR_BLACK_BISHOP"] -class PausedKnight(pygame.sprite.Sprite): +class ReplayedKnight(pygame.sprite.Sprite): white_knight_list = [] black_knight_list = [] def __init__(self, color, coordinate_history, coord=None): @@ -137,12 +137,12 @@ def __init__(self, color, coordinate_history, coord=None): self.color = color if self.color == "white": self.image = IMAGES["SPR_WHITE_KNIGHT"] - PAUSED_SPRITES.add(self) - PausedKnight.white_knight_list.append(self) + REPLAYED_SPRITES.add(self) + ReplayedKnight.white_knight_list.append(self) elif self.color == "black": self.image = IMAGES["SPR_BLACK_KNIGHT"] - PAUSED_SPRITES.add(self) - PausedKnight.black_knight_list.append(self) + REPLAYED_SPRITES.add(self) + ReplayedKnight.black_knight_list.append(self) self.coordinate = coord self.coordinate_history = coordinate_history self.rect = self.image.get_rect() @@ -155,9 +155,9 @@ def update(self): self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft def destroy(self): if self.color == "white": - PausedKnight.white_knight_list.remove(self) + ReplayedKnight.white_knight_list.remove(self) elif self.color == "black": - PausedKnight.black_knight_list.remove(self) + ReplayedKnight.black_knight_list.remove(self) self.kill() def prior_move_update(self): if self.taken_off_board != True: @@ -172,7 +172,7 @@ def prior_move_update(self): else: self.image = IMAGES["SPR_BLACK_KNIGHT"] -class PausedRook(pygame.sprite.Sprite): +class ReplayedRook(pygame.sprite.Sprite): white_rook_list = [] black_rook_list = [] def __init__(self, color, coordinate_history, coord=None): @@ -180,12 +180,12 @@ def __init__(self, color, coordinate_history, coord=None): self.color = color if self.color == "white": self.image = IMAGES["SPR_WHITE_ROOK"] - PAUSED_SPRITES.add(self) - PausedRook.white_rook_list.append(self) + REPLAYED_SPRITES.add(self) + ReplayedRook.white_rook_list.append(self) elif self.color == "black": self.image = IMAGES["SPR_BLACK_ROOK"] - PAUSED_SPRITES.add(self) - PausedRook.black_rook_list.append(self) + REPLAYED_SPRITES.add(self) + ReplayedRook.black_rook_list.append(self) self.coordinate = coord self.coordinate_history = coordinate_history self.rect = self.image.get_rect() @@ -198,9 +198,9 @@ def update(self): self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft def destroy(self): if self.color == "white": - PausedRook.white_rook_list.remove(self) + ReplayedRook.white_rook_list.remove(self) elif self.color == "black": - PausedRook.black_rook_list.remove(self) + ReplayedRook.black_rook_list.remove(self) self.kill() def prior_move_update(self): if self.taken_off_board != True: @@ -215,7 +215,7 @@ def prior_move_update(self): else: self.image = IMAGES["SPR_BLACK_ROOK"] -class PausedQueen(pygame.sprite.Sprite): +class ReplayedQueen(pygame.sprite.Sprite): white_queen_list = [] black_queen_list = [] def __init__(self, color, coordinate_history, coord=None): @@ -223,12 +223,12 @@ def __init__(self, color, coordinate_history, coord=None): self.color = color if self.color == "white": self.image = IMAGES["SPR_WHITE_QUEEN"] - PAUSED_SPRITES.add(self) - PausedQueen.white_queen_list.append(self) + REPLAYED_SPRITES.add(self) + ReplayedQueen.white_queen_list.append(self) elif self.color == "black": self.image = IMAGES["SPR_BLACK_QUEEN"] - PAUSED_SPRITES.add(self) - PausedQueen.black_queen_list.append(self) + REPLAYED_SPRITES.add(self) + ReplayedQueen.black_queen_list.append(self) self.coordinate = coord self.coordinate_history = coordinate_history self.rect = self.image.get_rect() @@ -241,9 +241,9 @@ def update(self): self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft def destroy(self): if self.color == "white": - PausedQueen.white_queen_list.remove(self) + ReplayedQueen.white_queen_list.remove(self) elif self.color == "black": - PausedQueen.black_queen_list.remove(self) + ReplayedQueen.black_queen_list.remove(self) self.kill() def prior_move_update(self): if self.taken_off_board != True: @@ -258,7 +258,7 @@ def prior_move_update(self): else: self.image = IMAGES["SPR_BLACK_QUEEN"] -class PausedKing(pygame.sprite.Sprite): +class ReplayedKing(pygame.sprite.Sprite): white_king_list = [] black_king_list = [] def __init__(self, color, coordinate_history, coord=None): @@ -266,12 +266,12 @@ def __init__(self, color, coordinate_history, coord=None): self.color = color if self.color == "white": self.image = IMAGES["SPR_WHITE_KING"] - PAUSED_SPRITES.add(self) - PausedKing.white_king_list.append(self) + REPLAYED_SPRITES.add(self) + ReplayedKing.white_king_list.append(self) elif self.color == "black": self.image = IMAGES["SPR_BLACK_KING"] - PAUSED_SPRITES.add(self) - PausedKing.black_king_list.append(self) + REPLAYED_SPRITES.add(self) + ReplayedKing.black_king_list.append(self) self.coordinate = coord self.coordinate_history = coordinate_history self.rect = self.image.get_rect() @@ -284,9 +284,9 @@ def update(self): self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft def destroy(self): if self.color == "white": - PausedKing.white_king_list.remove(self) + ReplayedKing.white_king_list.remove(self) elif self.color == "black": - PausedKing.black_king_list.remove(self) + ReplayedKing.black_king_list.remove(self) self.kill() def prior_move_update(self): if self.taken_off_board != True: From b52c855ec4ee4e324aa51064b98cb222404c6674 Mon Sep 17 00:00:00 2001 From: Brad Wyatt Date: Sun, 9 May 2021 16:33:05 -0700 Subject: [PATCH 14/16] Replay when a queen is promoted now works --- Chess.py | 34 ++++++++++++++++- replayed_objects.py | 90 ++++++++++++++++++++++++++++++--------------- 2 files changed, 93 insertions(+), 31 deletions(-) diff --git a/Chess.py b/Chess.py index e23df0d..f76225a 100644 --- a/Chess.py +++ b/Chess.py @@ -643,7 +643,11 @@ def play_to_replayed(play_list, class_obj, color): # Play pieces spawn where their placed piece correspondents are located for play_obj in play_list: if play_obj.coordinate is not None: - class_obj(color, play_obj.coordinate_history, play_obj.coordinate) + class_obj(color, play_obj.coordinate_history, coord=play_obj.coordinate) + elif play_obj.coordinate is None: + class_obj(color, play_obj.coordinate_history, + captured_move_number_and_coordinate=play_obj.captured_move_number_and_coordinate, + out_of_bounds_x_y=play_obj.rect.topleft) def rewind_moves(): list_of_moves_backwards = Switch_Modes_Controller.list_of_moves_backwards(Move_Tracker.df_prior_moves)[:-1] # list_of_moves_backwards list is ordered in descending order to the selected move @@ -665,6 +669,34 @@ def rewind_moves(): Grid_Controller.piece_on_grid('d1').coordinate = 'a1' elif replayed_obj.color == "black": Grid_Controller.piece_on_grid('d8').coordinate = 'a8' + if "x" in ast.literal_eval(move_dict[piece_history])['move_notation']: + if replayed_obj.color == "white": + for piece_list in replayed_objects.Piece_Lists_Shortcut.black_pieces(): + for piece in piece_list: + if piece.captured_move_number_and_coordinate: + if ast.literal_eval(move_dict[piece_history])['after'] == piece.captured_move_number_and_coordinate['coordinate'] \ + and piece.captured_move_number_and_coordinate['move_number'] == piece_history: + piece.coordinate = ast.literal_eval(move_dict[piece_history])['after'] + elif replayed_obj.color == "black": + for piece_list in replayed_objects.Piece_Lists_Shortcut.white_pieces(): + for piece in piece_list: + if piece.captured_move_number_and_coordinate: + if ast.literal_eval(move_dict[piece_history])['after'] == piece.captured_move_number_and_coordinate['coordinate'] \ + and piece.captured_move_number_and_coordinate['move_number'] == piece_history: + piece.coordinate = ast.literal_eval(move_dict[piece_history])['after'] + if "=Q" in ast.literal_eval(move_dict[piece_history])['move_notation']: + if replayed_obj.color == "white": + for piece_list in replayed_objects.Piece_Lists_Shortcut.white_pieces(): + for piece in replayed_objects.ReplayedQueen.white_queen_list: + if piece.coordinate == eval(Move_Tracker.df_prior_moves.loc[piece_history, "white_move"])['after']: + piece.kill() + replayed_objects.ReplayedQueen.white_queen_list.remove(piece) + elif replayed_obj.color == "black": + for piece_list in replayed_objects.Piece_Lists_Shortcut.black_pieces(): + for piece in replayed_objects.ReplayedQueen.black_queen_list: + if piece.coordinate == eval(Move_Tracker.df_prior_moves.loc[piece_history, "black_move"])['after']: + piece.kill() + replayed_objects.ReplayedQueen.black_queen_list.remove(piece) prior_move_grid_and_piece_highlight_dict = Switch_Modes_Controller.list_of_moves_backwards(Move_Tracker.df_prior_moves)[-1] old_grid_coordinate_before = ast.literal_eval(list(prior_move_grid_and_piece_highlight_dict.values())[0])['before'] old_grid_coordinate_after = ast.literal_eval(list(prior_move_grid_and_piece_highlight_dict.values())[0])['after'] diff --git a/replayed_objects.py b/replayed_objects.py index 3e5147c..d9ca6df 100644 --- a/replayed_objects.py +++ b/replayed_objects.py @@ -46,7 +46,7 @@ def black_pieces(): class ReplayedPawn(pygame.sprite.Sprite): white_pawn_list = [] black_pawn_list = [] - def __init__(self, color, coordinate_history, coord=None): + def __init__(self, color, coordinate_history, coord=None, captured_move_number_and_coordinate=None, out_of_bounds_x_y=None): pygame.sprite.Sprite.__init__(self) self.color = color if self.color == "white": @@ -60,13 +60,18 @@ def __init__(self, color, coordinate_history, coord=None): self.coordinate = coord self.coordinate_history = coordinate_history self.rect = self.image.get_rect() - for grid in board.Grid.grid_list: - if grid.coordinate == self.coordinate: - self.rect.topleft = grid.rect.topleft + if self.coordinate is not None: + for grid in board.Grid.grid_list: + if grid.coordinate == self.coordinate: + self.rect.topleft = grid.rect.topleft + else: + self.rect.topleft = out_of_bounds_x_y self.prior_move_color = False self.taken_off_board = False + self.captured_move_number_and_coordinate = captured_move_number_and_coordinate def update(self): - self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft + if self.coordinate is not None: + self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft def destroy(self): if self.color == "white": ReplayedPawn.white_pawn_list.remove(self) @@ -89,7 +94,7 @@ def prior_move_update(self): class ReplayedBishop(pygame.sprite.Sprite): white_bishop_list = [] black_bishop_list = [] - def __init__(self, color, coordinate_history, coord=None): + def __init__(self, color, coordinate_history, coord=None, captured_move_number_and_coordinate=None, out_of_bounds_x_y=None): pygame.sprite.Sprite.__init__(self) self.color = color if self.color == "white": @@ -103,13 +108,18 @@ def __init__(self, color, coordinate_history, coord=None): self.coordinate = coord self.coordinate_history = coordinate_history self.rect = self.image.get_rect() - for grid in board.Grid.grid_list: - if grid.coordinate == self.coordinate: - self.rect.topleft = grid.rect.topleft + if self.coordinate is not None: + for grid in board.Grid.grid_list: + if grid.coordinate == self.coordinate: + self.rect.topleft = grid.rect.topleft + else: + self.rect.topleft = out_of_bounds_x_y self.prior_move_color = False self.taken_off_board = False + self.captured_move_number_and_coordinate = captured_move_number_and_coordinate def update(self): - self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft + if self.coordinate is not None: + self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft def destroy(self): if self.color == "white": ReplayedBishop.white_bishop_list.remove(self) @@ -132,7 +142,7 @@ def prior_move_update(self): class ReplayedKnight(pygame.sprite.Sprite): white_knight_list = [] black_knight_list = [] - def __init__(self, color, coordinate_history, coord=None): + def __init__(self, color, coordinate_history, coord=None, captured_move_number_and_coordinate=None, out_of_bounds_x_y=None): pygame.sprite.Sprite.__init__(self) self.color = color if self.color == "white": @@ -146,13 +156,18 @@ def __init__(self, color, coordinate_history, coord=None): self.coordinate = coord self.coordinate_history = coordinate_history self.rect = self.image.get_rect() - for grid in board.Grid.grid_list: - if grid.coordinate == self.coordinate: - self.rect.topleft = grid.rect.topleft + if self.coordinate is not None: + for grid in board.Grid.grid_list: + if grid.coordinate == self.coordinate: + self.rect.topleft = grid.rect.topleft + else: + self.rect.topleft = out_of_bounds_x_y self.prior_move_color = False self.taken_off_board = False + self.captured_move_number_and_coordinate = captured_move_number_and_coordinate def update(self): - self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft + if self.coordinate is not None: + self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft def destroy(self): if self.color == "white": ReplayedKnight.white_knight_list.remove(self) @@ -175,7 +190,7 @@ def prior_move_update(self): class ReplayedRook(pygame.sprite.Sprite): white_rook_list = [] black_rook_list = [] - def __init__(self, color, coordinate_history, coord=None): + def __init__(self, color, coordinate_history, coord=None, captured_move_number_and_coordinate=None, out_of_bounds_x_y=None): pygame.sprite.Sprite.__init__(self) self.color = color if self.color == "white": @@ -189,13 +204,18 @@ def __init__(self, color, coordinate_history, coord=None): self.coordinate = coord self.coordinate_history = coordinate_history self.rect = self.image.get_rect() - for grid in board.Grid.grid_list: - if grid.coordinate == self.coordinate: - self.rect.topleft = grid.rect.topleft + if self.coordinate is not None: + for grid in board.Grid.grid_list: + if grid.coordinate == self.coordinate: + self.rect.topleft = grid.rect.topleft + else: + self.rect.topleft = out_of_bounds_x_y self.prior_move_color = False self.taken_off_board = False + self.captured_move_number_and_coordinate = captured_move_number_and_coordinate def update(self): - self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft + if self.coordinate is not None: + self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft def destroy(self): if self.color == "white": ReplayedRook.white_rook_list.remove(self) @@ -218,7 +238,7 @@ def prior_move_update(self): class ReplayedQueen(pygame.sprite.Sprite): white_queen_list = [] black_queen_list = [] - def __init__(self, color, coordinate_history, coord=None): + def __init__(self, color, coordinate_history, coord=None, captured_move_number_and_coordinate=None, out_of_bounds_x_y=None): pygame.sprite.Sprite.__init__(self) self.color = color if self.color == "white": @@ -232,13 +252,18 @@ def __init__(self, color, coordinate_history, coord=None): self.coordinate = coord self.coordinate_history = coordinate_history self.rect = self.image.get_rect() - for grid in board.Grid.grid_list: - if grid.coordinate == self.coordinate: - self.rect.topleft = grid.rect.topleft + if self.coordinate is not None: + for grid in board.Grid.grid_list: + if grid.coordinate == self.coordinate: + self.rect.topleft = grid.rect.topleft + else: + self.rect.topleft = out_of_bounds_x_y self.prior_move_color = False self.taken_off_board = False + self.captured_move_number_and_coordinate = captured_move_number_and_coordinate def update(self): - self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft + if self.coordinate is not None: + self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft def destroy(self): if self.color == "white": ReplayedQueen.white_queen_list.remove(self) @@ -261,7 +286,7 @@ def prior_move_update(self): class ReplayedKing(pygame.sprite.Sprite): white_king_list = [] black_king_list = [] - def __init__(self, color, coordinate_history, coord=None): + def __init__(self, color, coordinate_history, coord=None, captured_move_number_and_coordinate=None, out_of_bounds_x_y=None): pygame.sprite.Sprite.__init__(self) self.color = color if self.color == "white": @@ -275,13 +300,18 @@ def __init__(self, color, coordinate_history, coord=None): self.coordinate = coord self.coordinate_history = coordinate_history self.rect = self.image.get_rect() - for grid in board.Grid.grid_list: - if grid.coordinate == self.coordinate: - self.rect.topleft = grid.rect.topleft + if self.coordinate is not None: + for grid in board.Grid.grid_list: + if grid.coordinate == self.coordinate: + self.rect.topleft = grid.rect.topleft + else: + self.rect.topleft = out_of_bounds_x_y self.prior_move_color = False self.taken_off_board = False + self.captured_move_number_and_coordinate = captured_move_number_and_coordinate def update(self): - self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft + if self.coordinate is not None: + self.rect.topleft = board.Grid.grid_dict[self.coordinate].rect.topleft def destroy(self): if self.color == "white": ReplayedKing.white_king_list.remove(self) From d41e7e8cba4dc911ed706891e59315a2b8b0774b Mon Sep 17 00:00:00 2001 From: Brad Wyatt Date: Sun, 9 May 2021 16:57:36 -0700 Subject: [PATCH 15/16] En Passant replay --- Chess.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Chess.py b/Chess.py index f76225a..a94fdf4 100644 --- a/Chess.py +++ b/Chess.py @@ -674,15 +674,21 @@ def rewind_moves(): for piece_list in replayed_objects.Piece_Lists_Shortcut.black_pieces(): for piece in piece_list: if piece.captured_move_number_and_coordinate: - if ast.literal_eval(move_dict[piece_history])['after'] == piece.captured_move_number_and_coordinate['coordinate'] \ - and piece.captured_move_number_and_coordinate['move_number'] == piece_history: + if piece.captured_move_number_and_coordinate['move_number'] == piece_history: + if 'ep_grid_after_coord' in piece.captured_move_number_and_coordinate: + # En Passant exception + piece.coordinate = piece.captured_move_number_and_coordinate['ep_grid_after_coord'][0] + str(int(piece.captured_move_number_and_coordinate['ep_grid_after_coord'][1])-1) + else: piece.coordinate = ast.literal_eval(move_dict[piece_history])['after'] elif replayed_obj.color == "black": for piece_list in replayed_objects.Piece_Lists_Shortcut.white_pieces(): for piece in piece_list: if piece.captured_move_number_and_coordinate: - if ast.literal_eval(move_dict[piece_history])['after'] == piece.captured_move_number_and_coordinate['coordinate'] \ - and piece.captured_move_number_and_coordinate['move_number'] == piece_history: + if piece.captured_move_number_and_coordinate['move_number'] == piece_history: + if 'ep_grid_after_coord' in piece.captured_move_number_and_coordinate: + # En Passant exception + piece.coordinate = piece.captured_move_number_and_coordinate['ep_grid_after_coord'][0] + str(int(piece.captured_move_number_and_coordinate['ep_grid_after_coord'][1])+1) + else: piece.coordinate = ast.literal_eval(move_dict[piece_history])['after'] if "=Q" in ast.literal_eval(move_dict[piece_history])['move_notation']: if replayed_obj.color == "white": @@ -697,6 +703,8 @@ def rewind_moves(): if piece.coordinate == eval(Move_Tracker.df_prior_moves.loc[piece_history, "black_move"])['after']: piece.kill() replayed_objects.ReplayedQueen.black_queen_list.remove(piece) + # -1 in the list refers to the highlighted move in the pane + # Retrieve the grids from the piece that we are replaying, and get the grid from the previous move to that one prior_move_grid_and_piece_highlight_dict = Switch_Modes_Controller.list_of_moves_backwards(Move_Tracker.df_prior_moves)[-1] old_grid_coordinate_before = ast.literal_eval(list(prior_move_grid_and_piece_highlight_dict.values())[0])['before'] old_grid_coordinate_after = ast.literal_eval(list(prior_move_grid_and_piece_highlight_dict.values())[0])['after'] From b1323dff35e78b4771d9a87a44ad810f5e4987e0 Mon Sep 17 00:00:00 2001 From: Brad Wyatt Date: Sun, 9 May 2021 17:30:32 -0700 Subject: [PATCH 16/16] final touches --- Chess.py | 6 +- .../list_index_out_of_range_Bc4ThenUndo.pgn | 13 +++++ play_objects.py | 56 ++++++++++--------- 3 files changed, 45 insertions(+), 30 deletions(-) create mode 100644 PGN_Sample_Games/list_index_out_of_range_Bc4ThenUndo.pgn diff --git a/Chess.py b/Chess.py index a94fdf4..98824fe 100644 --- a/Chess.py +++ b/Chess.py @@ -563,8 +563,6 @@ def prior_move_color(grid_coordinate, prior_move_piece): piece.prior_move_color = False piece.no_highlight() else: - #%% Prior move color func - #print("PIECE: " + str(prior_move_piece) + " with coordinate " + str(prior_move_piece.coordinate)) # Updating prior move sprites for replayed objects for piece_list in replayed_objects.Piece_Lists_Shortcut.all_pieces(): for piece in piece_list: @@ -711,7 +709,6 @@ def rewind_moves(): old_piece = Grid_Controller.piece_on_grid(old_grid_coordinate_after) Grid_Controller.prior_move_color(old_grid_coordinate_before, old_piece) def replayed_game(replayed, game_controller): - #%% Working on currently Switch_Modes_Controller.REPLAYED = replayed if Switch_Modes_Controller.REPLAYED == True: replayed_objects.remove_all_replayed() @@ -1122,11 +1119,12 @@ def undo_move(game_controller): Move_Tracker.undo_move_in_dfs("black") game_controller.switch_turn("black") Grid_Controller.update_prior_move_color("black") + print("TESTING123") + #%% Trying to resolve issue where we undo move into check Move_Controller.game_status_check(game_controller) log.info("Back to (" + str(len(Move_Tracker.df_moves)) + ".) " + "Black undo turn " + str(piece_to_undo) + " going back to " + str(piece_to_undo.coordinate)) elif game_controller.WHOSETURN == "black": for piece_to_undo in pieces_to_undo: - #print("PIECES TO UNDO " + str(piece_to_undo) + " LIST " + str(piece_to_undo.coordinate_history)) piece_to_undo.coordinate = piece_to_undo.coordinate_history[Move_Tracker.move_counter()]['before'] piece_to_undo.rect.topleft = board.Grid.grid_dict[piece_to_undo.coordinate].rect.topleft del piece_to_undo.coordinate_history[Move_Tracker.move_counter()] diff --git a/PGN_Sample_Games/list_index_out_of_range_Bc4ThenUndo.pgn b/PGN_Sample_Games/list_index_out_of_range_Bc4ThenUndo.pgn new file mode 100644 index 0000000..c1e4398 --- /dev/null +++ b/PGN_Sample_Games/list_index_out_of_range_Bc4ThenUndo.pgn @@ -0,0 +1,13 @@ +[Event ""] +[Site ""] +[Date ""] +[Round ""] +[White ""] +[Black ""] +[Result "*"] +[ECO ""] +[TimeControl "0"] +[WhiteElo ""] +[BlackElo ""] + +1. e4 e5 2. Nf3 Nf6 3. Nxe5 Nxe4 4. Qe2 Nf6 5. Nc6 Qe7 6. Nxe7 Bxe7 7. Nc3 h6 8. d4 Nh5 9. Bf4 O-O 10. h3 Nxf4 11. Qe3 Nd5 12. Nxd5 Re8 13. O-O-O Bf6 14. Qxe8 Kh7 15. Qe4 Kh8 * \ No newline at end of file diff --git a/play_objects.py b/play_objects.py index e8754fe..1ca276a 100644 --- a/play_objects.py +++ b/play_objects.py @@ -6,20 +6,20 @@ class Piece_Lists_Shortcut(): def all_pieces(): - return [PlayPawn.white_pawn_list, PlayBishop.white_bishop_list, + return [PlayKing.black_king_list, PlayKing.white_king_list, + PlayPawn.white_pawn_list, PlayBishop.white_bishop_list, PlayKnight.white_knight_list, PlayRook.white_rook_list, - PlayQueen.white_queen_list, PlayKing.white_king_list, - PlayPawn.black_pawn_list, PlayBishop.black_bishop_list, - PlayKnight.black_knight_list, PlayRook.black_rook_list, - PlayQueen.black_queen_list, PlayKing.black_king_list] + PlayQueen.white_queen_list, PlayPawn.black_pawn_list, + PlayBishop.black_bishop_list, PlayKnight.black_knight_list, + PlayRook.black_rook_list, PlayQueen.black_queen_list] def white_pieces(): - return [PlayPawn.white_pawn_list, PlayBishop.white_bishop_list, - PlayKnight.white_knight_list, PlayRook.white_rook_list, - PlayQueen.white_queen_list, PlayKing.white_king_list] + return [PlayKing.white_king_list, PlayPawn.white_pawn_list, + PlayBishop.white_bishop_list, PlayKnight.white_knight_list, + PlayRook.white_rook_list, PlayQueen.white_queen_list] def black_pieces(): - return [PlayPawn.black_pawn_list, PlayBishop.black_bishop_list, - PlayKnight.black_knight_list, PlayRook.black_rook_list, - PlayQueen.black_queen_list, PlayKing.black_king_list] + return [PlayKing.black_king_list, PlayPawn.black_pawn_list, + PlayBishop.black_bishop_list, PlayKnight.black_knight_list, + PlayRook.black_rook_list, PlayQueen.black_queen_list] class ChessPiece: def __init__(self, coord, image, col): @@ -343,22 +343,26 @@ def bishop_direction_spaces_available(bishop, game_controller, x, y): grid.highlight(bishop.color, bishop.coordinate) # If current king is in check elif game_controller.color_in_check == bishop.color: + # Disable piece if it is pinned and checked from another enemy piece - if bishop.pinned == True: - bishop.disable = True - return - # Block path of enemy bishop, rook, or queen - # You cannot have multiple spaces in one direction when blocking so return - elif grid.coordinate in game_controller.check_attacking_coordinates[:-1] \ - and (game_controller.attacker_piece == "bishop" or game_controller.attacker_piece == "rook" \ - or game_controller.attacker_piece == "queen"): - grid.highlight(bishop.color, bishop.coordinate) - return - # The only grid available is the attacker piece when pawn or knight - elif grid.coordinate == game_controller.check_attacking_coordinates[0] \ - and (game_controller.attacker_piece == "pawn" or game_controller.attacker_piece == "knight"): - grid.highlight(bishop.color, bishop.coordinate) - return + try: + if bishop.pinned == True: + bishop.disable = True + return + # Block path of enemy bishop, rook, or queen + # You cannot have multiple spaces in one direction when blocking so return + elif grid.coordinate in game_controller.check_attacking_coordinates[:-1] \ + and (game_controller.attacker_piece == "bishop" or game_controller.attacker_piece == "rook" \ + or game_controller.attacker_piece == "queen"): + grid.highlight(bishop.color, bishop.coordinate) + return + # The only grid available is the attacker piece when pawn or knight + elif grid.coordinate == game_controller.check_attacking_coordinates[0] \ + and (game_controller.attacker_piece == "pawn" or game_controller.attacker_piece == "knight"): + grid.highlight(bishop.color, bishop.coordinate) + return + except: + print("BISHOP COORD " + str(bishop.coordinate)) # If pinned and the grid is within the attacking coordinates restraint # Includes grid.coordinate != self.coordinate so that staying at same coordinate doesn't count as move elif(bishop.pinned == True and grid.coordinate in bishop.pin_attacking_coordinates \