generated from BattlesnakeOfficial/starter-snake-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
108 lines (82 loc) · 3.61 KB
/
main.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# Welcome to https://gevecht-slang.the-serious-pro.repl.co
# __________ __ __ .__ __
# \______ \_____ _/ |__/ |_| | ____ ______ ____ _____ | | __ ____
# | | _/\__ \\ __\ __\ | _/ __ \ / ___// \\__ \ | |/ // __ \
# | | \ / __ \| | | | | |_\ ___/ \___ \| | \/ __ \| <\ ___/
# |________/(______/__| |__| |____/\_____>______>___|__(______/__|__\\_____>
#
# This file can be a nice home for your Battlesnake logic and helper functions.
#
# To get you started we've included code to prevent your Battlesnake from moving backwards.
# For more info see docs.battlesnake.com
import random
import typing
# info is called when you create your Battlesnake on play.battlesnake.com
# and controls your Battlesnake's appearance
# TIP: If you open your Battlesnake URL in a browser you should see this data
def info() -> typing.Dict:
print("INFO")
return {
"apiversion": "1",
"author": "the-serious-programmer",
"color": "#000000",
"head": "beluga",
"tail": "bolt",
}
# start is called when your Battlesnake begins a game
def start(game_state: typing.Dict):
print("GAME START")
# end is called when your Battlesnake finishes a game
def end(game_state: typing.Dict):
print("GAME OVER\n")
# 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(game_state: typing.Dict) -> typing.Dict:
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"
board_width = int(game_state['board']['width'])
board_height = int(game_state['board']['height'])
if my_neck["x"] < my_head["x"] or board_width == 0: # Neck is left of head, don't move left
is_move_safe["left"] = False
elif my_neck["x"] > my_head["x"] or board_width == 10: # Neck is right of head, don't move right
is_move_safe["right"] = False
elif my_neck["y"] < my_head["y"] or board_height == 10: # Neck is below head, don't move down
is_move_safe["down"] = False
elif my_neck["y"] > my_head["y"] or board_height == 0: # 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 3 - Prevent your Battlesnake from colliding with other Battlesnakes
# opponents = game_state['board']['snakes']
# Are there any safe moves left?
safe_moves = []
for move, isSafe in is_move_safe.items():
if isSafe:
safe_moves.append(move)
if len(safe_moves) == 0:
random_move = random.choice([is_move_safe.keys()])
print(f"MOVE {game_state['turn']}: No safe moves detected! Moving {random_move}")
return {"move": random_move}
# Choose a random move from the safe ones
next_move = random.choice(safe_moves)
# TODO: Step 4 - Move towards food instead of random, to regain health and survive longer
# food = game_state['board']['food']
print(f"MOVE {game_state['turn']}: {next_move}")
return {"move": next_move}
# Start server when `python main.py` is run
if __name__ == "__main__":
from server import run_server
run_server({
"info": info,
"start": start,
"move": move,
"end": end
})