You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python躲避球游戏模拟程序结果异常,请求排查逻辑错误

Python躲避球游戏模拟程序结果异常,请求排查逻辑错误

我正在开发一个名为Dodgeball的游戏模拟程序,游戏规则如下:

  • 每个玩家站在场地的整数坐标位置
  • 游戏开始时,一名玩家从指定方向(N、NE、E、SE、S、SW、W、NW,顺时针顺序)接到球
  • 当玩家接到球时,他们顺时针旋转(每次45°),直到找到另一名恰好处于该方向的玩家,然后将球扔出并退出游戏
  • 如果同一方向有多名玩家,球总是传给最近的那一个
  • 当玩家无法将球传给其他人时,游戏结束

我需要计算:

  • 游戏结束前的传球次数
  • 最后一个接到球的玩家

我用角度和距离计算写了Python程序,但结果不正确。以下是我的代码:

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[i]
        # 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()

我用以下输入测试程序:

[
  {
    "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
  }
]

预期输出:

4 8  
5 6  

但实际得到的输出是:

1 8  
1 5

看起来我的代码没有正确找到每一步的下一个玩家。

可能的原因

  • 角度计算或近似问题:我用math.atan2()计算角度,math.degrees()转换,但角度比较逻辑可能有问题
  • 最近玩家选择错误:我用欧氏距离的平方dir_x**2 + dir_y**2来选最近的,但方法可能不正确
  • 方向更新错误:我用(direction_index + 1) % 8更新方向索引,这是否符合游戏规则?

我的疑问

  • 我的角度计算逻辑正确吗?是否需要用不同的方式归一化角度?
  • 我选择最近玩家的方法正确吗?如何验证?
  • 有没有更可靠的方式处理玩家的旋转逻辑?

备注:内容来源于stack exchange,提问作者Ste347789

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 12:09:31