How to Use Neural Network in Gaming?

Aaditya Tiwari
7 min readMar 1, 2021

--

Face Recognition, Speech Recognition, Handwriting Recognition, just a few of the tasks many tasks that we as humans are able to quickly solve. We seem to be able, to perform these tasks effortlessly, which is in some cases impossible for even the most sophisticated computer programs to solve that tasks. Then the question that arises is “What is the difference between computer and human?”.

Here, we are not going to take more discussion on it but going to take an introductory look at one aspect of it. A massive parallel network of simple computation units forms by the biological structure of the human brain that has been trained to solve problems quickly. When this network simulated on a computer is known as an artificial neural network or neural net for short.

In this blog, we are going to get some introductory parts of neural networks and how we used a neural network in gaming. It is an interesting area because gaming technology has always been an attractive area.

Let’s start with the introduction part of the Neural Network…

Neural Network

A neural network is a system of software/hardware patterned like a human’s brain neurons. A Neural network is also known as Artificial Neural Networks (ANN). It is a type of deep learning, a concept of machine learning. Commercial applications of these technologies generally focus on solving complex pattern recognition problems.

It is a way to build a computer program that learns from data. It is based on how we think the human brain works.

A neural network made when a collection of software called “neurons” are created and connected together. It allows them to send messages to each other. After that, the network asked to solve a particular problem, which it attempts to do again and again. The strengthening of connections each time leads to success and diminishing those that lead to failure.

We can also understand the Neural Networks/Artificial Neural Network as-

Neural networks are typically made of layers (Input Layer, Hidden Layer, Output Layer). Layers are made up of a number of interconnected ‘nodes’ that contain an ‘activation function’. Patterns are presented to the network through the input layer’. This input layer communicates with one or more hidden layers’. The actual processing is done through a system of weighted ‘connections’. The hidden layers then link to an ‘output layer’ and the output comes as:

Most Artificial Neural Networks contains learning rule that modifies the weights of the connections according to the input patterns that it is presented with. Artificial Neural Network learns by example as do their biological counterparts. A child learns to recognize dogs by learning.

Learning Rules

There are different learning rules used in an artificial neural network. Some name s are as follows…

  • Hebbian Learning Rule
  • Perceptron Learning Rule
  • Delta Learning Rule
  • Correlation Learning Rule
  • Out Star Learning Rule

Applications of Neural Network

Some applications where neural network used, are as follows:

  • It is used to capture associations or discover regularities within a set of patterns.
  • the relationships between variables are vaguely understood.
  • It is used where the relationships are difficult to describe adequately with conventional approaches.
  • where the volume, number of variables, or diversity of the data is very high.
  • Now, comes on to how neural networks used in gaming…

Here, I am going to talk about a small practice of using neural networks that are based on training one to play a Snake Game. This practice is for beginners. It would be good if you know something about machine learning, neural networks, and TensorFlow but there is no problem if you don’t know.

This practice consists of three phases.:

  • Phase 1
  • Phase 2
  • and Phase 3

Let’s start with phase 1…

THE GAME PHASE

Firstly, we need to write a game that will have a 3 pieces snake at the starting, a 20×20 field, a randomly generated apple in each moment of time, and API to use with the network.

Here is the code for the game:

The game would be like this:

Now let’s start with a neural network.

SURVIVE PHASE

This phase consist of 4 steps are as follows:

  • Features
  • Input data
  • The architecture of neural network
  • Results

Features

Here, we need to create features to teach our snake and to make our snake smart by giving some knowledge data. Always try to choose the most useful features because too many features arise a problem that makes hard for a network to decide which are more important and learning will be longer. It is important to add features through which a network will get enough information to be good but if you are not adding enough features, it makes difficult for a network to get good information.

At the first step, we will focus on the snake that how it survive and will not think about apples. To choose a right direction it should know if there are any hurdle around it. On the basis of these hurdle/obstacle and suggested direction the network will decide that it is a good action or not.

We use an array of 4 numbers for the inputs of our neural network:

  • If, there is an obstacle to the left of the snake then(1 — yes, 0 — no)
  • There is an obstacle in front of the snake then (1 — yes, 0 — no)
  • If, there is an obstacle to the right of the snake then (1 — yes, 0 — no)
  • and For Suggested direction (-1 — left, 0 — forward, 1 — right)

With these inputs we want some output that would be 1 or 0:

  • 1 — Means we should go in the selected direction.
  • 0 -Means we should choose another one.

Input Data

A neural network needs some data to learn. Data is the key element of the machine learning if you have a huge amount of data then you can achieve great results even if you are not having a good architecture. In this time big companies focus on to get all the information that they can get because big data matters to get good results.

We need to create data randomly by choosing a direction and observe the snake is still alive after a turn.

By playing different numbers of a game we collect data around 4500 which is enough for training to survive.

Architecture of Neural Network

A neural network is always hard to choose. It always depends on the task that you trying to solve. In this, you need to choose the number of layers, number of neurons in layers, and types of neurons. It is better to try a different variety and choose one of the best fits more than others.

So, to achieve our task we need only Input Layer and Output Layer there is no need to use Hidden Layers.

Summary

A neural network is a wide concept of machine learning and used in different real-world applications to make work easier. One of the most interesting application is gaming. In gaming application neural network used to provide knowledge to the model and make them smart by learning. It helps to make a model like human’s mind that helps to take decisions or we can say right decisions.

Here, I used features for our neural network, an architecture and then got some input data. From collected input data network choose the best result for a given features.

In this practice, you can see that how a snake can be knowledgeable by giving some knowledge or knowledge data. It is a small practice on a neural network we can make more complex model using a neural network.

I think this will give you some basic knowledge about Neural Network or Artificial Neural Network (ANNs) and how it is used in gaming technology.

Thank You :)

--

--