Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if bot_initialization:
# We assume that we are in get_initial phase
turn = idx
bot_turn = None
seed = game_state['rnd'].randint(0, sys.maxsize)
else:
turn = game_state['turn']
bot_turn = game_state['turn'] // 2
seed = None
bot_position = game_state['bots'][turn]
own_team = turn % 2
enemy_team = 1 - own_team
enemy_positions = game_state['bots'][enemy_team::2]
noised_positions = noiser(walls=game_state['walls'],
bot_position=bot_position,
enemy_positions=enemy_positions,
noise_radius=game_state['noise_radius'],
sight_distance=game_state['sight_distance'],
rnd=game_state['rnd'])
team_state = {
'team_index': own_team,
'bot_positions': game_state['bots'][own_team::2],
'score': game_state['score'][own_team],
'kills': game_state['kills'][own_team::2],
'deaths': game_state['deaths'][own_team::2],
'bot_was_killed': game_state['bot_was_killed'][own_team::2],
'error_count': len(game_state['errors'][own_team]),
'food': list(game_state['food'][own_team]),
'name': game_state['team_names'][own_team]
def test_noiser_noising_at_noise_radius_extreme(ii):
""" It is the any team's turn, and the noiser should
still noise within confines of maze, despite extreme radius """
gamestate = make_gamestate()
gamestate["turn"] = random.randint(0, 3)
team_id = gamestate["turn"] % 2
old_bots = gamestate["bots"]
team_bots = old_bots[team_id::2]
enemy_bots = old_bots[1 - team_id::2]
noised = gf.noiser(walls=gamestate["walls"],
bot_position=gamestate["bots"][gamestate["turn"]],
enemy_positions=enemy_bots,
noise_radius=50, sight_distance=5, rnd=None)
assert all(noised["is_noisy"])
for noised_pos in noised["enemy_positions"]:
# check that the noised position is legal
assert noised_pos not in gamestate["walls"]
assert 0 <= noised_pos[0] < max(gamestate["walls"])[0]
assert 0 <= noised_pos[1] < max(gamestate["walls"])[1]
test_layout = (
""" ##################
########## . 2 #
########## #####3#
###0###### . . 1#
################## """)
# we test what bot 1 sees
# bot 0 should not be noised
# bot 2 should not be noised
parsed = parse_layout(test_layout)
expected = parsed['food'] + [parsed['bots'][0]]
position_bucket = collections.defaultdict(int)
# check a few times
for i in range(5):
noised = gf.noiser(walls=parsed['walls'],
bot_position=parsed['bots'][1],
enemy_positions=parsed['bots'][0::2])
assert noised['is_noisy'] == [False, False]
noised_pos = noised['enemy_positions']
assert noised_pos == parsed['bots'][0::2]
parsed = parse_layout(test_layout)
expected_0 = [(1, 1), (1, 2), (1, 3), (2, 3), (3, 3),
(4, 3), (5, 3), (6, 3), (7, 3), (7, 2),
(7, 1), (6, 1), (5, 1), (4, 1), (3, 1),
(8, 2), (8, 3)]
position_bucket_0 = collections.defaultdict(int)
expected_2 = [(1, 1), (1, 2), (2, 3), (3, 3), (4, 3),
(5, 3), (6, 3), (7, 3), (8, 2), (8, 1),
(7, 1), (6, 1), (5, 1), (4, 1), (3, 1),
(9, 2), (8, 3), (7, 2), (10, 1)]
position_bucket_2 = collections.defaultdict(int)
for i in range(200):
noised = gf.noiser(walls=parsed['walls'],
bot_position=parsed['bots'][1],
enemy_positions=parsed['bots'][0::2])
assert noised['is_noisy'] == [True, True]
position_bucket_0[noised['enemy_positions'][0]] += 1
position_bucket_2[noised['enemy_positions'][1]] += 1
assert 200 == sum(position_bucket_0.values())
assert 200 == sum(position_bucket_2.values())
# Since this is a randomized algorithm we need to be a bit lenient with
# our tests. We check that each position was selected at least once.
assert set(position_bucket_0.keys()) == set(expected_0)
assert set(position_bucket_2.keys()) == set(expected_2)
def test_uniform_noise_manhattan(noise_radius, expected, test_layout=None):
# Test how bot 1 observes bot 0
if not test_layout:
test_layout = (
""" ##################
# #. . # . #
# ##### ##### #
# 0 . # . .#1#
################## """)
parsed = parse_layout(test_layout)
position_bucket = collections.defaultdict(int)
for i in range(200):
noised = gf.noiser(walls=parsed['walls'],
bot_position=parsed['bots'][1],
enemy_positions=[parsed['bots'][0]],
noise_radius=noise_radius)
if noise_radius == 0:
assert noised['is_noisy'] == [False]
else:
assert noised['is_noisy'] == [True]
noised_pos = noised['enemy_positions'][0]
position_bucket[noised_pos] += 1
assert 200 == sum(position_bucket.values())
# Since this is a randomized algorithm we need to be a bit lenient with
# our tests. We check that each position was selected at least once.
assert set(position_bucket.keys()) == set(expected)
def test_uniform_noise_manhattan_graphical_distance(test_layout, is_noisy):
# Test how bot 1 observes bot 0
# the expected locations are where the food is placed
parsed = parse_layout(test_layout)
expected = parsed['food'] + [parsed['bots'][0]]
position_bucket = collections.defaultdict(int)
NUM_TESTS = 400
for i in range(NUM_TESTS):
noised = gf.noiser(walls=parsed['walls'],
bot_position=parsed['bots'][1],
enemy_positions=[parsed['bots'][0]])
# use default values for radius and distance
# noise_radius=5, sight_distance=5
assert noised['is_noisy'] == [is_noisy]
noised_pos = noised['enemy_positions'][0]
position_bucket[noised_pos] += 1
assert NUM_TESTS == sum(position_bucket.values())
# Since this is a randomized algorithm we need to be a bit lenient with
# our tests. We check that each position was selected at least once.
assert set(position_bucket.keys()) == set(expected)