Turn: 0
How to Play as Human
Use W (up), S (down), A (left), D (right) keys on your keyboard to control your snake's direction each turn.
Sample Bot Code (Python) ▾
import sys
import json
import random
from collections import deque
# Constants
MAXN = 25
DX = [-1, 0, 1, 0]
DY = [0, 1, 0, -1]
# Global variables
invalid = [[False] * MAXN for _ in range(MAXN)]
snake = [deque(), deque()] # 0: self, 1: opponent
possible_dire = []
n = m = 0
def whether_grow(num):
return num <= 24 or (num - 24) % 3 == 0
def delete_end(id):
snake[id].pop()
def move(id, dire, num):
head = snake[id][0]
x, y = head[0] + DX[dire], head[1] + DY[dire]
snake[id].appendleft((x, y))
if not whether_grow(num):
delete_end(id)
def is_in_body(x, y):
for s in snake:
if (x, y) in s:
return True
return False
def valid_direction(id, k):
head = snake[id][0]
x, y = head[0] + DX[k], head[1] + DY[k]
if not (1 <= x <= n and 1 <= y <= m):
return False
if invalid[x][y] or is_in_body(x, y):
return False
return True
def main():
global n, m
input_str = input()
input_data = json.loads(input_str)
first_req = input_data['requests'][0]
n, m = first_req['height'], first_req['width']
x = first_req['x']
# Init snakes
if x == 1:
snake[0].appendleft((1, 1))
snake[1].appendleft((n, m))
else:
snake[1].appendleft((1, 1))
snake[0].appendleft((n, m))
# Process obstacles
for obs in first_req.get('obstacle', []):
invalid[obs['x']][obs['y']] = True
# Replay moves
total = len(input_data['responses'])
for i in range(total):
move(0, input_data['responses'][i]['direction'], i)
move(1, input_data['requests'][i + 1]['direction'], i)
if not whether_grow(total):
delete_end(0)
delete_end(1)
# Collect valid directions
for k in range(4):
if valid_direction(0, k):
possible_dire.append(k)
# If no valid directions found, allow all directions
if len(possible_dire) == 0:
for k in range(4):
possible_dire.append(k)
random.seed(time.time() + total)
ret = {'response': {'direction': random.choice(possible_dire)}}
print(json.dumps(ret))
if __name__ == '__main__':
import time
main()
Regulations ▾
Unlike the traditional single-player Snake game, this version is a two-player competitive game where both players simultaneously make decisions each turn to control their own snake.
Players control a snake on an n*m grid. A snake is defined as a finite, ordered sequence of unique coordinates, where each adjacent pair of coordinates in the sequence are also adjacent on the grid (i.e., they differ by one unit in either the x or y direction). The first coordinate in the sequence represents the snake's head. Players can only control the direction of the snake's head (East, South, West, or North). The snake moves at a constant speed: each move inserts a new head coordinate in the current direction and removes the last coordinate (tail) of the sequence.
The initial positions of the snakes are at the top-left corner ([1,1]) and the bottom-right corner ([n,m]) of the grid, respectively. Each snake starts with a length of 1. Unlike the traditional version, there are no food pellets on the map. Instead, the snake grows automatically: for the first 25 turns, the snake grows by one unit per turn (i.e., the tail is not removed), and from turn 26 onwards, it grows by one unit every 3 turns.
The map consists of an n*m grid made up of 1*1 grass tiles and obstacles. The map is centrally symmetric.
A snake is considered dead if its head moves into an invalid position or if the input/output format is incorrect. Invalid positions include: out of bounds, an obstacle tile, the body of its own snake (excluding the head), or the body of the opponent's snake. The game ends immediately when any snake dies. If both snakes die at the same time, the game is a draw. Otherwise, the first to die loses, and the other wins.
The values of n and m are randomly chosen: n is an integer in the range [10, 17], and m is in [10, 13]. Player programs must handle all valid combinations of n and m. The game guarantees that n + m is always an odd number.