generated from BattlesnakeOfficial/starter-snake-python
-
Notifications
You must be signed in to change notification settings - Fork 4
/
snake_lucas.py
90 lines (67 loc) · 3 KB
/
snake_lucas.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import random
import typing
import json
from pathlib import Path
def info_lucas():
return {
"apiversion": "1",
"author": "lucas", # TODO: Your Battlesnake Username
"color": "#ff0000", # TODO: Choose color
"head": "tongue", # TODO: Choose head
"tail": "hook", # TODO: Choose tail
}
# move is called on every turn and returns your next move
# Valid moves are "up", "down", "left", or "right"
# See https://docs.battlesnake.com/api/example-move for available data
def move_lucas(game_state: typing.Dict) -> typing.Dict:
logFileName = "logs/turn_" + str(game_state["turn"]) + ".json"
logFilePath = Path(__file__).parent / logFileName
json_file = open(logFilePath, "w")
json.dump(game_state, json_file, indent=4)
json_file.close()
is_move_safe = {"up": True, "down": True, "left": True, "right": True}
# We've included code to prevent your Battlesnake from moving backwards
my_head = game_state["you"]["body"][0] # Coordinates of your head
my_neck = game_state["you"]["body"][1] # Coordinates of your "neck"
if my_neck["x"] < my_head["x"]: # Neck is left of head, don't move left
is_move_safe["left"] = 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 below head, don't move down
is_move_safe["down"] = False
elif my_neck["y"] > my_head["y"]: # Neck is above head, don't move up
is_move_safe["up"] = False
# TODO: Step 1 - Prevent your Battlesnake from moving out of bounds
board_width = game_state['board']['width']
board_height = game_state['board']['height']
if (my_head["x"] >= board_width):
is_move_safe["right"] = False
if (my_head["x"] <= 0):
is_move_safe["left"] = False
if (my_head["y"] >= board_height):
is_move_safe["down"] = False
if (my_head["x"] <= 0 ):
is_move_safe["up"] = False
# TODO: Step 2 - Prevent your Battlesnake from colliding with itself
# my_body = game_state['you']['body']
# TODO: Step 3 - Prevent your Battlesnake from colliding with other Battlesnakes
# opponents = game_state['board']['snakes']
# Are there any safe moves left?
safe_moves = []
for move, is_safe in is_move_safe.items():
if is_safe:
safe_moves.append(move)
if len(safe_moves) == 0:
print(f"MOVE {game_state['turn']}: No safe moves detected! Moving down")
return {"move": "down"}
# Choose a random move from the safe ones
next_move = random.choice(safe_moves)
# Step 4 - Move towards food instead of random, to regain health and survive longer
print(f"MOVE {game_state['turn']}: {next_move}")
return {"move": next_move}
if __name__ == "__main__":
dataFilePath = Path(__file__).parent / "snake_lucas.json"
rhandle = open(dataFilePath, "r")
gamestate = json.load(rhandle)
rhandle.close()
print(move_lucas(gamestate))