コード例 #1
0
ファイル: world.py プロジェクト: DanTulovsky/game
  def _NeighborCount(self, square):
    """Returns the count and color of a square's neighbors.

    Args:
      square: (Square) Object

    Returns:
      neighbors: (dict) {
                          Color.white: N,  # total
                          "friends": H,
                          "enemies": J,
                          "total_neighbors": L,  # non-white neighbors
                          "influence": Team  # the team with the most influence
                                             # simply with the most squares
                                             # surrounding this one
                        }
    """
    neighbors = {"total_neighbors": 0, "friends": 0, "enemies": 0}


    if square.position == (46, 14):
      pass
    # initialize all colors to 0
    for color in Colors.all():
      neighbors[color] = 0

    neighbor_team_count = {}

    for pos in (world_rules.POSITIONS):
      try:
        neighbor_color, neighbor_team = self._NeighborColor(square, pos)
      except (OutsideWorld, NoNeighbor):
        continue

      neighbors[neighbor_color] += 1
      if neighbor_color != Colors.white:
        neighbors["total_neighbors"] += 1

        neighbor_team_count.setdefault(neighbor_team, 0)
        neighbor_team_count[neighbor_team] += 1

        if neighbor_team == square.team:
          neighbors["friends"] += 1
        else:
          neighbors["enemies"] += 1

    influence = None
    max = 0
    for team, count in neighbor_team_count.iteritems():
      if count > max:
        max = count
        influence = team


      neighbors["influence"] = influence

    return neighbors