def test_equal_positions(): layout_str = """ ######## #0### # # . ...# ######## ######## #1### # # . ...# ######## ######## #E### # # . ...# ######## ######## #E### # # . ...# ######## """ layout = parse_layout(layout_str, allow_enemy_chars=True) assert layout['bots'] == [(1, 1), (1, 1)] assert layout['enemy'] == [(1, 1), (1, 1)] setup_test_game(layout=layout_str)
def test_equal_positions(self): layout_str = """ ######## #0### # # . ...# ######## ######## #1### # # . ...# ######## ######## #E### # # . ...# ######## ######## #E### # # . ...# ######## """ layout = create_layout(layout_str) assert layout.bots == [(1, 1), (1, 1)] assert layout.enemy == [(1, 1), (1, 1)] setup_test_game(layout=layout_str)
def test_equal_positions(self): layout_str = """ ######## #0### # # . ...# ######## ######## #1### # # . ...# ######## ######## #E### # # . ...# ######## ######## #E### # # . ...# ######## """ layout = create_layout(layout_str) assert layout.bots == [(1, 1), (1, 1)] assert layout.enemy == [(1, 1), (1, 1)] setup_test_game(layout=layout_str)
def test_rebuild_uni(self): layout = """ ############ #0#. .# 1# ############ """ game = setup_test_game(layout=layout, is_blue=True) assert game.team[0].position == (1, 1) assert game.team[1].position == (10, 1) assert game.team[0].enemy[0].position is None assert game.team[0].enemy[1].position is None uni, state = _rebuild_universe(game.team[0]._bots) assert uni.bots[0].current_pos == (1, 1) assert uni.bots[2].current_pos == (10, 1) assert uni.bots[1].current_pos == (9, 1) assert uni.bots[3].current_pos == (10, 1) with pytest.raises(ValueError): uni, state = _rebuild_universe(game.team[0]._bots[0:2]) bots = bots_from_universe(uni, [None] * 4, round=0, team_name=state['team_name'], timeout_count=state['timeout_teams']) uni2, state = _rebuild_universe(bots) assert uni2 == uni
def test_rebuild_uni(self): layout = """ ############ #0#. .# 1# ############ """ bot = setup_test_game(layout=layout, is_blue=True) assert bot._team[0].position == (1, 1) assert bot._team[1].position == (10, 1) assert bot._team[0].enemy[0].position is None assert bot._team[0].enemy[1].position is None uni, state = _rebuild_universe(bot._bots) assert uni.bots[0].current_pos == (1, 1) assert uni.bots[2].current_pos == (10, 1) assert uni.bots[1].current_pos == (9, 1) assert uni.bots[3].current_pos == (10, 1) with pytest.raises(ValueError): uni, state = _rebuild_universe(bot._bots[0:2]) bots = bots_from_universe(uni, [None] * 4, round=0, team_name=state['team_name'], timeout_count=state['timeout_teams']) uni2, state = _rebuild_universe(bots) assert uni2 == uni
def test_setup_test_game(is_blue): # Test that is_noisy is set properly layout = """ ################## #. ... .##. y# # # # . .### # # # # ##. x . # # . .## # # #a# ###. . # # # #b .##. ... .# ################## """ test_game = utils.setup_test_game(layout=layout, is_blue=is_blue, is_noisy={"a":False, "b":True, "x":False, "y":True}) if is_blue: assert test_game.position == (1, 5) assert test_game.other.position == (1, 6) assert test_game.enemy[0].position == (8, 3) assert test_game.enemy[1].position == (16, 1) else: assert test_game.position == (8, 3) assert test_game.other.position == (16, 1) assert test_game.enemy[0].position == (1, 5) assert test_game.enemy[1].position == (1, 6) # load_builtin_layout loads unnoised enemies assert test_game.enemy[0].is_noisy is False assert test_game.enemy[1].is_noisy is True
def test_shortest_path(): # is the Graph implementation in pelita giving us the shortest path to the # food pellet? And are we really following it? # straight line is the right choice layout1=""" ######## #0 .# #.1 EE# ######## """ path1 = [(6,1), (5,1), (4,1), (3,1), (2,1)] # there are more alternatives now, but the shortest is unique, so we can # test it layout2=""" ######## #0####.# # # # # #.1 EE# ######## """ path2 = [(6, 1), (6,2), (5,2), (4,2), (3,2), (2,2), (1,2)] for l, p in ((layout1, path1), (layout2, path2)): game = setup_test_game(layout=l, is_blue=True) # we can ignore this, we just call move to have the bot generate the graph # representation of the maze next_move = move(0, game) graph = game.state['graph'] path = graph.a_star((1,1), (6,1)) # test that the generated path is the shortest one assert path == p # given this layout, move our bot to the next position in the shortest # path and see if we follow it path.reverse() # flip the path so we have it in the right order for idx, step in enumerate(path[:-1]): # create a layout where we are starting from the current step in # the path new_layout = create_layout(l, bots=[step]) game = setup_test_game(layout=new_layout, is_blue=True) next_move = move(0, game) next_pos = (step[0]+next_move[0], step[1]+next_move[1]) assert next_pos == path[idx+1]
def test_stop_at_the_border(): # do we stop at the border when we reach it? layout = """ ######## # 1.# #. 0E E# ########""" bot = setup_test_game(layout=layout, is_blue=True) next_pos = move(bot, {}) assert next_pos == bot.position
def test_stop_at_the_border(): # do we stop at the border when we reach it? layout = """ ######## # 1.# #. 0E E# ########""" bot = setup_test_game(layout=layout, is_blue=True) next_move, _ = move(bot, None) assert next_move == (0, 0)
def test_face_the_enemy(): # do we move along the border to face the enemy when it's still in its own # homezone? layout = """ ######## # 0 1.# #. E E# ########""" bot = setup_test_game(layout=layout, is_blue=True) next_move, _ = move(bot, None) assert next_move == (0, 1)
def test_eat_food(): # check that we indeed collect food when possible layout = """ ######## #E # .## #1.E 0 # ######## """ game = setup_test_game(layout=layout, is_blue=True) next_move = move(0, game) assert next_move == (0, -1)
def test_eat_food(): # do we eat food when it's available? layout=""" ######## # 0.# #.1 EE# ######## """ game = setup_test_game(layout=layout, is_blue=True) next_move = move(0, game) assert next_move == (1, 0)
def test_kill_enemy(): # do we kill enemies when possible? layout=""" ######## # 1.# #.0E E# ######## """ bot = setup_test_game(layout=layout, is_blue=True) next_pos, _ = move(bot, None) assert next_pos == (3, 2)
def test_no_kamikaze_stop(): # Check that we stop if escaping would kill us layout = """ ######## # ###.# #1. E0E# ######## """ bot = setup_test_game(layout=layout, is_blue=True) next_pos, _ = move(bot, None) assert next_pos == (5, 2)
def test_eat_food(): # check that we indeed collect food when possible layout = """ ######## #E # .## #1.E 0 # ######## """ bot = setup_test_game(layout=layout, is_blue=True) next_pos, _ = move(bot, None) assert next_pos == (5, 1)
def test_eat_enemy(): # check that we indeed eat an enemy when possible layout = """ ######## #E###.## #0. 1E# ######## """ bot = setup_test_game(layout=layout, is_blue=True) next_pos, _ = move(bot, None) assert next_pos == (1, 1)
def test_kill_enemy(): # do we eat food when it's available? layout = """ ######## # 1.# #.0E E# ######## """ bot = setup_test_game(layout=layout, is_blue=True) next_move, _ = move(bot, None) assert next_move == (1, 0)
def test_no_kamikaze_stop(): # Check that we stop if escaping would kill us layout = """ ######## # ###.# #1. E0E# ######## """ game = setup_test_game(layout=layout, is_blue=True) next_move = move(0, game) assert next_move == (0, 0)
def test_face_the_enemy(): # do we move along the border to face the enemy when it's still in its own # homezone? layout = """ ######## # a b.# #. x y# ########""" bot = setup_test_game(layout=layout, is_blue=True) next_pos = move(bot, {}) assert next_pos == (3, 2)
def test_eat_food(): # do we eat food when it's available? layout=""" ######## # a.# #.b xy# ######## """ bot = setup_test_game(layout=layout, is_blue=True) next_move = move(bot, {}) assert next_move == (6, 1)
def test_eat_enemy(): # check that we indeed eat an enemy when possible layout = """ ######## #E###.## #0. 1E# ######## """ game = setup_test_game(layout=layout, is_blue=True) next_move = move(0, game) assert next_move == (0, -1)
def test_no_kamikaze(): # do we avoid enemies when they can kill us? layout=""" ######## # x.# #.b ay# ######## """ bot = setup_test_game(layout=layout, is_blue=True) next_move = move(bot, {}) assert next_move == (4, 2) or next_move == (5, 2)
def test_do_not_step_on_enemy(): # check that we don't step back on an enemy when we are fleeing layout=""" ######## # x.# #.b #ay# ######## """ bot = setup_test_game(layout=layout, is_blue=True) next_move = move(bot, {}) assert next_move == (5, 2)
def test_will_not_do_kamikaze(self): # check that we eat the last food when adjacent layout = """ ######## #x a.## # . b # ######## """ bot = setup_test_game(layout=layout, is_blue=True, bots={'y': (5, 1)}) next_pos = smart_eating_player(bot, {}) assert next_pos == (4, 1)
def test_eat_food(self): # check that we eat the last food when adjacent layout = """ ######## #x a.## # . by# ######## """ bot = setup_test_game(layout=layout, is_blue=True) next_pos = smart_eating_player(bot, {}) assert next_pos == (5, 1)
def test_move_towards_food(self): # check that we move closer to the food layout = """ ######## #y a .## #b.x # ######## """ bot = setup_test_game(layout=layout, is_blue=True) next_pos = smart_eating_player(bot, {}) assert next_pos == (4, 1)
def test_legalmoves(self): # check that the only two valid moves are returned layout = """ ######## #a###### #b. .xy# ######## """ bot = setup_test_game(layout=layout, is_blue=True) next_pos = smart_eating_player(bot, {}) assert next_pos in ((1, 2), (1, 1))
def test_no_kamikaze_stop(self): # Check that we stop if escaping would kill us layout = """ ######## # ###.# #b. xay# ######## """ bot = setup_test_game(layout=layout, is_blue=True) next_pos = smart_random_player(bot, {}) assert next_pos == (5, 2)
def test_eat_food(self): # check that we indeed collect food when possible layout = """ ######## #y # .## #b.x a # ######## """ bot = setup_test_game(layout=layout, is_blue=True) next_pos = smart_random_player(bot, {}) assert next_pos == (5, 1)
def test_kill_enemy(self): # check that we indeed kill an enemy when possible layout = """ ######## #x###.## #a. by# ######## """ bot = setup_test_game(layout=layout, is_blue=True) next_pos = smart_random_player(bot, {}) assert next_pos == (1, 1)
def test_only_move_unless_blocked(self): # test that the bot only moves forward layout = """ ######## #y #.### #b.x#a # ######## """ bot = setup_test_game(layout=layout, is_blue=True) bot.track = [(6, 2), (5, 2)] assert bot.position == (5, 2) next_pos = nq_random_player(bot, {}) assert next_pos == (6, 2)
def test_legalmoves(self): # check that the only two valid moves are always returned # we try ten times, to test 10 different random streams layout = """ ######## #a###### #b. .xy# ######## """ for i in range(10): bot = setup_test_game(layout=layout, is_blue=True) next_pos = smart_random_player(bot, {}) assert next_pos in ((1, 2), (1, 1))