Skip to content

Commit

Permalink
Use positive logic when calculating score
Browse files Browse the repository at this point in the history
Signed-off-by: yzamir <kobi.zamir@gmail.com>
  • Loading branch information
yaacov authored and nirs committed Aug 27, 2023
1 parent 3207b26 commit 91606ad
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
1 change: 1 addition & 0 deletions rose/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

score_move_forward = 10
score_move_backward = -10
score_pickup = 10
score_jump = 5
score_brake = 4

Expand Down
59 changes: 39 additions & 20 deletions rose/server/score.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,49 +27,65 @@ def process(players, track):
if player.x < config.matrix_width - 1:
player.x += 1

# Now handle obstacles, preferring players in their own lane.
# Proccess the players by order, first the ones in lane and then
# the ones out of lane, this ensure the car in lane will have
# priority when picking pinguins and in case of collisions.

sorted_players = sorted(six.itervalues(players),
key=lambda p: 0 if p.in_lane() else 1)
positions = set()

# Now handle obstacles, preferring players in their own lane.

for player in sorted_players:
player.score += config.score_move_forward
obstacle = track.get(player.x, player.y)
if obstacle == obstacles.CRACK:
if player.action != actions.JUMP:
track.clear(player.x, player.y)
player.y += 1
player.score += config.score_move_backward * 2
else:
player.score += config.score_jump

if obstacle == obstacles.NONE:
# Move forward, leaving the obstacle on the track.
player.score += config.score_move_forward

elif obstacle in (obstacles.TRASH,
obstacles.BIKE,
obstacles.BARRIER):
if player.action not in (actions.LEFT, actions.RIGHT):
# Move back consuming the obstacle.
track.clear(player.x, player.y)
player.y += 1
player.score += config.score_move_backward

elif obstacle == obstacles.CRACK:
if player.action == actions.JUMP:
# Move forward leaving the obstacle on the track
player.score += config.score_move_forward + config.score_jump
else:
# Move back consuming the obstacle.
track.clear(player.x, player.y)
player.y += 1
player.score += config.score_move_backward * 2
player.score += config.score_move_backward

elif obstacle == obstacles.WATER:
if player.action != actions.BRAKE:
if player.action == actions.BRAKE:
# Move forward leaving the obstacle on the track
player.score += config.score_move_forward + config.score_brake
else:
# Move back consuming the obstacle.
track.clear(player.x, player.y)
player.y += 1
player.score += config.score_move_backward * 2
else:
player.score += config.score_brake
player.score += config.score_move_backward

elif obstacle == obstacles.PENGUIN:
if player.action == actions.PICKUP:
# Move forward and collect an aquatic bird
track.clear(player.x, player.y)
player.score += config.score_move_forward + config.score_pickup
else:
# Move forward leaving the obstacle on the track
player.score += config.score_move_forward

# Here we can end the game when player gets out of
# the track bounds. For now, just keep the player at the same
# location.
player.y = min(config.matrix_height - 1, max(2, player.y))

# Finally forget action
player.action = actions.NONE

# Fix up collisions

if (player.x, player.y) in positions:
Expand All @@ -83,9 +99,12 @@ def process(players, track):
elif player.x < config.matrix_width - 1:
player.x += 1

# Finally forget action
player.action = actions.NONE

positions.add((player.x, player.y))

log.info('process_actions: name=%s lane=%d pos=%d,%d score=%d '
'response_time=%0.6f',
player.name, player.lane, player.x, player.y, player.score,
player.response_time)

positions.add((player.x, player.y))

0 comments on commit 91606ad

Please sign in to comment.