1  Snake Game

Overview

This is an asynchronous Snake Game environment designed for reinforcement learning or AI experiments. The snake moves automatically based on selected actions, and the goal is to maximize the score by eating food while avoiding collisions with walls or itself.

NOTICE

The game is developed with Unity and exported to WebGL, in order to be able to control it, You would need to first, make unityInstance visible by browser globally.

window.gameInstance = unityInstance


Functions

move

Input

This function accepts a number between [0, 1, 2] as input.

0 — Step Front

1 — Step Right

2 — Step Left

Please notice that the the default choice is set to move right.

Output

This function returns a list of 3 numbers as output.

[game_over, score, reward] = window.action_result.

Details game_over (int)1 if the game ends after this move; otherwise 0.

window.score (int) — Current score after the move.

window.reward (int)+10 if the snake eats a food, -10 if the snake collides with walls or itself; otherwise 0.

Example

window.gameInstance.SendMessage("Snake", "Move", Number(0));

[game_over, score, reward] = window.action_result;

get_state

Input

This function doesn’t accept any input. You would be able to access the states by window.current_state.

Output

This function returns a list of 11 numbers as output.

Example

window.gameInstance.SendMessage("Snake", "GetState");

current_state = window.current_state;
Index Feature Description
0 Danger Front Immediate danger straight ahead
1 Danger Right Immediate danger to the right
2 Danger Left Immediate danger to the left
3 Moving Left Snake currently moving left
4 Moving Right Snake currently moving right
5 Moving Up Snake currently moving up
6 Moving Down Snake currently moving down
7 Food Left Food is located to the left
8 Food Right Food is located to the right
9 Food Up Food is located above
10 Food Down Food is located below

Episodes and Scoring

  • Each training/test session consists of 100 episodes (games).
  • The metric considered in Leaderboard is Average Score and is calculated as:
    • Average_Score = Accumulative Score / Number of Episodes

To access scoring data, you can have a look at the following:

window.avg_score

window.score

window.best_score

window.step

window.episode

window.max_episode

window.max_possible_step