Beispiel #1
0
def test_loop():
    game = Game(8, 8)
    game.add_snake(LoopSnake())
    for _board in game.run():
        pass
    assert_killed(game, "Starvation!")
    assert game.turn_count > game.MAX_HEALTH
Beispiel #2
0
def test_wall():
    game = Game(8, 8)
    game.add_snake(IterSnake(repeat("left")))
    for _board in game.run():
        pass
    assert game.turn_count == 7
    assert_killed(game, "Moved out of bounds")
def test_game():
    game = Game(20, 20)
    game.add_snake(GenetiSnake(move_food))
    game.add_snake(GenetiSnake(move_food))
    for _board in game.run():
        if game.turn_count > game.MAX_HEALTH:
            break
    assert game.turn_count > 5
def test_game_greedy():
    game = Game(20, 20)
    game.add_snake(GenetiSnake(move_space))
    game.add_snake(GenetiSnake(move_food))
    game.add_snake(GenetiSnake(move_killer))
    game.add_snake(GenetiSnake(move_afraid))
    for _board in game.run():
        if game.turn_count > game.MAX_HEALTH:
            break
    assert game.turn_count > game.MAX_HEALTH
def cli(max_turns):
    game = Game(20, 20)
    game.add_snake(GenetiSnake(move_space))
    game.add_snake(GenetiSnake(move_food))
    game.add_snake(GenetiSnake(move_killer))
    game.add_snake(GenetiSnake(move_afraid))
    for _board in game.run():
        print "game turn=%s" % game.turn_count
        print game

        if max_turns and game.turn_count > max_turns:
            break
def test_collision_3():
    game = Game(8, 8)
    game.add_snake(GenetiSnake(move_food))
    game.add_snake(GenetiSnake(move_food))
    game.add_snake(GenetiSnake(move_food))
    for board in game.run():
        if game.turn_count == 0:
            game.food = [board.index(4, 4)]
            game.food_count = 1
            game.snakes[0].body = [board.index(4, 3)]
            game.snakes[1].body = [board.index(4, 5)]
            game.snakes[2].body = [board.index(3, 4)]
    assert_killed(game, A="Ran into B!", B="Ran into A!", C="Ran into A!")
Beispiel #7
0
def test_collision():
    game = Game(8, 8)
    game.add_snake(IterSnake(repeat("left")))
    game.add_snake(IterSnake(repeat("right")))
    for _board in game.run():
        pass
    assert_killed(game, "Ran into B!", "Ran into A!")
Beispiel #8
0
def move(snake=None):
    #LOG.debug("move: request=%s", request)

    # See https://stembolthq.github.io/battle_snake/
    # board	Array<Array<BoardCell>>	Two dimensional representation of the board
    # food	Array<Point>	Array of all food currently on the board
    # game_id	UUID
    # height	integer
    # width	integer
    # snakes	Array<Snake>	Array of all living snakes in the game
    #   coords	 	Array<Point>
    #   health_points	 	0..100
    #   id	 	UUID
    #   name	 	string
    #   taunt	 	string
    # turn	integer	The current turn.
    # you	UUID	A reference to your snake's id, the snake object can be found in snakes.
    request.get_data() # get all request data and cache it
    data = request.json

    if not snake:
        snake=BEST_SNAKE
    
    if "board" in data:
        del data["board"]

    LOG.debug("move: pid=%s turn=%s content_length=%s",
              os.getpid(), data["turn"],
              request.headers.get('content-length'))

    # convert the Battlesnake server structure into my Game
    width, height = (int(data[x]) for x in ("width", "height"))
    game = Game(width, height)
    board = game.render()
    game.food = [board.index(*xy) for xy in data["food"]]
    game.turn_count = int(data["turn"])
    self_index = None
    for idx, data_snake in enumerate(data["snakes"]):
        player = game.add_snake(data_snake, data_snake["id"])
        player.body = [board.index(*xy) for xy in data_snake["coords"]]
        player.health = data_snake["health_points"]
        if player.name == data["you"]:
            self_index = idx
    board = game.render()
    move_name = snake.move(game, board, self_index)

    LOG.debug("move: move=%s", move_name)

    return jsonify({
        "move": move_name,
        "taunt": taunt(),
        })