Fixed performance issues caused by deepcopy

master
Tibor Bizjak 2019-09-10 21:42:55 +02:00
parent d0bbc4a14a
commit 3d9f1aaf94
1 changed files with 13 additions and 7 deletions

View File

@ -130,7 +130,9 @@ class Game:
"black" : {"queen" : True, "black" : {"queen" : True,
"king" : True} "king" : True}
} }
self.history.append(deepcopy(self)) self.history.append((deepcopy(self.can_castle),
deepcopy(self.board),
self.turn))
def __repr__(self): def __repr__(self):
# Unicode board representation # Unicode board representation
@ -173,10 +175,10 @@ class Game:
def timetravel(self, i): def timetravel(self, i):
if i >= len(self.history) or i < 0: if i >= len(self.history) or i < 0:
return False return False
new = self.history[i] c, board, turn = self.history[i]
self.board = new.board self.board = board
self.turn = new.turn self.turn = turn
self.can_castle = new.can_castle self.can_castle = c
self.index = i self.index = i
def prev(self): def prev(self):
@ -253,7 +255,9 @@ class Game:
self.can_castle[color]["king"] = False self.can_castle[color]["king"] = False
self.moves.append(king_move) self.moves.append(king_move)
self.turn = invert(self.turn) 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 self.index += 1
return True return True
@ -297,7 +301,9 @@ class Game:
self.board = new.board self.board = new.board
self.can_castle = new.can_castle self.can_castle = new.can_castle
self.turn = invert(moved.color) 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 self.index += 1
return True return True