Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def move(b, s):
return b.position
""")
out_folder = tempfile.TemporaryDirectory()
with tempfile.NamedTemporaryFile('w+', suffix='.py') as f:
with tempfile.NamedTemporaryFile('w+', suffix='.py') as g:
print(failing_player, file=f, flush=True)
print(good_player, file=g, flush=True)
if failing_team == 0:
teams = [f.name, g.name]
elif failing_team == 1:
teams = [g.name, f.name]
state = pelita.game.run_game(teams,
max_rounds=2,
layout_dict=pelita.layout.parse_layout(layout),
store_output=out_folder.name)
assert state['whowins'] == 1 - failing_team
# when team 1 fails, it’s turn will be 1
if failing_team == 0:
fail_turn = 0
elif failing_team == 1:
fail_turn = 1
assert state['fatal_errors'][failing_team][0] == {'type': 'FatalException',
'description': 'Exception in client (ZeroDivisionError): division by zero',
'turn': fail_turn,
'round': 2}
assert state['fatal_errors'][1 - failing_team] == []
assert state['errors'] == [{}, {}]
# in the first round (round #1),
# all bots move to the south
if bot.round == 1:
# go one step to the right
return (bot.position[0], bot.position[1] + 1)
else:
# There should not be more then one round in this test
raise RuntimeError("We should not be here in this test")
l = layout.parse_layout(l)
assert l['bots'][0] == (2, 1)
assert l['bots'][1] == (5, 1)
assert l['bots'][2] == (1, 1)
assert l['bots'][3] == (6, 1)
# max_rounds == 1 should call move just once
final_state = run_game([move, move], layout_dict=l, max_rounds=1)
assert final_state['round'] == 1
assert final_state['bots'][0] == (2, 2)
assert final_state['bots'][1] == (5, 2)
assert final_state['bots'][2] == (1, 2)
assert final_state['bots'][3] == (6, 2)
# max_rounds == 2 should finish and have the first team lose
final_state = run_game([move, move], layout_dict=l, max_rounds=2)
assert final_state['round'] == 2
assert final_state['turn'] == 0
assert final_state['bots'][0] == (2, 2)
assert final_state['bots'][1] == (5, 2)
assert final_state['bots'][2] == (1, 2)
assert final_state['bots'][3] == (6, 2)
assert final_state['gameover']
assert final_state['whowins'] == 1
assert final_state['fatal_errors'][0][0] == {
def test_path(self):
test_layout = (
""" ############
# . # .# ##
# ## # # ##
#a#. .##x#
###. .####
#b#. .# y#
############ """)
teams = [
nq_random_player,
nq_random_player
]
state = run_game(teams, layout_dict=parse_layout(test_layout), max_rounds=7)
assert state['bots'][0] == (4, 3)
assert state['bots'][1] == (10, 3)
def test_too_many_moves(self):
test_layout = (
""" ############
#a . . x#
#b y#
############ """)
movements_0 = [east, east]
movements_1 = [west, west]
teams = [
stepping_player(movements_0, movements_0),
stepping_player(movements_1, movements_1)
]
state = run_game(teams, layout_dict=parse_layout(test_layout), max_rounds=2)
assert state['fatal_errors'] == [[], []]
state = run_game(teams, layout_dict=parse_layout(test_layout), max_rounds=3)
assert len(state['fatal_errors'][0])
# TODO: check for exact turn/round of the failure
def test_minimal_remote_game():
def move(b, s):
return b.position
layout_name, layout_string = layout.get_random_layout()
l = layout.parse_layout(layout_string)
final_state = run_game(["test/demo01_stopping.py", move], max_rounds=20, layout_dict=l)
final_state = run_game(["test/demo01_stopping.py", 'test/demo02_random.py'], max_rounds=20, layout_dict=l)
assert final_state['gameover'] is True
assert final_state['score'] == [0, 0]
assert final_state['round'] == 20
def test_stepping_players(self):
test_layout = (
""" ############
#a . . x#
#b y#
############ """)
movements_0 = [east, east]
movements_1 = [west, west]
teams = [
stepping_player(movements_0, movements_0),
stepping_player(movements_1, movements_1)
]
state = setup_game(teams, layout_dict=parse_layout(test_layout), max_rounds=2)
assert state['bots'] == [(1, 1), (10, 1), (1, 2), (10, 2)]
state = run_game(teams, layout_dict=parse_layout(test_layout), max_rounds=2)
assert state['bots'] == [(3, 1), (8, 1), (3, 2), (8, 2)]
new_pos = (x + 1, y)
# The blue team should notice immediately (in round == 2!) that bot 1 has been eaten
if bot.round == 2 and bot.is_blue and bot.turn == 0:
assert bot.was_killed is False
assert bot.other.was_killed
# When bot 1 moves, the flag is still set
elif bot.round == 2 and bot.is_blue and bot.turn == 1:
assert bot.was_killed
assert bot.other.was_killed is False
else:
assert bot.was_killed is False
assert bot.other.was_killed is False
# otherwise return current position
return new_pos
state = run_game([move, move], max_rounds=3, layout_dict=parse_layout(layout), allow_exceptions=True)
# assertions might have been caught in run_game
# check that all is good
assert state['fatal_errors'] == [[], []]
def test_minimal_game():
def move(b, s):
return b.position
layout_name, layout_string = layout.get_random_layout()
l = layout.parse_layout(layout_string)
final_state = run_game([move, move], max_rounds=20, layout_dict=l)
assert final_state['gameover'] is True
assert final_state['score'] == [0, 0]
assert final_state['round'] == 20
layout_name = str(layout_path)
# use the basename of the path as a layout name
layout_name = layout_path.parts[-1]
layout_string = layout_path.read_text()
else:
# if the given layout is not a path, we assume it is the name of one
# of the built-in paths
layout_name = args.layout
layout_string = pelita.layout.get_layout_by_name(args.layout)
else:
layout_name, layout_string = pelita.layout.get_random_layout(args.size)
print("Using layout '%s'" % layout_name)
layout_dict = layout.parse_layout(layout_string)
game.run_game(team_specs=team_specs, max_rounds=args.rounds, layout_dict=layout_dict, layout_name=layout_name, seed=args.seed,
timeout_length=args.timeout_length, max_team_errors=args.max_timeouts,
viewers=viewers, viewer_options=viewer_options,
store_output=args.store_output)