Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# 0.2 #
# 3.1 #
########
"""
bot_turn = bot_to_move // 2
team_to_move = bot_to_move % 2
def move(bot, s):
if team_to_move == 0 and bot.is_blue and bot_turn == bot._bot_turn:
return (4, 1)
# eat the food between 0 and 2
if team_to_move == 1 and (not bot.is_blue) and bot_turn == bot._bot_turn:
# eat the food between 3 and 1
return (3, 2)
return bot.position
l = layout.parse_layout(l)
final_state = run_game([move, move], layout_dict=l, max_rounds=20)
assert final_state['round'] == 1
assert final_state['turn'] == bot_to_move
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
]
state = setup_game(teams, layout_dict=parse_layout(test_layout), max_rounds=20, seed=20)
assert state['bots'][0] == (4, 4)
assert state['bots'][1] == (4 + 7, 4)
state = run_game(teams, layout_dict=parse_layout(test_layout), max_rounds=20, seed=20)
pos_left_bot = state['bots'][0]
pos_right_bot = state['bots'][1]
# running again to test seed:
state = run_game(teams, layout_dict=parse_layout(test_layout), max_rounds=20, seed=20)
assert state['bots'][0] == pos_left_bot
assert state['bots'][1] == pos_right_bot
# running again with other seed:
state = run_game(teams, layout_dict=parse_layout(test_layout), max_rounds=20, seed=200)
# most probably, either the left bot or the right bot or both are at
# a different position
assert not (state['bots'][0] == pos_left_bot and state['bots'][1] == pos_right_bot)
def test_play_turn_move():
"""Checks that bot is moved to intended space"""
turn = 0
l = layout.get_layout_by_name("small_100")
parsed_l = layout.parse_layout(l)
game_state = {
"food": parsed_l["food"],
"walls": parsed_l["walls"],
"bots": parsed_l["bots"],
"max_rounds": 300,
"team_names": ("a", "b"),
"turn": turn,
"round": 0,
"timeout": [],
"gameover": False,
"whowins": None,
"team_say": "bla",
"score": 0,
"kills":[0]*4,
"deaths": [0]*4,
"bot_was_killed": [False]*4,
for i in range(4):
state = game.play_turn(state)
test_first_round = layout.parse_layout(
""" ######
# 0. #
#..1 #
#2 3#
###### """)
assert test_first_round['bots'] == state['bots']
assert test_first_round['food'] == list(state['food'][0]) + list(state['food'][1])
assert state['score'] == [0, 0]
for i in range(4):
state = game.play_turn(state)
test_second_round = layout.parse_layout(
""" ######
# . #
#. #
# #
######
######
# 0 #
#21 #
# 3#
###### """)
assert test_second_round['bots'] == state['bots']
assert test_second_round['food'] == list(state['food'][0]) + list(state['food'][1])
assert state['score'] == [0, 1]
for i in range(4):
def test_get_legal_positions_basic():
"""Check that the output of legal moves contains all legal moves for one example layout"""
l = layout.get_layout_by_name(layout_name="small_100")
parsed_l = layout.parse_layout(l)
legal_positions = get_legal_positions(parsed_l["walls"], parsed_l["bots"][0])
exp = [(1, 4), (1, 6), (1, 5)]
assert legal_positions == exp
tp = """
import time
TEAM_NAME = "500ms timeout"
def move(b, s):
if b.round == 1 and b.turn == 1:
return (-2, 0)
time.sleep(0.5)
return b.position
"""
with tempfile.NamedTemporaryFile('w+', suffix='.py') as f:
print(dedent(tp), file=f, flush=True)
timeout_player = f.name
state = pelita.game.run_game([stopping_player, timeout_player],
max_rounds=8,
layout_dict=pelita.layout.parse_layout(layout),
timeout_length=0.5)
assert state['whowins'] == 0
assert state['fatal_errors'] == [[], []]
assert state['errors'] == [{},
{(1, 1): {'description': '', 'type': 'PlayerTimeout'},
(1, 3): {'bot_position': (-2, 0), 'reason': 'illegal move'},
(2, 1): {'description': '', 'type': 'PlayerTimeout'},
(2, 3): {'description': '', 'type': 'PlayerTimeout'},
(3, 1): {'description': '', 'type': 'PlayerTimeout'}}]
def test_speaking_player():
test_layout = (
""" ############
#ab#. .#yx#
############ """)
teams = [
speaking_player,
random_player
]
state = run_game(teams, layout_dict=parse_layout(test_layout), max_rounds=1)
assert state["say"][0].startswith("Going")
assert state["say"][1] == ""
########
# .. #
#3210 #
########
"""
l1 = """
########
# .. #
# 1032#
########
"""
# dummy bots
stopping = lambda bot, s: (bot.position, s)
parsed_l0 = layout.parse_layout(l0)
for bot in (1, 3):
game_state = setup_game([stopping, stopping], layout_dict=parsed_l0)
game_state['turn'] = bot
# get position of bot 2
suicide_position = game_state['bots'][2]
new_state = apply_move(game_state, suicide_position)
# team 0 scores
assert new_state['score'] == [5, 0]
# # bots 1 and 3 are back to origin
if bot == 1:
assert new_state['bots'][1::2] == [(6, 2), (1, 2)]
elif bot == 3:
assert new_state['bots'][1::2] == [(3, 2), (6, 1)]
parsed_l1 = layout.parse_layout(l1)