An iOS mobile based Snake Game which relies on neural networks to navigate a grid and collect food items.
Basic components:
- Tensorflow
- Keras
- CoreML
- SpriteKit
- iOS
How it was built:
A Keras model was generated by training a Snake to navigate through a grid and collect food items.
This way, we generated a CSV file with the following inputs and outputs.
Inputs:
- Can snake move left?
- Can snake move forward?
- Can snake move right?
- What is the orthogonal angle between the food and snake head? This needs to be relative to current direction.
- A random direction
Output:
- On moving closer to food, it was given a score of 1.
- On moving away, it was given a score of 0.
- On dying it was given a score of -1.
Keras is a high-level framework built on top of Theano and TensorFlow. In our case, Keras was backed by TensorFlow. We built the model using the following layers:
Input layer:
5 neurons based on relu activation function
Hidden layer # 1:
25 neurons based on relu activation function
Hidden layer # 2:
25 neurons based on relu activation function
Output layer:
1 neurons based on linear activation function
The model and training algorithm was written entirely in Python, it was a good project to practice python and get good practice on Keras and Neural Networks in general. Then the model was exported to CoreML using a couple of lines of code.
Then that CoreML model was imported in iOS project and FE of the game was made in SpriteKit (2D game framework provided by Apple).
Inspired by the tutorial:
