add versions to snakes and read it in server class
This commit is contained in:
+38
-36
@@ -3,53 +3,55 @@ from snakes.TemplateSnake import TemplateSnake
|
||||
import random
|
||||
|
||||
class DummSnake(TemplateSnake):
|
||||
def choose_move(self, data: dict) -> str:
|
||||
is_move_safe = {"up": True, "down": True, "left": True, "right": True}
|
||||
VERSION = "1.0.0"
|
||||
|
||||
# We've included code to prevent your Battlesnake from moving backwards
|
||||
my_head = data["you"]["body"][0] # Coordinates of your head
|
||||
my_neck = data["you"]["body"][1] # Coordinates of your "neck"
|
||||
def choose_move(self, data: dict) -> str:
|
||||
is_move_safe = {"up": True, "down": True, "left": True, "right": True}
|
||||
|
||||
if my_neck["x"] < my_head["x"]: # Neck is left of head, don't move left
|
||||
is_move_safe["left"] = False
|
||||
# We've included code to prevent your Battlesnake from moving backwards
|
||||
my_head = data["you"]["body"][0] # Coordinates of your head
|
||||
my_neck = data["you"]["body"][1] # Coordinates of your "neck"
|
||||
|
||||
elif my_neck["x"] > my_head["x"]: # Neck is right of head, don't move right
|
||||
is_move_safe["right"] = False
|
||||
if my_neck["x"] < my_head["x"]: # Neck is left of head, don't move left
|
||||
is_move_safe["left"] = False
|
||||
|
||||
elif my_neck["y"] < my_head["y"]: # Neck is below head, don't move down
|
||||
is_move_safe["down"] = False
|
||||
elif my_neck["x"] > my_head["x"]: # Neck is right of head, don't move right
|
||||
is_move_safe["right"] = False
|
||||
|
||||
elif my_neck["y"] > my_head["y"]: # Neck is above head, don't move up
|
||||
is_move_safe["up"] = False
|
||||
elif my_neck["y"] < my_head["y"]: # Neck is below head, don't move down
|
||||
is_move_safe["down"] = False
|
||||
|
||||
# TODO: Step 1 - Prevent your Battlesnake from moving out of bounds
|
||||
# board_width = game_state['board']['width']
|
||||
# board_height = game_state['board']['height']
|
||||
elif my_neck["y"] > my_head["y"]: # Neck is above head, don't move up
|
||||
is_move_safe["up"] = False
|
||||
|
||||
# TODO: Step 2 - Prevent your Battlesnake from colliding with itself
|
||||
# my_body = game_state['you']['body']
|
||||
# TODO: Step 1 - Prevent your Battlesnake from moving out of bounds
|
||||
# board_width = game_state['board']['width']
|
||||
# board_height = game_state['board']['height']
|
||||
|
||||
# TODO: Step 3 - Prevent your Battlesnake from colliding with other Battlesnakes
|
||||
# opponents = game_state['board']['snakes']
|
||||
# TODO: Step 2 - Prevent your Battlesnake from colliding with itself
|
||||
# my_body = game_state['you']['body']
|
||||
|
||||
# Are there any safe moves left?
|
||||
safe_moves = []
|
||||
for move, isSafe in is_move_safe.items():
|
||||
if isSafe:
|
||||
safe_moves.append(move)
|
||||
# TODO: Step 3 - Prevent your Battlesnake from colliding with other Battlesnakes
|
||||
# opponents = game_state['board']['snakes']
|
||||
|
||||
if len(safe_moves) == 0:
|
||||
print(f"MOVE {data['turn']}: No safe moves detected! Moving down")
|
||||
self.add_to_history({"my_head": my_head, "my_neck": my_neck, "move": move, "safe_moves": safe_moves, "is_move_safe": is_move_safe})
|
||||
return {"move": "down"}
|
||||
# Are there any safe moves left?
|
||||
safe_moves = []
|
||||
for move, isSafe in is_move_safe.items():
|
||||
if isSafe:
|
||||
safe_moves.append(move)
|
||||
|
||||
# Choose a random move from the safe ones
|
||||
move = random.choice(safe_moves)
|
||||
if len(safe_moves) == 0:
|
||||
print(f"MOVE {data['turn']}: No safe moves detected! Moving down")
|
||||
self.add_to_history({"my_head": my_head, "my_neck": my_neck, "move": move, "safe_moves": safe_moves, "is_move_safe": is_move_safe})
|
||||
return {"move": "down"}
|
||||
|
||||
# TODO: Step 4 - Move towards food instead of random, to regain health and survive longer
|
||||
# food = game_state['board']['food']
|
||||
# Choose a random move from the safe ones
|
||||
move = random.choice(safe_moves)
|
||||
|
||||
self.add_to_history({"my_head": my_head, "my_neck": my_neck, "move": move, "safe_moves": safe_moves, "is_move_safe": is_move_safe})
|
||||
print(f"{data['game']['id']} MOVE {data['turn']}: {move} picked from all valid options in {is_move_safe}")
|
||||
# TODO: Step 4 - Move towards food instead of random, to regain health and survive longer
|
||||
# food = game_state['board']['food']
|
||||
|
||||
return move
|
||||
self.add_to_history({"my_head": my_head, "my_neck": my_neck, "move": move, "safe_moves": safe_moves, "is_move_safe": is_move_safe})
|
||||
print(f"{data['game']['id']} MOVE {data['turn']}: {move} picked from all valid options in {is_move_safe}")
|
||||
|
||||
return move
|
||||
|
||||
Reference in New Issue
Block a user