Skip to content

Commit

Permalink
NF: Try to reduce the copying of mutable objects in game_master.
Browse files Browse the repository at this point in the history
When serialising, these will be copied anyway.
  • Loading branch information
Debilski committed Jun 14, 2012
1 parent c9d0d32 commit e1c1f33
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions pelita/game_master.py
Expand Up @@ -2,7 +2,6 @@

""" The controller """

import copy
import random
import sys
import time
Expand Down Expand Up @@ -117,24 +116,24 @@ def update_viewers(self):
""" Call the 'observe' method on all registered viewers.
"""
for viewer in self.viewers:
viewer.observe(self.universe.copy(),
copy.deepcopy(self.game_state))
viewer.observe(self.universe,
self.game_state)

def set_initial(self):
""" This method needs to be called before a game is started.
It notifies the PlayerTeams and the Viewers of the initial
universes and tells the PlayerTeams what team_id they have.
"""
for team_id, team in enumerate(self.player_teams):
team.set_initial(team_id, self.universe.copy())
team.set_initial(team_id, self.universe)

if len(self.player_teams) != len(self.universe.teams):
raise IndexError(
"Universe uses %i teams, but only %i are registered."
% (len(self.player_teams), len(self.universe.teams)))

for viewer in self.viewers:
viewer.set_initial(self.universe.copy())
viewer.set_initial(self.universe)

# TODO the game winning detection should be refactored
def play(self):
Expand Down Expand Up @@ -229,12 +228,16 @@ def _play_bot(self, bot):

player_team = self.player_teams[bot.team_index]
try:
universe_copy = self.universe.copy()
if self.noiser:
universe_copy = self.noiser.uniform_noise(universe_copy, bot.index)
# need to copy before we apply noise to the universe
universe = self.noiser.uniform_noise(self.universe.copy(), bot.index)
else:
universe = self.universe

team_time_begin = time.time()
move = player_team.get_move(bot.index, universe_copy)

move = player_team.get_move(bot.index, universe)

team_time_needed = time.time() - team_time_begin
self.game_state["team_time"][bot.team_index] += team_time_needed

Expand Down

0 comments on commit e1c1f33

Please sign in to comment.