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 <[email protected]>
  • Loading branch information
yaacov committed Aug 26, 2023
1 parent 4589e77 commit 61f9eaa
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 33 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
82 changes: 49 additions & 33 deletions rose/server/score.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,54 +27,64 @@ def process(players, track):
if player.x < config.matrix_width - 1:
player.x += 1

# Now handle obstacles, preferring players in their own lane.
obstacle = track.get(player.x, player.y)

sorted_players = sorted(six.itervalues(players),
key=lambda p: 0 if p.in_lane() else 1)
positions = set()
if obstacle == obstacles.NONE:
# Move forlward
player.score += config.score_move_forward

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
elif obstacle in (obstacles.TRASH,
obstacles.BIKE,
obstacles.BARRIER):
if player.action not in (actions.LEFT, actions.RIGHT):
# Move back
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 forlward
player.score += config.score_move_forward
player.score += config.score_jump
else:
# Move back
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 forlward
player.score += config.score_move_forward
player.score += config.score_brake
else:
# Move back
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 forlward
track.clear(player.x, player.y)
player.score += config.score_move_forward
player.score += config.score_pickup
else:
# Move forlward no reward
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
# Filter for players with the same x and y
all_collisions = filter(lambda p:
p.name != player.name and
p.x == player.x and
p.y == player.y, six.itervalues(players))
next_collision = next(all_collisions, None)

# Fix up collisions
# if we have a collision and we are not in our lane, try to move back
if next_collision and not player.in_lane():
log.info('player %s collision at %d,%d with %s',
player.name, player.x, player.y, next_collision.name)

if (player.x, player.y) in positions:
log.info('player %s collision at %d,%d',
player.name, player.x, player.y)
player.score += config.score_move_backward
if player.y < config.matrix_height - 1:
player.y += 1
Expand All @@ -83,9 +93,15 @@ def process(players, track):
elif player.x < config.matrix_width - 1:
player.x += 1

# 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

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 61f9eaa

Please sign in to comment.