Beispiel #1
0
def start():
    """
    Called every time a new Battlesnake game starts and your snake is in it.
    Your response will control how your snake is displayed on the board.
    """
    data = bottle.request.json
    print("START:", json.dumps(data))
    
    global dirs
    dirs = {Loc(-1,0):'left',Loc(0,1):'down',Loc(1,0):'right',Loc(0,-1):'up'}
    global H
    global W
    global G
    global finite_snake_machine
    
    print(dirs)
    
    
    H, W = data["board"]["height"], data["board"]["width"]
    G = graph.Graph(W,H)
    
    finite_snake_machine = FSM()

    response = {"color": "#ffccff", "smile": "regular", "tailType": "round-bum"}
    return HTTPResponse(
        status=200,
        headers={"Content-Type": "application/json"},
        body=json.dumps(response),
    )
Beispiel #2
0
 def test_movie_grossing(self):
     test_dictionary = helper.Helper().load_structure('test.json')
     graph = g.Graph(test_dictionary)
     answer = 52100000.0
     actual = graph.movie_grossing('the finest hours')
     self.assertEqual(answer, actual, 'movie grossing is wrong')
Beispiel #3
0
def move():
    """
    Called when the Battlesnake Engine needs to know your next move.
    The data parameter will contain information about the board.
    Your response must include your move of up, down, left, or right.
    """
    t = time.time()
    
    data = bottle.request.json

    # Choose a random direction to move in
    global dirs
    global H
    global W
    global finite_snake_machine
    H, W = data["board"]["height"], data["board"]["height"]
    """
    TODO: Using the data from the endpoint request object, your
            snake AI must choose a direction to move in.
    """
        
    me = snk.Snake(data['you'])
    snakes = build_snakes(data, me_id=me.id)
    
    food = to_loc_list(data["board"]["food"])

    G = graph.Graph(H,W)
    G.build_from_data(snakes, me)
    
    d = None
    #state parameters
    params = [0,0,0,0,0,0]
    
    #init fsm
    ### Back code to make the fsm work with the following code
    finite_snake_machine = FSM()
    to_kill = None
    to_run = None

    if me.health >= 50:
        for snake in snakes:
            if G.floodfind(me.head,snake.head):
                params[1] = True
                finite_snake_machine.current_state = finite_snake_machine.states['kill']
                to_kill = snake
    
    #approaching wall
    forward = G.floodfill(me.head, me.head_direction)
    left = G.floodfill(me.head,me.head_direction.rotate_90())
    right = G.floodfill(me.head,me.head_direction.rotate_90(clockwise=True))
    
    snakes_near_me = []
    
    for snake in snakes:
        if me.head.square_dist(snake.head) == 2:
            #snake head in the freaking way
            snakes_near_me.append(snake)
            if me.size > snake.size:
                #we can go head to freakin head
                d = G.Astar(me.head,snake.head)
                move = dirs[d[1]-d[0]]
                break
            
        
    #TODO: figure out how to get this to work: approaching wall shit
    if right+left+forward < 3*me.size:
        #go in direction of largest area
        if right >= left and right >= forward:
            d = me.head_direction.rotate_90(clockwise=True)
            move = dirs[d]
        elif left >= right and left >= forward:
            d = me.head_direction.rotate_90()
            move = dirs[d]
        elif forward >= right and forward >= left:
            d = me.head_direction
            move = dirs[d]
    else:
        if right < me.size and left < me.size:
            d = me.head_direction
            move = dirs[d]
        elif right < me.size and forward < me.size:
            d = me.head_direction.rotate_90()
            move = dirs[d]
        elif left < me.size and forward < me.size:
            d = me.head_direction.rotate_90(clockwise=True)
            move = dirs[d]
    
    if type(d) == type(None):
        #move based off state
        #d = G.Astar(me.head, food[0])
        
        #assess situation
        
        #TODO: how to check if hungry
        #Option 1: threshold hunger level; less computation
        #option 2: distance-based hunger; more computation
        
        #going with option 1 as a placeholder
        if me.health < 50:
            #me hungy
            params[0] = True
        else:
            params[0] = False
            
        
        #food available
        params[2] = G.floodfind(me.head,food)
        #tail available
        params[3] = G.floodfind(me.head,me.tail)
        #other available
        params[4] = G.floodfind(me.head,[s.tail for s in snakes])
        
        
        #put into binary string
        params_bin = str(int(params[0]))+str(int(params[1]))+str(int(params[2]))+str(int(params[3]))+str(int(params[4]))+str(int(params[5]))
        #turn binary string into respective base 10 number
        print(params_bin)
        params_bin = int(params_bin,2)
        
        finite_snake_machine.next_state(params_bin)
        print(finite_snake_machine.current_state)
        
        #take action
        d = finite_snake_machine.current_state.action(G, snakes, me, food, to_kill)
        
        #check if im running into danger:
        for snake in snakes_near_me:
            if snake.head.square_dist(d[len(d)-1]) == 2:
                #run
                
                
        
        if d == None:
            #find tail
            d = G.Astar(me.head, me.tail)
    
        
        #TODO:
        #the first few turns the snake tries to chase its tail, but its tail
        #is inside its body, so it collides with itself
        if type(d) == type(None):
            move = dirs[me.head_direction]
        else:
            if len(d) <= 1:
                move = 'up'
            else:
                move = dirs[d[1]-d[0]]
    #TODO: handle if location is none
    


    # Shouts are messages sent to all the other snakes in the game.
    # Shouts are not displayed on the game board.
    shout = "I am a python snake!"
    
    print(time.time()-t)

    response = {"move": move, "shout": shout}
    return HTTPResponse(
        status=200,
        headers={"Content-Type": "application/json"},
        body=json.dumps(response),
    )
Beispiel #4
0
 def test_movies_given_year(self):
     test_dictionary = helper.Helper().load_structure('test.json')
     graph = g.Graph(test_dictionary)
     answer = [u'the benefactor']
     actual = graph.movies_given_year(2015)
     self.assertEqual(answer, actual, 'movies given year is wrong')
Beispiel #5
0
 def test_actors_given_year(self):
     test_dictionary = helper.Helper().load_structure('test.json')
     graph = g.Graph(test_dictionary)
     answer = [u'dakota fanning', u'richard gere', u'theo james', u'clarke peters']
     actual = graph.actors_given_year(2015)
     self.assertEqual(answer, actual, 'actors given year is wrong')
Beispiel #6
0
 def test_oldest_x_actors(self):
     test_dictionary = helper.Helper().load_structure('test.json')
     graph = g.Graph(test_dictionary)
     answer = [u'fred willard', u'charles dance', u'jeremy irons']
     actual = graph.oldest_x_actors(3)
     self.assertEqual(answer, actual, 'oldest actors is wrong')
Beispiel #7
0
 def test_calculate_value(self):
     test_dictionary = helper.Helper().load_structure('test.json')
     graph = g.Graph(test_dictionary)
     answer = 56700000.0
     actual = graph.calculate_value('owen wilson')
     self.assertEqual(answer, actual, 'calculate value is wrong')
Beispiel #8
0
 def test_top_x_actors(self):
     test_dictionary = helper.Helper().load_structure('test.json')
     graph = g.Graph(test_dictionary)
     answer = [u'dakota johnson', u'chlo grace moretz', u'owen wilson']
     actual = graph.top_x_actors(3)
     self.assertEqual(answer, actual, 'top actors is wrong')
Beispiel #9
0
 def test_actors_in_movie(self):
     test_dictionary = helper.Helper().load_structure('test.json')
     graph = g.Graph(test_dictionary)
     answer = [u'chris pine', u'casey affleck', u'ben foster', u'holliday grainger', u'john ortiz', u'eric bana']
     actual = graph.actors_in_movie('the finest hours') 
     self.assertEqual(answer, actual, 'actors in movie is wrong')
Beispiel #10
0
 def test_movies_worked_in(self):
     test_dictionary = helper.Helper().load_structure('test.json')
     graph = g.Graph(test_dictionary)
     answer = [u'zoolander 2']
     actual = graph.movies_worked_in('owen wilson')
     self.assertEqual(answer, actual, 'movies actor worked is wrong')