Skip to content

Commit d5c1b91

Browse files
implement Move() in main
1 parent 90bd10c commit d5c1b91

1 file changed

Lines changed: 5 additions & 48 deletions

File tree

main.py

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import random
1414
import typing
15-
15+
from move import Move
1616

1717
# info is called when you create your Battlesnake on play.battlesnake.com
1818
# and controls your Battlesnake's appearance
@@ -43,53 +43,10 @@ def end(game_state: typing.Dict):
4343
# Valid moves are "up", "down", "left", or "right"
4444
# See https://docs.battlesnake.com/api/example-move for available data
4545
def move(game_state: typing.Dict) -> typing.Dict:
46-
47-
is_move_safe = {"up": True, "down": True, "left": True, "right": True}
48-
49-
# We've included code to prevent your Battlesnake from moving backwards
50-
my_head = game_state["you"]["body"][0] # Coordinates of your head
51-
my_neck = game_state["you"]["body"][1] # Coordinates of your "neck"
52-
53-
if my_neck["x"] < my_head["x"]: # Neck is left of head, don't move left
54-
is_move_safe["left"] = False
55-
56-
elif my_neck["x"] > my_head["x"]: # Neck is right of head, don't move right
57-
is_move_safe["right"] = False
58-
59-
elif my_neck["y"] < my_head["y"]: # Neck is below head, don't move down
60-
is_move_safe["down"] = False
61-
62-
elif my_neck["y"] > my_head["y"]: # Neck is above head, don't move up
63-
is_move_safe["up"] = False
64-
65-
# TODO: Step 1 - Prevent your Battlesnake from moving out of bounds
66-
# board_width = game_state['board']['width']
67-
# board_height = game_state['board']['height']
68-
69-
# TODO: Step 2 - Prevent your Battlesnake from colliding with itself
70-
# my_body = game_state['you']['body']
71-
72-
# TODO: Step 3 - Prevent your Battlesnake from colliding with other Battlesnakes
73-
# opponents = game_state['board']['snakes']
74-
75-
# Are there any safe moves left?
76-
safe_moves = []
77-
for move, isSafe in is_move_safe.items():
78-
if isSafe:
79-
safe_moves.append(move)
80-
81-
if len(safe_moves) == 0:
82-
print(f"MOVE {game_state['turn']}: No safe moves detected! Moving down")
83-
return {"move": "down"}
84-
85-
# Choose a random move from the safe ones
86-
next_move = random.choice(safe_moves)
87-
88-
# TODO: Step 4 - Move towards food instead of random, to regain health and survive longer
89-
# food = game_state['board']['food']
90-
91-
print(f"MOVE {game_state['turn']}: {next_move}")
92-
return {"move": next_move}
46+
47+
bot = Move()
48+
49+
return bot.choose_move(game_state)
9350

9451

9552
# Start server when `python main.py` is run

0 commit comments

Comments
 (0)