def test_multiple_enemies_killing(): """ Check that you can kill multiple enemies at once. """ l0 = """ ######## # .. # # 210 # ######## ######## # .. # # 3 # ######## """ l1 = """ ######## # .. # # 103 # ######## ######## # .. # # 2 # ######## """ # dummy bots stopping = lambda bot, s: (bot.position, s) parsed_l0 = layout.parse_layout(l0) for bot in (0, 2): game_state = setup_game([stopping, stopping], layout_dict=parsed_l0) game_state['turn'] = bot # get position of bots 1 (and 3) kill_position = game_state['bots'][1] assert kill_position == game_state['bots'][3] new_state = apply_move(game_state, kill_position) # team 0 scores twice assert new_state['score'] == [10, 0] # bots 1 and 3 are back to origin assert new_state['bots'][1::2] == [(6, 2), (6, 1)] parsed_l1 = layout.parse_layout(l1) for bot in (1, 3): game_state = setup_game([stopping, stopping], layout_dict=parsed_l1) game_state['turn'] = bot # get position of bots 0 (and 2) kill_position = game_state['bots'][0] assert kill_position == game_state['bots'][2] new_state = apply_move(game_state, kill_position) # team 1 scores twice assert new_state['score'] == [0, 10] # bots 0 and 2 are back to origin assert new_state['bots'][0::2] == [(1, 1), (1, 2)]
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"], "shape": parsed_l["shape"], "max_rounds": 300, "team_names": ("a", "b"), "turn": turn, "round": 0, "timeout": [], "gameover": False, "whowins": None, "team_say": "bla", "score": 0, "error_limit": 5, "kills": [0] * 4, "deaths": [0] * 4, "bot_was_killed": [False] * 4, "errors": [[], []], "fatal_errors": [{}, {}], "rnd": random.Random() } legal_positions = get_legal_positions(game_state["walls"], game_state["shape"], game_state["bots"][turn]) game_state_new = apply_move(game_state, legal_positions[0]) assert game_state_new["bots"][turn] == legal_positions[0]
def test_apply_move_resets_bot_was_killed(bot_to_move, bot_was_killed_flags): """ Check that `prepare_bot_state` sees the proper bot_was_killed flag and that `apply_move` will reset the flag to False. """ team_id = bot_to_move % 2 other_bot = (bot_to_move + 2) % 4 other_team_id = 1 - team_id # specify which bot should move test_state = setup_random_basic_gamestate(turn=bot_to_move) bot_was_killed_flags = list(bot_was_killed_flags) # needs to be a list test_state[ 'bot_was_killed'] = bot_was_killed_flags[:] # copy to avoid reference issues # create bot state for current turn current_bot_position = test_state['bots'][bot_to_move] bot_state = game.prepare_bot_state(test_state) # bot state should have proper bot_was_killed flag assert bot_state['team']['bot_was_killed'] == bot_was_killed_flags[ team_id::2] # apply a dummy move that should reset bot_was_killed for the current bot new_test_state = game.apply_move(test_state, current_bot_position) # the bot_was_killed flag should be False again assert test_state['bot_was_killed'][bot_to_move] == False # the bot_was_killed flags for other bot should still be as before assert test_state['bot_was_killed'][other_bot] == bot_was_killed_flags[ other_bot] # all bot_was_killed flags for other team should still be as before assert test_state['bot_was_killed'][ other_team_id::2] == bot_was_killed_flags[other_team_id::2]
def test_suicide(): """ Check that suicide works. """ l0 = """ ######## # .. # #ybxa # ######## """ l1 = """ ######## # .. # # xayb# ######## """ # 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) for bot in (0, 2): game_state = setup_game([stopping, stopping], layout_dict=parsed_l1) game_state['turn'] = bot # get position of bot 3 suicide_position = game_state['bots'][3] new_state = apply_move(game_state, suicide_position) # team 0 scores assert new_state['score'] == [0, 5]
def test_play_turn_illegal_position(turn): """check that illegal moves are added to error dict and bot still takes move""" game_state = setup_random_basic_gamestate() game_state["turn"] = turn team = turn % 2 illegal_position = game_state["walls"][0] game_state_new = apply_move(game_state, illegal_position) assert len(game_state_new["errors"][team]) == 1 assert game_state_new["errors"][team][(1, turn)].keys() == set(["reason", "bot_position"]) assert game_state_new["bots"][turn] in get_legal_positions(game_state["walls"], game_state["bots"][turn])
def test_play_turn_fatal(turn): """Checks that game quite after fatal error""" game_state = setup_random_basic_gamestate() game_state["turn"] = turn team = turn % 2 fatal_list = [{}, {}] fatal_list[team] = {"error":True} game_state["fatal_errors"] = fatal_list move = get_legal_positions(game_state["walls"], game_state["bots"][turn]) game_state_new = apply_move(game_state, move[0]) assert game_state_new["gameover"] assert game_state_new["whowins"] == int(not team)
def test_play_turn_friendly_fire(setups): """Check that you can kill enemies but not yourself""" ### 012345678901234567 #0# ################## #1# #. ... .##. y# #2# # # # . .### #x# #3# # # ##. . # #4# # . .## # # #5# #a# ###. . # # # #6# #b .##. ... .# #7# ################## game_state = setup_specific_basic_gamestate() turn = setups[0] enemy_pos = setups[1] team = turn % 2 game_state["turn"] = turn enemy_idx = (1, 3) if team == 0 else (0, 2) game_state["bots"][enemy_idx[0]] = enemy_pos game_state_new = apply_move(game_state, enemy_pos) # assert game_state_new["DEATHS"][team] == 5 assert game_state_new["score"][team] == 5
def test_play_turn_killing(turn): """Check that you can kill enemies but not yourself""" ### 012345678901234567 #0# ################## #1# #. ... .##. y# #2# # # # . .### #x# #3# # # ##. . # #4# # . .## # # #5# #a# ###. . # # # #6# #b .##. ... .# #7# ################## game_state = setup_specific_basic_gamestate() team = turn % 2 game_state["turn"] = turn enemy_idx = (1, 3) if team == 0 else (0, 2) (friend_idx, ) = set([0, 1, 2, 3]) - set([*enemy_idx, turn]) game_state_new = apply_move(game_state, game_state["bots"][friend_idx]) # assert game_state_new["DEATHS"][team] == 5 assert game_state_new["score"] == [0, 0] assert game_state_new["deaths"] == [0] * 4
def test_play_turn_apply_error(turn): """check that quits when there are too many errors""" game_state = setup_random_basic_gamestate() error_dict = {"reason": 'illegal move', "bot_position": (1, 2)} game_state["turn"] = turn team = turn % 2 game_state["errors"] = [{(r, t): error_dict for r in (1, 2) for t in (0, 1)}, {(r, t): error_dict for r in (1, 2) for t in (0, 1)}] # we pretend that two rounds have already been played # so that the error dictionaries are sane game_state["round"] = 3 illegal_position = (0, 0) # should always be a wall game_state_new = apply_move(game_state, illegal_position) assert game_state_new["gameover"] assert len(game_state_new["errors"][team]) == 5 assert game_state_new["whowins"] == int(not team) assert set(game_state_new["errors"][team][(3, turn)].keys()) == set( ["reason", "bot_position"])
def test_play_turn_eating_enemy_food(turn, which_food): """Check that you eat enemy food but not your own""" ### 012345678901234567 #0# ################## #1# #. ... .##. y# #2# # # # . .### #x# #3# # # ##. . # #4# # . .## # # #5# #a# ###. . # # # #6# #2 .##. ... .# #7# ################## game_state = setup_specific_basic_gamestate(round=0, turn=turn) team = turn % 2 prev_len_food = [len(team_food) for team_food in game_state["food"]] if which_food == 0: # Try to eat food on left side game_state["bots"][turn] = (6, 4) move = (7, 4) else: # Try to eat food on right side game_state["bots"][turn] = (11, 1) move = (10, 1) game_state_new = apply_move(game_state, move) if team == which_food: # No changes for either team assert game_state_new["score"][team] == 0 assert game_state_new["score"][1 - team] == 0 assert prev_len_food[team] == len(game_state_new["food"][team]) assert prev_len_food[1 - team] == len(game_state_new["food"][1 - team]) elif team != which_food: # Own team gains points, other team loses food assert game_state_new["score"][team] > 0 assert game_state_new["score"][1 - team] == 0 assert prev_len_food[team] == len(game_state_new["food"][team]) assert prev_len_food[1 - team] > len(game_state_new["food"][1 - team])