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,
"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