Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
TEAM_NAME="p2"
def move(b, s):
print(f"{b.round} {b.turn} p2", file=sys.stdout)
print("p2err", file=sys.stderr)
return b.position
""")
out_folder = tempfile.TemporaryDirectory()
with tempfile.NamedTemporaryFile('w+', suffix='.py') as f:
with tempfile.NamedTemporaryFile('w+', suffix='.py') as g:
print(p1, file=f, flush=True)
print(p2, file=g, flush=True)
state = pelita.game.run_game([f.name, g.name],
max_rounds=2,
layout_dict=pelita.layout.parse_layout(layout),
store_output=out_folder.name)
assert state['whowins'] == 2
assert state['fatal_errors'] == [[], []]
assert state['errors'] == [{}, {}]
path = Path(out_folder.name)
blue_lines = (path / 'blue.out').read_text().split('\n')
red_lines = (path / 'red.out').read_text().split('\n')
# The first line contains the welcome message 'blue team 'path' -> 'name''
assert 'blue team' in blue_lines[0]
assert 'red team' in red_lines[0]
# now check what has been printed
assert blue_lines[1:] == ['1 0 p1', '1 1 p1', '2 0 p1', '2 1 p1', '']
assert red_lines[1:] == ['1 0 p2', '1 1 p2', '2 0 p2', '2 1 p2', '']
def setup_random_basic_gamestate(*, round=1, turn=0):
"""helper function for testing play turn"""
l = layout.get_layout_by_name("small_100")
parsed_l = layout.parse_layout(l)
stopping = lambda bot, s: (bot.position, s)
game_state = setup_game([stopping, stopping], layout_dict=parsed_l)
game_state['round'] = round
game_state['turn'] = turn
return game_state
def test_initial_positions_basic():
"""Checks basic example for initial positions"""
simple_layout = """
########
# ### #
# #
########
"""
walls = layout.parse_layout(simple_layout)['walls']
out = initial_positions(walls)
exp = [(1, 1), (6, 2), (1, 2), (6, 1)]
assert len(out) == 4
assert out == exp
def main():
args = parser.parse_args()
if args.help or args.long_help:
parser.print_help()
sys.exit(0)
if args.version:
print("Pelita {}".format(pelita.__version__))
sys.exit(0)
if args.list_layouts:
layouts = pelita.layout.get_available_layouts(size='all')
layouts.sort()
print('\n'.join(layouts))
sys.exit(0)
if args.viewer.startswith('tk') and not args.publish_to:
raise ValueError("Options --tk (or --tk-no-sync) and --no-publish are mutually exclusive.")
if args.log:
start_logging(args.log)
if args.rounds < 1:
raise ValueError(f"Must play at least one round (rounds={args.rounds}).")
if args.check_team:
if not args.team_specs:
raise ValueError("No teams specified.")
if args.layout:
# first check if the given layout is a file
layout_path = Path(args.layout)
if layout_path.exists():
# OK, this is a valid file, load it
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)
random.seed(args.seed)
if args.layout:
# first check if the given layout is a file
layout_path = Path(args.layout)
if layout_path.exists():
# OK, this is a valid file, load it
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)
def show_state(self, game_state):
uni_str = layout.layout_as_str(walls=game_state['walls'],
food=game_state['food'],
bots=game_state['bots'])
# color bots
uni_str = self.color_bots(uni_str)
# Everything that we print explicitly is removed from the state dict.
state = {}
state.update(game_state)
# for death and kills just leave a summary
state['blue deaths'] = state['deaths'][::2]
state['blue kills'] = state['kills'][::2]
state['red deaths'] = state['deaths'][1::2]
state['red kills'] = state['kills'][1::2]
del state['kills']
del state['deaths']
del state['walls']
def make_bots(*, walls, team, enemy, round, bot_turn, rng):
bots = {}
team_index = team['team_index']
enemy_index = enemy['team_index']
homezone = create_homezones(walls)
initial_positions = layout.initial_positions(walls)
team_initial_positions = initial_positions[team_index::2]
enemy_initial_positions = initial_positions[enemy_index::2]
team_bots = []
for idx, position in enumerate(team['bot_positions']):
b = Bot(bot_index=idx,
is_on_team=True,
score=team['score'],
deaths=team['deaths'][idx],
kills=team['kills'][idx],
was_killed=team['bot_was_killed'][idx],
error_count=team['error_count'],
food=team['food'],
walls=walls,
round=round,
bot_turn=bot_turn,
# OK, this is a valid file, load it
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)