From 3d9f1aaf94b7efc978feff030044721fdd0275c5 Mon Sep 17 00:00:00 2001 From: Tibor Bizjak Date: Tue, 10 Sep 2019 21:42:55 +0200 Subject: [PATCH] Fixed performance issues caused by deepcopy --- chess.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/chess.py b/chess.py index bd42a7f..7392d06 100644 --- a/chess.py +++ b/chess.py @@ -130,7 +130,9 @@ class Game: "black" : {"queen" : True, "king" : True} } - self.history.append(deepcopy(self)) + self.history.append((deepcopy(self.can_castle), + deepcopy(self.board), + self.turn)) def __repr__(self): # Unicode board representation @@ -173,10 +175,10 @@ class Game: def timetravel(self, i): if i >= len(self.history) or i < 0: return False - new = self.history[i] - self.board = new.board - self.turn = new.turn - self.can_castle = new.can_castle + c, board, turn = self.history[i] + self.board = board + self.turn = turn + self.can_castle = c self.index = i def prev(self): @@ -253,7 +255,9 @@ class Game: self.can_castle[color]["king"] = False self.moves.append(king_move) self.turn = invert(self.turn) - self.history.append(deepcopy(self)) + self.history.append((deepcopy(self.can_castle), + deepcopy(self.board), + self.turn)) self.index += 1 return True @@ -297,7 +301,9 @@ class Game: self.board = new.board self.can_castle = new.can_castle self.turn = invert(moved.color) - self.history.append(deepcopy(self)) + self.history.append((deepcopy(self.can_castle), + deepcopy(self.board), + self.turn)) self.index += 1 return True