Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_suicide_win():
# Test how a bot eats a food pellet that the enemy sits on
# Since it is the last pellet, the game will end directly
test_layout = """
######
#0 .1#
#. #
#2 3#
######
"""
teams = [
stepping_player('>>', '--'),
stepping_player('<-', '--')
]
state = setup_game(teams, layout_dict=layout.parse_layout(test_layout), max_rounds=2)
assert state['bots'] == [(1, 1), (4, 1), (1, 3), (4, 3)]
assert state['food'] == [{(1, 2)}, {(3, 1)}]
# play until finished
state = run_game(teams, layout_dict=layout.parse_layout(test_layout), max_rounds=2)
# bot 0 has been reset
assert state['bots'] == [(1, 2), (3, 1), (1, 3), (4, 3)]
assert state['food'] == [{(1, 2)}, set()]
assert state['gameover'] == True
assert state['whowins'] == 1
assert state['round'] == 2
assert state['turn'] == 0
assert state['score'] == [1, game.KILL_POINTS]
def test_bot_does_not_eat_own_food():
test_layout = """
######
#0 .3#
#.21 #
######
"""
teams = [
stepping_player('v', '<'),
stepping_player('^', '<')
]
state = setup_game(teams, layout_dict=layout.parse_layout(test_layout), max_rounds=2)
assert state['bots'] == [(1, 1), (3, 2), (2, 2), (4, 1)]
assert state['food'] == [{(1, 2)}, {(3, 1)}]
for i in range(4):
state = play_turn(state)
assert state['bots'] == [(1, 2), (3, 1), (1, 2), (3, 1)]
assert state['food'] == [{(1, 2)}, {(3, 1)}]
def test_shorthand(self):
test_layout = (
""" ############
#a . . y#
#b x#
############ """)
num_rounds = 5
teams = [
stepping_player('>v<^-', '-----'),
stepping_player('<^>v-', '-----')
]
state = setup_game(teams, layout_dict=parse_layout(test_layout), max_rounds=5)
player0_expected_positions = [(1,1), (2,1), (2,2), (1,2), (1,1), (1, 1)]
player1_expected_positions = [(10,2), (9,2), (9,1), (10,1), (10,2), (10, 2)]
assert state['bots'][0] == player0_expected_positions[0]
assert state['bots'][1] == player1_expected_positions[0]
for i in range(1, num_rounds+1):
for step in range(4):
state = play_turn(state)
assert state['bots'][0] == player0_expected_positions[i]
assert state['bots'][1] == player1_expected_positions[i]
def test_bot_does_not_eat_own_food():
test_layout = """
######
#0 .3#
#.21 #
######
"""
teams = [
stepping_player('v', '<'),
stepping_player('^', '<')
]
state = setup_game(teams, layout_dict=layout.parse_layout(test_layout), max_rounds=2)
assert state['bots'] == [(1, 1), (3, 2), (2, 2), (4, 1)]
assert state['food'] == [{(1, 2)}, {(3, 1)}]
for i in range(4):
state = play_turn(state)
assert state['bots'] == [(1, 2), (3, 1), (1, 2), (3, 1)]
assert state['food'] == [{(1, 2)}, {(3, 1)}]
def test_double_suicide():
# Test how a bot can be killed when it runs into two bots
test_layout = """
######
# 01 #
#. .#
######
######
# 2 #
#. 3.#
######
"""
teams = [
stepping_player('-', '-'),
stepping_player('<', '-')
]
state = setup_game(teams, layout_dict=layout.parse_layout(test_layout), max_rounds=2)
assert state['bots'] == [(2, 1), (3, 1), (2, 1), (3, 2)]
assert state['food'] == [{(1, 2)}, {(4, 2)}]
# play a two turns so that 1 moves
state = play_turn(state)
state = play_turn(state)
# bot 1 has been reset
assert state['bots'] == [(2, 1), (4, 2), (2, 1), (3, 2)]
assert state['food'] == [{(1, 2)}, {(4, 2)}]
assert state['gameover'] == False
assert state['round'] == 1
assert state['turn'] == 1
# only a single KILL_POINT has been given
assert state['score'] == [game.KILL_POINTS, 0]
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)]
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)]
def test_double_suicide():
# Test how a bot can be killed when it runs into two bots
test_layout = """
######
# 01 #
#. .#
######
######
# 2 #
#. 3.#
######
"""
teams = [
stepping_player('-', '-'),
stepping_player('<', '-')
]
state = setup_game(teams, layout_dict=layout.parse_layout(test_layout), max_rounds=2)
assert state['bots'] == [(2, 1), (3, 1), (2, 1), (3, 2)]
assert state['food'] == [{(1, 2)}, {(4, 2)}]
# play a two turns so that 1 moves
state = play_turn(state)
state = play_turn(state)
# bot 1 has been reset
assert state['bots'] == [(2, 1), (4, 2), (2, 1), (3, 2)]
assert state['food'] == [{(1, 2)}, {(4, 2)}]
assert state['gameover'] == False
assert state['round'] == 1
assert state['turn'] == 1
# only a single KILL_POINT has been given
assert state['score'] == [game.KILL_POINTS, 0]
def test_shorthand(self):
test_layout = (
""" ############
#a . . y#
#b x#
############ """)
num_rounds = 5
teams = [
stepping_player('>v<^-', '-----'),
stepping_player('<^>v-', '-----')
]
state = setup_game(teams, layout_dict=parse_layout(test_layout), max_rounds=5)
player0_expected_positions = [(1,1), (2,1), (2,2), (1,2), (1,1), (1, 1)]
player1_expected_positions = [(10,2), (9,2), (9,1), (10,1), (10,2), (10, 2)]
assert state['bots'][0] == player0_expected_positions[0]
assert state['bots'][1] == player1_expected_positions[0]
for i in range(1, num_rounds+1):
for step in range(4):
state = play_turn(state)
assert state['bots'][0] == player0_expected_positions[i]
assert state['bots'][1] == player1_expected_positions[i]
def test_moving_through_maze():
test_start = """
######
#0 . #
#.. 1#
#2 3#
###### """
parsed = layout.parse_layout(test_start)
teams = [
stepping_player('>-v>>>-', '-^^->->'),
stepping_player('<<-<<<-', '-------')
]
state = setup_game(teams, layout_dict=parsed, max_rounds=8)
# play first round
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])