Manage error of players

This commit is contained in:
Thomas Forgione 2019-04-04 11:38:31 +02:00
parent 3535967092
commit 8f7d3e9714
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
1 changed files with 26 additions and 3 deletions

View File

@ -92,6 +92,9 @@ class Game:
Then, all the players are updated and if a player lands on a square
that is already occupied or that is outside of the map, the players
dies.
If there is an error during the evaluation of a player strategy, the
player will automatically lose and this function will return False.
"""
map_clone = self.map()
@ -102,7 +105,16 @@ class Game:
# Play next move
for id, pp in enumerate(self.pps):
(pp.position, pp.player.direction) = pp.player.next_position_and_direction(pp.position, id + 1, self.map())
try:
(pp.position, pp.player.direction) = pp.player.next_position_and_direction(pp.position, id + 1, self.map())
except:
# An error occured during the evaluation of pp.player strategy
if id == 1:
self.winner = 2
elif id == 2:
self.winner = 1
return False
# Update history with newly played move
self.history[-1].player_one_direction = self.pps[0].player.direction
@ -118,7 +130,15 @@ class Game:
break
for pp in self.pps:
pp.player.manage_event(event)
try:
pp.player.manage_event(event)
except:
# An error occured during the evaluation of pp.player strategy
if id == 1:
self.winner = 2
elif id == 2:
self.winner = 1
return False
for (id, pp) in enumerate(self.pps):
@ -140,6 +160,8 @@ class Game:
# Append to history
self.history.append(HistoryElement(map_clone, None, None))
return True
def main_loop(self, window = None):
"""
Loops until the game is finished
@ -157,7 +179,8 @@ class Game:
if window:
sleep(0.1)
self.next_frame(window)
if not self.next_frame(window):
break
for pp in self.pps:
if pp.alive: