├── .gitignore ├── .idea ├── caches │ └── build_file_checksums.ser ├── codeStyles │ └── Project.xml ├── gradle.xml ├── misc.xml └── runConfigurations.xml ├── LICENSE.md ├── ReadMe.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── pd │ │ └── mlgame │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── pd │ │ │ └── mlgame │ │ │ └── MainActivity.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ ├── ic_launcher_background.xml │ │ ├── icon.gif │ │ └── logo.jpg │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── pd │ └── mlgame │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/libraries 5 | /.idea/modules.xml 6 | /.idea/workspace.xml 7 | .DS_Store 8 | /build 9 | /captures 10 | .externalNativeBuild 11 | -------------------------------------------------------------------------------- /.idea/caches/build_file_checksums.ser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pradyuman7/MachineLearning-Game/81d1f04368d61940059d6d7f9c0e0d92b7f965b3/.idea/caches/build_file_checksums.ser -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 15 | 16 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 27 | 28 | 29 | 30 | 31 | 32 | 34 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Pradyuman Dixit 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /ReadMe.md: -------------------------------------------------------------------------------- 1 | # Machine Learning Game (Android) 2 | 3 | A simple machine learning ✊ ✋✌️ android game, which becomes unbeatable 😱 as you play (learns from your past moves). 4 | 5 | [![ML_Game](https://img.shields.io/badge/Pradyuman7-ML_Game-blue.svg?style=plastic)](https://github.com/Pradyuman7/MachineLearning-Game) 6 | 7 | ## Finished app looks like this 8 | ![screenshot_2019-01-20-09-53-16-578_com pd mlgame](https://user-images.githubusercontent.com/41565823/51437119-dde72d80-1c99-11e9-8685-58ff09f02a99.jpg) 9 | ![screenshot_2019-01-20-09-53-06-962_com pd mlgame-2](https://user-images.githubusercontent.com/41565823/51437120-dde72d80-1c99-11e9-9709-0e53fc7e7676.jpg) 10 | 11 | **Read the Hacker Noon post about building this app, [here](https://hackernoon.com/how-to-make-a-machine-learning-android-game-from-scratch-82d9406a7635).** 12 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 28 5 | defaultConfig { 6 | applicationId "com.pd.mlgame" 7 | minSdkVersion 15 8 | targetSdkVersion 28 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(dir: 'libs', include: ['*.jar']) 23 | implementation 'com.android.support:appcompat-v7:28.0.0' 24 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 25 | testImplementation 'junit:junit:4.12' 26 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 28 | } 29 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/pd/mlgame/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.pd.mlgame; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.pd.mlgame", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/pd/mlgame/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.pd.mlgame; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | import android.view.View; 6 | import android.widget.Button; 7 | import android.widget.TextView; 8 | 9 | 10 | public class MainActivity extends AppCompatActivity { 11 | 12 | /*first two are for previous result second two are for recent result 13 | *results[0][x][0][x] 0 at index 0 or 2 is a win 14 | *results[1][x][1][x] 1 at index 0 or 2 is a tie 15 | *results[2][x][2][x] 2 at index 0 or 2 is a loss 16 | *results[x][0][x][0] 0 at index 1 or 3 is for rock 17 | *results[x][1][x][1] 1 at index 1 or 3 is for scissors 18 | *results[x][2][x][2] 2 at index 1 or 3 is for paper*/ 19 | 20 | private int[][][][][][] results6D=new int[3][3][3][3][3][3]; 21 | private int[][][][] results4D=new int[3][3][3][3]; 22 | private int[][] results2D= new int [3][3]; 23 | 24 | private int lastResult=-1; 25 | private int lastCompMove=-1; 26 | 27 | private int secondResult=-1; 28 | private int secondCompMove=-1; 29 | 30 | private int status=0; 31 | 32 | @Override 33 | protected void onCreate(Bundle savedInstanceState) { 34 | super.onCreate(savedInstanceState); 35 | setContentView(R.layout.activity_main); 36 | 37 | } 38 | 39 | public void userMove(View v) { 40 | 41 | int userMoveInt; 42 | Button b=(Button) v; 43 | String name=b.getText().toString(); 44 | switch (name) { 45 | case ("Rock"): { 46 | userMoveInt = 0; 47 | break; 48 | } 49 | case ("Paper"): { 50 | userMoveInt = 1; 51 | break; 52 | } 53 | default: 54 | userMoveInt = 2; 55 | } 56 | 57 | int compMoveInt; 58 | String compMoveStr; 59 | int resultStatus; 60 | compMoveInt=nextCompMove(); 61 | 62 | if (compMoveInt==0) 63 | compMoveStr="rock"; 64 | 65 | else if (compMoveInt==1) 66 | compMoveStr="paper"; 67 | 68 | else if (compMoveInt==2) 69 | compMoveStr="scissors"; 70 | 71 | else 72 | compMoveStr="nothing"; 73 | 74 | String text="I drew "+compMoveStr+", "; 75 | resultStatus=checkResult(compMoveInt,userMoveInt); 76 | 77 | if (resultStatus==0) 78 | text+="You Win!"; 79 | 80 | else if (resultStatus==1) 81 | text+="It's a Tie."; 82 | 83 | else if (resultStatus==2) 84 | text+="You Lose!"; 85 | 86 | TextView output= findViewById(R.id.output); 87 | output.setText(text); 88 | trainModel(resultStatus,compMoveInt); 89 | 90 | float[] percentages=analyzeResults(); 91 | int[] changes=changes(percentages); 92 | int rockChance=changes[0]; 93 | int paperChance=changes[1]; 94 | 95 | text = "Rock: "+String.valueOf(rockChance)+"%"; 96 | ((TextView) findViewById(R.id.rPer)).setText(text); 97 | text = "Paper: "+String.valueOf(paperChance)+"%"; 98 | ((TextView) findViewById(R.id.pPer)).setText(text); 99 | text = "Scissors: "+String.valueOf(100-rockChance-paperChance)+"%"; 100 | ((TextView) findViewById(R.id.sPer)).setText(text); 101 | 102 | int top=Math.max(rockChance,Math.max(paperChance,100-rockChance-paperChance)); 103 | text = "Confidence\n"+String.valueOf(Math.round(2*(top-50)))+"%"; 104 | ((TextView) findViewById(R.id.confidence)).setText(text); 105 | } 106 | 107 | private int nextCompMove() { 108 | 109 | if (lastResult==-1) 110 | return 1; 111 | 112 | int chanceOneHundred=(int)(Math.random()*99)+1; 113 | float[] percentages=analyzeResults(); 114 | int[] changes=changes(percentages); 115 | int rockChance=changes[0]; 116 | int paperChance=changes[1]; 117 | int compMove; 118 | 119 | if (chanceOneHundred 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/icon.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pradyuman7/MachineLearning-Game/81d1f04368d61940059d6d7f9c0e0d92b7f965b3/app/src/main/res/drawable/icon.gif -------------------------------------------------------------------------------- /app/src/main/res/drawable/logo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pradyuman7/MachineLearning-Game/81d1f04368d61940059d6d7f9c0e0d92b7f965b3/app/src/main/res/drawable/logo.jpg -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 13 | 24 | 25 | 33 | 34 |