How to make a Machine Learning Android Game from scratch

Pradyuman Dixit
HackerNoon.com
5 min readJan 20, 2019

--

Let’s make an android machine learning game from scratch 🌟.

Are you fascinated by machine learning? Do you like Java or Android? Do you want to make a cool project using android and machine learning?

machine learning

If yes, then you’re certainly at the right place. Here we will be making an android application which uses machine learning to defeat the user.

Yes, defeat!! That means as you play along, it will become more and more pro at the game and your techniques, so you better prepare to lose.

But even after losing, you’ll definitely be proud. Why, you ask?

Well, you made it learn, and you made it pro in the game. So, who’s the boss?

For starting purposes, let’s start with a simple game, with less possibilities. I think something like, Rock, Paper and Scissors will do just fine.

This post will help you understand how you can make a game with machine learning and android, so you can make an amazing project game of your own, after this!

Let’s dive into understanding how our app will progress, in a simple flow chart.

Android App → Gives options for Rock/Paper/Scissor → User chooses one of them → If first time, then a random choice by computer → Otherwise, computer analyses the user input, and uses statistics to choose the one with more probability to win → Game continues, computer becomes more pro.

So this is our app in a flow chart, which will give us direction, when we are stuck at what to do now.

Simple Developer Tip: Always make a simple flow chart before making an app, to guide your journey during it’s making.

Now let’s dive into some code.

Start a new project on Android Studio or IDE of your choice. Head toward the MainActivity.java file. We will start with the Java code, first.

Let’s first initialise some variables and flags. So add the following code for that in your MainActivity.

I have commented in the code for more understanding. We will be using last and recent choices by the user to train our Machine Learning model.

Now we should add the code that gives user the choice to choose from Rock, Paper and Scissor. Also the code which shows what the computer drew, and the result of the game.

The code like this would do for the above discussed aspect of our game, also this is the code inside the userMove(view) method of our code.

Also we are using integer 0 for rock, 1 for paper and 2 for scissors.

Now let’s write some code for the computer to choose and learn from the user. Also, this code is inside the method nextCompMove() of our code.

If you understand a bit of probability, then you know, choosing any one of rock, paper or scissors at random has probability 1/3 or ~33%.

So, we are putting the default chances for every choice, as 33%. The code to change the choices, for the method changes(percentage) looks something like this:

Now let’s help computer analyse the moves of the user, and choose something that has the most probability to win the game for the computer.

That will be our code for the method analyzeResults() :

Take some time to understand this code, which is almost self explanatory. If you understand this, you almost understand the whole code for the game.

Let’s also complete the trainModel(resultStatus, compMove) code, so we’re done with the machine learning crux part of the game.

And you’re all done. You’ve made yourself a machine learning android game, which almost certainly wins against the user.

But, wait! Where’s the winning part? We certainly don’t want to leave the user in dark, about who won the game, now do we?

So, let’s write the code for checkResult(compMove, userMove) which tells the user, about the winning or losing of the game.

And yes, now you’ve coded yourself everything you need for your machine learning android game to work and defeat anyone.

The UI and some other fine tweaking in the code, can be found on my GitHub repository.

The full source code for the app.

You’re all done now, for sure.

What are you waiting for? Go show your friends how pathetically they can lose to your android game.

Go and Show Off !

--

--