def test_double_suicide(): # Test how a bot can be killed when it runs into two bots test_layout = """ ###### # bx # #. y.# ###### """ teams = [stepping_player('-', '-'), stepping_player('<', '-')] state = setup_game(teams, layout_dict=layout.parse_layout(test_layout, bots={'a': (2, 1)}), 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_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 = """ ###### #a .x# #. # #b y# ###### """ 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_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 = """ ###### #a .y# #.bx # ###### """ 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_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])
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_moving_through_maze(): test_start = """ ###### #a . # #.. x# #b y# ###### """ 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(""" ###### # a. # #..x # #b y# ###### """) 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(""" ###### # a. # #bx # # y# ###### """, bots={'b': (1, 2)}, food=[(1, 2)]) # b sitting on food 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): state = game.play_turn(state) test_third_round = layout.parse_layout(""" ###### #b . # #.a x# # y# ###### """) assert test_third_round['bots'] == state['bots'] assert test_third_round['food'] == list(state['food'][0]) + list( state['food'][1]) assert state['score'] == [game.KILL_POINTS, 1] for i in range(4): state = game.play_turn(state) test_fourth_round = layout.parse_layout(""" ###### #b . # #a x # # y# ###### """, bots={'a': (1, 2)}, food=[(1, 2)]) # a sitting on food assert test_fourth_round['bots'] == state['bots'] assert test_fourth_round['food'] == list(state['food'][0]) + list( state['food'][1]) assert state['score'] == [game.KILL_POINTS, game.KILL_POINTS + 1] for i in range(4): state = game.play_turn(state) test_fifth_round = layout.parse_layout(""" ###### # b. # #.a x# # y# ###### """) assert test_fifth_round['bots'] == state['bots'] assert test_fifth_round['food'] == list(state['food'][0]) + list( state['food'][1]) assert state['score'] == [game.KILL_POINTS * 2, game.KILL_POINTS + 1] for i in range(4): state = game.play_turn(state) test_sixth_round = layout.parse_layout(""" ###### # b. # #a x # # y# ###### """, bots={'a': (1, 2)}, food=[(1, 2)]) # a sitting on food assert test_sixth_round['bots'] == state['bots'] assert test_sixth_round['food'] == list(state['food'][0]) + list( state['food'][1]) assert state['score'] == [game.KILL_POINTS * 2, game.KILL_POINTS * 2 + 1] for i in range(3): # !! Only move three bots state = game.play_turn(state) test_seventh_round = layout.parse_layout(""" ###### # b # #a x # # y# ###### """, bots={'a': (1, 2)}, food=[(1, 2) ]) # a sitting on food assert test_seventh_round['bots'] == state['bots'] assert test_seventh_round['food'] == list(state['food'][0]) + list( state['food'][1]) assert state['score'] == [ game.KILL_POINTS * 2 + 1, game.KILL_POINTS * 2 + 1 ] assert state['gameover'] == True assert state['whowins'] == 2 with pytest.raises(ValueError): state = game.play_turn(state)