Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replay previous moves #22

Merged
merged 16 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 176 additions & 22 deletions Chess.py

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions PGN_Sample_Games/list_index_out_of_range_Bc4ThenUndo.pgn
Original file line number Diff line number Diff line change
@@ -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 *
72 changes: 36 additions & 36 deletions placed_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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()
56 changes: 30 additions & 26 deletions play_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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 \
Expand Down
Loading