Assignment #1: Snake AI
Game AI Programming, IS53049A (2018/2019), Alan Zucconi
Overview
The purpose of this assignment is to write an AI that plays the game Snake, using behaviour trees. The assignment must be completed using the Snake simulator and the behaviour tree library available on Learn.Gold .
Snake has been chosen because of several reasons. First of all, it is a very popular game most people are familiar with. Most importantly, is relatively easy to create a decent AI to play it, but is exceptionally challenging to play it optimally.
Table of Content? Overview Table of Content Deliverables ?? Unity Package ?? Report ?? Video The Game Addressing Cells Actions Perceptions Getting Started Importing the project Testing the project Setting up your AI Using the Snake Simulator Writing your Snake AI Conditionals Random Actions Custom Conditions Common Issues Submitting your Snake AI Testing your AI Exporting your Snake AI Snake API?
Deliverables
The submission consists of three deliverables:
A Unity package which contains the original C# script you wrote, and the scriptable object which was used to run the Snake simulator. The correct procedure to export the Unity package is explained in the section ¶ “Submitting your Snake AI”.
? Only one Snake AI per student is allowed.
? Submissions are tested automatically. If you do not comply with the submission instructions, your AI will not be tested and this will heavily penalise your work.
You will need to:
- Install Unity
- Download the Unity project from Learn.Gold
- Create your own Snake AI script and scriptable object (¶ “Setting up your AI”)
- Use a behaviour tree to control the Snake movement (¶ “Writing your Snake AI”)
- Test the performance of your Snake AI (¶ “Testing your AI”)
- Export your files as Unity package (¶ “Exporting your Snake AI”)
- Upload the Unity package on Learn.Gold
Report
A short report (2-5 pages of text), in PDF format, which explains how your solution works and how it was implemented. This is also your chance to critically discuss when and why your AI fails, and how you could fix that. Do not forget to add references and bibliography, shall you need it.
Optional: you can add a diagram to visualise your behaviour tree, and a screenshot of the AI performance plot generated from the Snake simulator (see ¶ “Testing your AI”). Anything else that can improve the report presentation (cover, table of content, bibliography, …) is welcome.
? Grammar is not assessed, but typos will penalise your work: check your spelling.
Video
A short video (1-5 minutes) that shows a few rounds of your AI playing Snake.
You can simply record the Unity editor while running the Snake simulator.
Optional: you can add text or voice over to explain what is happening and how your solution works. You can also explain your code and design decisions.
? Spelling and pronunciations are not assessed
The Game
- Snake is a game played on a grid.
- The snake starts with length 1, which increases every time some food is eaten.
- There is only one piece of food in the world at any time.
- The snake has a direction or movement (North, East, South or West), and continues travelling in that direction if no action is taken.
The Unity package contains a class called SnakeGame where the logic of the game is implemented. You do not need to open or read this class, as it uses many advanced features you might not be familiar with. However, you will need to use some of its methods and properties to build you Snake AI (see ¶ “ Snake API ” for a full list of what you can use).
Addressing Cells
Like every AI agent that performs some sort of decision making, the snake has actions (things it can do) and perceptions (things it can see). The snake has a limited vision, and it can only see what is inside the four cells immediately adjacent to its head.
There are two ways to
address the cells around the head: relative and absolute (table below):
Actions
The only actions the snake can perform are about changing its direction of travelling. The snake can also refuse to take a decision: in that case, it will simply keep travelling along its current direction of movement.
When you are playing Snake with the keyboard, you can use the arrow keys to change its direction in one of the cardinal directions: North, East, South or West. This way of controlling the snake is possible using absolute actions.
Alternatively, you can also rotate the snake using relative actions, such as turn left or turn right, based on its current direction of movement.
The class SnakeGame contains several methods to control the movement of the snake in either a relative or absolute fashion (table below).
Relative |
Absolute |
|
Actions |
TurnLeft TurnRight |
GoNorth GoEast GoSouth GoWest |
Getting Started
To complete this assignment you will need to have Unity installed. You can download it for free (Personal Edition) from the Unity Store . You need Unity 2018.2; the project is not guaranteed to work with any other version. You get install Unity Hub to manage multiple versions of Unity on the same machine.
Importing the package
- Download the Unity package from Learn.Gold
-
Open Unity, click on “New” to create a new project.
You should be using Unity 2018.2 - Drag the package into Unity to import it
Testing the project
Once the package is imported, you need to run a test scene to make sure everything works as expected.
- Open the scene called “0. Test” in the folder “ Assets/Snake”
- Run the scene by pressing the play button
The Snake simulator will start, playing the game with an AI that moves randomly (see below).
If you see something different, or if you receive error messages in the Console window, there might be a problem with your setup or with your version of Unity.
There are other two scenes that you can try: “1. Keyboard” and “ 2. Example”. The former allows you to play snake with the
keyboard. The latter shows a very simple AI that tries to avoid obstacles.
Setting up your AI
Now that the project is working correctly, you need to create your own AI. For this task, you can the scene “3. Create”.
Creating the Script
- From the Project window, open the folder “Snake/AIs/Students”
-
Select the script “SnakeAI_yourlogin” and duplicate it by
pressing Ctrl+D.
? This operation has to be done from the Unity editor! Ctrl+D on Windows deletes the selected files without asking for confirmation! -
Right click on the newly created file and rename it using your student
login.
For instance, if your login is “azucc002”, you should rename it “SnakeAI_azucc002” (use your student login instead).
After these steps, you might see an error appearing in the console. That is
normal, and is caused by the fact that while we have renamed the file, we have not changed the name of the class it
contains (class names in C# must be unique).
Editing the Script
- Open your newly renames script using MonoDevelop or Visual Studio
- Rename the class from “SnakeAI_yourlogin” to “SnakeAI_azucc002 ” (use your student login instead), as seen below:
// Before [CreateAssetMenu(fileName = "SnakeAI_yourlogin", menuName = "Snake/SnakeAI_yourlogin")] public class SnakeAI_yourlogin : SnakeAI // After [CreateAssetMenu(fileName = "SnakeAI_azucc002", menuName = "Snake/SnakeAI_azucc002")] public class SnakeAI_azucc002 : SnakeAI
At this point, there should be no errors in the console.
If you want to experiment with multiple AIs, you can repeat this procedure to create other classes.
? While you are encouraged to play with multiple AIs, only the one named with your student login will be valid for submission!
Creating the Scriptable Object
- From the Project window, open the folder “Snake/AIs/Students”
-
Right click on it and from the context menu select “ Create/Snake/SnakeAI_azucc002” (use your student login
instead).
? Make sure the name of your scriptable object is “ SnakeAI_azucc002” (use your student login instead).
This will instantiate your script into ascriptable object; loosely speaking, scripts andscriptable objects are comparable to classes and objects.
The scriptable object is the asset that you will drag and drop in the Snake simulator.
Using the Snake Simulator
Once you have created a scriptable object for your AI, you can start using the Snake simulator.
- Open the scene “3. Create” from the folder “Snake”
-
Select the game object “SnakeGame” from the Hierarchy (the
list on the left side of the screen).
You will see all the parameters of the Snake simulator on the Inspector (right side of the screen). - Find the scriptable object that you have previously created, and drag it onto the “AI” slot of “SnakeGame” (below).
Once that is done, the Snake simulator will use the chosen AI to control the snake movement. If you have not made any change to your Snake AI, the snake will simply travel in a straight line.
? All the settings of the Snake simulator (world size, snake graphics, speed, …) are accessible and can be changed at any time. Please, remember that your Snake AI will be tested with the default ones.
public override Node CreateBehaviourTree(SnakeGame Snake) { return new Filter ( Snake.IsFoodLeft, // The condition to test for new Action( Snake.TurnLeft ) // The action to execute ); }
? For the purpose of the submission, your Unity package should only contain those two files.
? If there are other files that you might want to submit (multiple AIs, additional classes or libraries, other test scenes you have used to finetune the parameters of your AIs…) get in touch and we can discuss that.
? Make sure to fill all the fields in the Snake AI scriptable objects before submission!
? You are also highly encouraged to give silly names to your Snake AI (such as TurboSnake3000).
Snake API
These are the methods of the SnakeGame class that are allowed to be called.
World properties (read only)
- Vector2Int Snake.FoodPosition
- Vector2Int Snake.Size
Relative perception
- bool Snake.IsFoodAhead ()
- bool Snake.IsFoodLeft ()
- bool Snake.IsFoodRight ()
- bool Snake.IsFreeAhead ()
- bool Snake.IsFreeLeft ()
- bool Snake.IsFreeRight ()
- bool Snake.IsObstacleAhead ()
- bool Snake.IsObstacleLeft ()
- bool Snake.IsObstacleRight ()
Relative movement
- void Snake.TurnLeft ()
- void Snake.TurnRight ()
Absolute perception
- bool Snake.IsFoodNorth ()
- bool Snake.IsFoodEast ()
- bool Snake.IsFoodSouth ()
- bool Snake.IsFoodWest ()
- bool Snake.IsFreeNorth ()
- bool Snake.IsFreeEast ()
- bool Snake.IsFreeSouth ()
- bool Snake.IsFreeWest ()
- bool Snake.IsObstacleNorth ()
- bool Snake.IsObstacleEast ()
- bool Snake.IsObstacleSouth ()
- bool Snake.IsObstacleWest ()
Absolute movement
- void Snake.GoNorth ()
- void Snake.GoEast ()
- void Snake.GoSouth ()
- void Snake.GoWest ()