From eb73a77e3c46e2216f7ced92ec5675ef3cc4b578 Mon Sep 17 00:00:00 2001 From: Lee Gauthier Date: Sat, 21 Feb 2015 17:18:53 -0800 Subject: [PATCH] refactor movement --- app/main.py | 48 +++++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/app/main.py b/app/main.py index e379c6542..a5ae831d6 100644 --- a/app/main.py +++ b/app/main.py @@ -122,30 +122,10 @@ def get_possible_directions(nagini, board, bounds): return directions -@bottle.post('/move') -def move(): - data = bottle.request.json - - bounds = { - "up": 1, - "down": len(data['board'][0]) - 2, - "right": len(data['board']) - 2, - "left": 1, - } - - nagini = [s for s in data['snakes'] if s['name'] == 'nagini'][0] +def move_edge(nagini, bounds): coords = nagini['coords'] head_x, head_y = coords[0][0], coords[0][1] - directions = get_possible_directions(nagini, data['board'], bounds) - if not directions: - # Commit suicide honorably so as not to give any victories to - # the other inferior snakes! - return json.dumps({ - 'move': seppuku(nagini, data['board']), - 'taunt': 'You will always remember this as the day you almost caught Captain Jack Sparrow!' - }) - # If not on an edge, start moving towards one if not any([head_x in (bounds['left'], bounds['right']), head_y in (bounds['up'], bounds['down'])]): edge_distances = [ @@ -174,6 +154,32 @@ def move(): else: direction = UP + return direction + +@bottle.post('/move') +def move(): + data = bottle.request.json + + bounds = { + "up": 1, + "down": len(data['board'][0]) - 2, + "right": len(data['board']) - 2, + "left": 1, + } + + nagini = [s for s in data['snakes'] if s['name'] == 'nagini'][0] + + directions = get_possible_directions(nagini, data['board'], bounds) + if not directions: + # Commit suicide honorably so as not to give any victories to + # the other inferior snakes! + return json.dumps({ + 'move': seppuku(nagini, data['board']), + 'taunt': 'You will always remember this as the day you almost caught Captain Jack Sparrow!' + }) + + direction = move_edge(nagini, bounds) + food = [x for x, food in directions if food] if food: