[Python] Issue with Dodgeball Game Simulation in Python

Discussão em 'Python' iniciado por Stack, Fevereiro 27, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working on a simulation problem for a game called Dodgeball. Here are the rules:

    Each player stands on a field at a specific integer coordinate. The game starts with one player receiving the ball from a given direction among: N, NE, E, SE, S, SW, W, NW (clockwise order). When a player receives the ball, they rotate clockwise (in 45° increments) until they find another player exactly in that direction. They then throw the ball and leave the game. If multiple players are in the same direction, the ball always goes to the closest one. The game ends when a player cannot pass the ball to anyone else. I need to compute:

    The number of throws before the game ends. The last player to receive the ball. I've written a Python program using angle and distance calculations to determine the next player, but the results are incorrect.Here's my code

    import json
    import math

    # Directions in clockwise order
    directions = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW']

    def get_direction_index(d):
    return directions.index(d)

    def get_next_player(current_player, players, direction_index):
    x, y = players[current_player]
    min_dist = float('inf')
    next_player = -1

    for i in range(len(players)):
    if i == current_player:
    continue
    nx, ny = players
    # Calculate the direction vector
    dir_x = nx - x
    dir_y = ny - y
    # Calculate the angle
    angle = math.atan2(dir_y, dir_x)
    # Convert to degrees and normalize
    angle_deg = math.degrees(angle)
    if angle_deg < 0:
    angle_deg += 360
    # Find the closest direction
    target_angle = direction_index * 45
    print(f"Player {i}: angle_deg = {angle_deg}, target_angle = {target_angle}, dist = {dir_x**2 + dir_y**2}")
    if abs((angle_deg - target_angle + 360) % 360 - 180) < 22.5: # Allow for a margin of error
    dist = dir_x**2 + dir_y**2
    if dist < min_dist:
    min_dist = dist
    next_player = i

    return next_player

    def simulate_game(players, start_dir, start_player):
    throws = 0
    current_player = start_player - 1
    direction_index = get_direction_index(start_dir)

    while True:
    next_player = get_next_player(current_player, players, direction_index)
    if next_player == -1:
    break
    throws += 1
    current_player = next_player
    direction_index = (direction_index + 1) % 8
    print(f"Throw {throws}: current_player = {current_player + 1}, direction_index = {direction_index}")

    return throws, current_player + 1

    def main():
    with open("input.json", "r") as f:
    data = json.load(f)

    for case in data:
    players = case["players"]
    start_dir = case["startingDirection"]
    start_player = case["startingPlayer"]
    throws, last_player = simulate_game(players, start_dir, start_player)
    print(throws, last_player)

    if __name__ == "__main__":
    main()


    When I test my program with the following input:

    [
    {
    "players": [
    [-10, -10],
    [-10, 10],
    [0, -10],
    [0, 10],
    [10, -10],
    [10, 10],
    [-9, -10],
    [-9, 0]
    ],
    "startingDirection": "NW",
    "startingPlayer": 5
    },
    {
    "players": [
    [-1000000, -1000000],
    [-1000000, 1000000],
    [0, -1000000],
    [0, 1000000],
    [1000000, -1000000],
    [1000000, 1000000],
    [-999999, -1000000],
    [-999999, 0]
    ],
    "startingDirection": "SE",
    "startingPlayer": 4
    }
    ]


    I expect the output to be

    4 8
    5 6


    But instead i get :

    1 8
    1 5


    It seems my code is not correctly finding the next player at each step.

    Possible Causes:


    • Incorrect angle calculation or approximation issues

      I'm using math.atan2() to compute angles and math.degrees() to convert them, but maybe my angle comparison logic is flawed.


    • Incorrect closest-player selection

      I'm picking the player with the smallest Euclidean distance dir_x**2 + dir_y**2, but my method might be incorrect.


    • Incorrect direction update

      I update direction_index using (direction_index + 1) % 8. Is this the correct way to rotate directions in this game?

    Questions:

    • Is my angle computation logic correct? Should I normalize angles differently?
    • Is my method for selecting the closest player correct? How can I verify it?
    • Is there a more robust way to handle player rotation?

    Continue reading...

Compartilhe esta Página