├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── drawable │ │ │ │ └── play_arrow.png │ │ │ ├── mipmap-hdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ ├── ic_human_marker.png │ │ │ │ ├── ic_default_marker.png │ │ │ │ ├── ic_launcher_round.png │ │ │ │ └── ic_computer_marker.png │ │ │ ├── mipmap-mdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ ├── ic_human_marker.png │ │ │ │ ├── ic_default_marker.png │ │ │ │ ├── ic_launcher_round.png │ │ │ │ └── ic_computer_marker.png │ │ │ ├── mipmap-xhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ ├── ic_human_marker.png │ │ │ │ ├── ic_computer_marker.png │ │ │ │ ├── ic_default_marker.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ ├── ic_human_marker.png │ │ │ │ ├── ic_computer_marker.png │ │ │ │ ├── ic_default_marker.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ ├── ic_default_marker.png │ │ │ │ ├── ic_human_marker.png │ │ │ │ ├── ic_launcher_round.png │ │ │ │ └── ic_computer_marker.png │ │ │ ├── layout │ │ │ │ ├── activity_main.xml │ │ │ │ ├── layout_play_again.xml │ │ │ │ └── layout_game_board.xml │ │ │ └── values │ │ │ │ ├── strings.xml │ │ │ │ ├── colors.xml │ │ │ │ └── styles.xml │ │ ├── java │ │ │ └── lingaraj │ │ │ │ └── hourglass │ │ │ │ └── in │ │ │ │ └── tictactoe │ │ │ │ ├── Constants.java │ │ │ │ ├── TicTacToeInterface.java │ │ │ │ ├── Models │ │ │ │ └── MachineMoveChoice.java │ │ │ │ ├── activities │ │ │ │ └── MainActivity.java │ │ │ │ └── Libraries │ │ │ │ └── TicTacToe.java │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── lingaraj │ │ │ └── hourglass │ │ │ └── in │ │ │ └── tictactoe │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── lingaraj │ │ └── hourglass │ │ └── in │ │ └── tictactoe │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── gradle.properties ├── gradlew.bat └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lingarajsankaravelu/TicTacToe/master/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/drawable/play_arrow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lingarajsankaravelu/TicTacToe/master/app/src/main/res/drawable/play_arrow.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lingarajsankaravelu/TicTacToe/master/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lingarajsankaravelu/TicTacToe/master/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lingarajsankaravelu/TicTacToe/master/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_human_marker.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lingarajsankaravelu/TicTacToe/master/app/src/main/res/mipmap-hdpi/ic_human_marker.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_human_marker.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lingarajsankaravelu/TicTacToe/master/app/src/main/res/mipmap-mdpi/ic_human_marker.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lingarajsankaravelu/TicTacToe/master/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lingarajsankaravelu/TicTacToe/master/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_default_marker.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lingarajsankaravelu/TicTacToe/master/app/src/main/res/mipmap-hdpi/ic_default_marker.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lingarajsankaravelu/TicTacToe/master/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_default_marker.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lingarajsankaravelu/TicTacToe/master/app/src/main/res/mipmap-mdpi/ic_default_marker.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lingarajsankaravelu/TicTacToe/master/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_human_marker.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lingarajsankaravelu/TicTacToe/master/app/src/main/res/mipmap-xhdpi/ic_human_marker.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_human_marker.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lingarajsankaravelu/TicTacToe/master/app/src/main/res/mipmap-xxhdpi/ic_human_marker.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_computer_marker.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lingarajsankaravelu/TicTacToe/master/app/src/main/res/mipmap-hdpi/ic_computer_marker.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_computer_marker.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lingarajsankaravelu/TicTacToe/master/app/src/main/res/mipmap-mdpi/ic_computer_marker.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_computer_marker.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lingarajsankaravelu/TicTacToe/master/app/src/main/res/mipmap-xhdpi/ic_computer_marker.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_default_marker.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lingarajsankaravelu/TicTacToe/master/app/src/main/res/mipmap-xhdpi/ic_default_marker.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lingarajsankaravelu/TicTacToe/master/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_computer_marker.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lingarajsankaravelu/TicTacToe/master/app/src/main/res/mipmap-xxhdpi/ic_computer_marker.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_default_marker.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lingarajsankaravelu/TicTacToe/master/app/src/main/res/mipmap-xxhdpi/ic_default_marker.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lingarajsankaravelu/TicTacToe/master/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_default_marker.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lingarajsankaravelu/TicTacToe/master/app/src/main/res/mipmap-xxxhdpi/ic_default_marker.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_human_marker.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lingarajsankaravelu/TicTacToe/master/app/src/main/res/mipmap-xxxhdpi/ic_human_marker.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lingarajsankaravelu/TicTacToe/master/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_computer_marker.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lingarajsankaravelu/TicTacToe/master/app/src/main/res/mipmap-xxxhdpi/ic_computer_marker.png -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Sep 01 21:02:54 IST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip 7 | -------------------------------------------------------------------------------- /app/src/main/java/lingaraj/hourglass/in/tictactoe/Constants.java: -------------------------------------------------------------------------------- 1 | package lingaraj.hourglass.in.tictactoe; 2 | 3 | /** 4 | * Created by lingaraj on 9/1/17. 5 | */ 6 | 7 | public class Constants { 8 | public static final int HUMAN = 0; 9 | public static final int COMPUTER = 1; 10 | public static final int INDEX_UNOCCUPIED = -1; 11 | 12 | } 13 | -------------------------------------------------------------------------------- /app/src/main/java/lingaraj/hourglass/in/tictactoe/TicTacToeInterface.java: -------------------------------------------------------------------------------- 1 | package lingaraj.hourglass.in.tictactoe; 2 | 3 | /** 4 | * Created by lingaraj on 9/3/17. 5 | */ 6 | 7 | public interface TicTacToeInterface { 8 | void endGame(boolean is_tie, String score_key, int player); 9 | void nextPlayerMove(int previously_played_player); 10 | 11 | } 12 | -------------------------------------------------------------------------------- /app/src/test/java/lingaraj/hourglass/in/tictactoe/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package lingaraj.hourglass.in.tictactoe; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 3 | 7 | 8 | 11 | 12 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | TicTacToe 3 | Tic Tac Toe 4 | Waiting for User to make a move... 5 | Waiting for Computer to make a move... 6 | Player 7 | 0 8 | Tie 9 | Machine 10 | Unable to Generate the next move for Computer 11 | Current Game ended in a tie 12 | You lost the game 13 | You won the game 14 | 15 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/lingaraj/hourglass/in/tictactoe/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package lingaraj.hourglass.in.tictactoe; 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 | * Instrumentation 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() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("lingaraj.hourglass.in.tictactoe", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | #F44336 8 | #3F51B5 9 | #009688 10 | #FF9800 11 | #E91E63 12 | #2196F3 13 | #4CAF50 14 | #FF5722 15 | #9C27B0 16 | #03A9F4 17 | #8BC34A 18 | #795548 19 | #673AB7 20 | #00BCD4 21 | #FFC107 22 | #607D8B 23 | 24 | 25 | -------------------------------------------------------------------------------- /app/src/main/res/layout/layout_play_again.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 17 | 18 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/lingaraj/Library/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | dataBinding.enabled = true; 5 | 6 | 7 | compileSdkVersion 26 8 | buildToolsVersion "26.0.0" 9 | defaultConfig { 10 | vectorDrawables.useSupportLibrary = true 11 | applicationId "lingaraj.hourglass.in.tictactoe" 12 | minSdkVersion 16 13 | targetSdkVersion 26 14 | versionCode 1 15 | versionName "1.0" 16 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 17 | } 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | } 25 | 26 | dependencies { 27 | compile fileTree(include: ['*.jar'], dir: 'libs') 28 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 29 | exclude group: 'com.android.support', module: 'support-annotations' 30 | }) 31 | compile 'com.android.support:appcompat-v7:26.+' 32 | compile 'com.android.support.constraint:constraint-layout:1.0.2' 33 | testCompile 'junit:junit:4.12' 34 | compile 'com.android.support:cardview-v7:26.0.0-alpha1' 35 | compile 'com.android.support:design:26.0.0-alpha1' 36 | } 37 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 20 | 21 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /app/src/main/java/lingaraj/hourglass/in/tictactoe/Models/MachineMoveChoice.java: -------------------------------------------------------------------------------- 1 | package lingaraj.hourglass.in.tictactoe.Models; 2 | 3 | /** 4 | * Created by lingaraj on 9/2/17. 5 | */ 6 | 7 | public class MachineMoveChoice { 8 | private boolean redAlert; 9 | private String boardKey; 10 | private boolean isNull; 11 | private boolean isScore; 12 | 13 | 14 | public MachineMoveChoice(boolean is_red_alert, String board_key, boolean is_null){ 15 | this.redAlert = is_red_alert; 16 | this.boardKey = board_key; 17 | this.isNull = is_null; 18 | } 19 | 20 | public MachineMoveChoice(boolean is_null){ 21 | this.isNull = is_null; 22 | } 23 | public boolean isScore() { 24 | return isScore; 25 | } 26 | 27 | public void setScore(boolean score) { 28 | isScore = score; 29 | } 30 | 31 | 32 | public boolean isNull() { 33 | return isNull; 34 | } 35 | 36 | 37 | public boolean isRedAlert() { 38 | return redAlert; 39 | } 40 | 41 | 42 | public String getBoardKey() { 43 | return boardKey; 44 | } 45 | 46 | public void setBoardKey(String boardKey) { 47 | this.boardKey = boardKey; 48 | } 49 | 50 | public void setRedAlert(boolean redAlert) { 51 | this.redAlert = redAlert; 52 | } 53 | 54 | public void setNull(boolean aNull) { 55 | isNull = aNull; 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /app/src/main/res/layout/layout_game_board.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 10 | 11 | 12 | 18 | 23 | 24 | 26 | 27 | 33 | 34 | 35 | 36 | 37 | 39 | 40 | 47 | 48 | 49 | 50 | 51 | 53 | 54 | 62 | 63 | 64 | 65 | 66 | 67 | 72 | 73 | 75 | 76 | 81 | 82 | 83 | 84 | 85 | 87 | 88 | 94 | 95 | 96 | 97 | 98 | 100 | 101 | 108 | 109 | 110 | 111 | 112 | 113 | 118 | 119 | 121 | 122 | 127 | 128 | 129 | 130 | 131 | 133 | 134 | 141 | 142 | 143 | 144 | 145 | 147 | 148 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 177 | 178 | 182 | 183 | 189 | 190 | 200 | 211 | 212 | 213 | 214 | 220 | 221 | 231 | 242 | 243 | 244 | 250 | 251 | 261 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | -------------------------------------------------------------------------------- /app/src/main/java/lingaraj/hourglass/in/tictactoe/activities/MainActivity.java: -------------------------------------------------------------------------------- 1 | package lingaraj.hourglass.in.tictactoe.activities; 2 | 3 | import android.databinding.DataBindingUtil; 4 | import android.os.Handler; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.os.Bundle; 7 | import android.util.Log; 8 | import android.view.View; 9 | import android.widget.ImageView; 10 | import android.widget.Toast; 11 | 12 | import java.util.ArrayList; 13 | import java.util.HashMap; 14 | import java.util.List; 15 | import java.util.Map; 16 | 17 | import lingaraj.hourglass.in.tictactoe.Constants; 18 | import lingaraj.hourglass.in.tictactoe.Libraries.TicTacToe; 19 | import lingaraj.hourglass.in.tictactoe.R; 20 | import lingaraj.hourglass.in.tictactoe.TicTacToeInterface; 21 | import lingaraj.hourglass.in.tictactoe.databinding.ActivityMainBinding; 22 | 23 | public class MainActivity extends AppCompatActivity implements TicTacToeInterface { 24 | 25 | ActivityMainBinding activity_binding; 26 | final String TAG = "ACTMAIN"; 27 | Map tiles_map = new HashMap<>(); 28 | TicTacToe ticTacToe; 29 | int total_tiles = 9; 30 | int current_index = 0; 31 | private int player_mode = Constants.HUMAN; 32 | @Override 33 | protected void onCreate(Bundle savedInstanceState) { 34 | super.onCreate(savedInstanceState); 35 | setContentView(R.layout.activity_main); 36 | activity_binding = DataBindingUtil.setContentView(this,R.layout.activity_main); 37 | ticTacToe = new TicTacToe(MainActivity.this); 38 | setInitalData(); 39 | 40 | } 41 | 42 | private void setInitalData() { 43 | setScoreBoard(); 44 | showGameBoard(); 45 | enableGameBoardTiles(); 46 | 47 | } 48 | 49 | public void gameBoardTilesClick(View view){ 50 | switch (view.getId()){ 51 | case R.id.zero_zero: 52 | setTilesImage("00",activity_binding.tiles.zeroZero); 53 | ticTacToe.addPlayerMove(0,0,player_mode); 54 | break; 55 | case R.id.zero_one: 56 | setTilesImage("01",activity_binding.tiles.zeroOne); 57 | ticTacToe.addPlayerMove(0,1,player_mode); 58 | break; 59 | case R.id.zero_two: 60 | setTilesImage("02",activity_binding.tiles.zeroTwo); 61 | ticTacToe.addPlayerMove(0,2,player_mode); 62 | break; 63 | case R.id.one_zero: 64 | setTilesImage("10",activity_binding.tiles.oneZero); 65 | ticTacToe.addPlayerMove(1,0,player_mode); 66 | break; 67 | case R.id.one_one: 68 | setTilesImage("11",activity_binding.tiles.oneOne); 69 | ticTacToe.addPlayerMove(1,1,player_mode); 70 | break; 71 | case R.id.one_two: 72 | setTilesImage("12",activity_binding.tiles.oneTwo); 73 | ticTacToe.addPlayerMove(1,2,player_mode); 74 | break; 75 | case R.id.two_zero: 76 | setTilesImage("20",activity_binding.tiles.twoZero); 77 | ticTacToe.addPlayerMove(2,0,player_mode); 78 | break; 79 | case R.id.two_one: 80 | setTilesImage("21",activity_binding.tiles.twoOne); 81 | ticTacToe.addPlayerMove(2,1,player_mode); 82 | break; 83 | case R.id.two_two: 84 | setTilesImage("22",activity_binding.tiles.twoTwo); 85 | ticTacToe.addPlayerMove(2,2,player_mode); 86 | break; 87 | 88 | } 89 | } 90 | 91 | private void setTilesImage(String key,ImageView image_view) { 92 | image_view.setClickable(false); 93 | if (player_mode == Constants.HUMAN){ 94 | image_view.setImageResource(R.mipmap.ic_human_marker); 95 | activity_binding.tiles.gameBoardLayout.setEnabled(false); 96 | } 97 | else { 98 | image_view.setImageResource(R.mipmap.ic_computer_marker); 99 | activity_binding.tiles.gameBoardLayout.setEnabled(true); 100 | 101 | } 102 | this.tiles_map.put(key,image_view); 103 | 104 | } 105 | 106 | public void tryAgain(View view){ 107 | resetTilesMap(); 108 | ticTacToe.resetGameBoard(); 109 | setScoreBoard(); 110 | //reset index to start with zero 111 | this.current_index = 0; 112 | //always starts with human as first player 113 | this.player_mode = Constants.HUMAN; 114 | showGameBoard(); 115 | Log.d(TAG,"try again clicked"); 116 | } 117 | 118 | private void resetTilesMap() { 119 | List keyset = new ArrayList(this.tiles_map.keySet()); 120 | for (String key:keyset) { 121 | this.tiles_map.get(key).setClickable(true); 122 | this.tiles_map.get(key).setImageResource(R.mipmap.ic_default_marker); 123 | this.tiles_map.remove(key); 124 | Log.d(TAG,"Removed Key from Tiles Map:"+key); 125 | } 126 | } 127 | 128 | public void showGameBoard(){ 129 | activity_binding.screen.getRoot().setVisibility(View.GONE); 130 | activity_binding.tiles.getRoot().setVisibility(View.VISIBLE); 131 | Log.d(TAG,"Game Board Visible"); 132 | } 133 | 134 | public void showRetry(){ 135 | activity_binding.tiles.getRoot().setVisibility(View.GONE); 136 | activity_binding.screen.getRoot().setVisibility(View.VISIBLE); 137 | Log.d(TAG,"Game Board Visible"); 138 | 139 | } 140 | 141 | @Override 142 | public void endGame(boolean is_tie, String score_key, int player) { 143 | Log.d(TAG,"End Game Called show"); 144 | if (is_tie){ 145 | ticTacToe.increementTieScore(); 146 | setScoreBoard(); 147 | showToast(getString(R.string.tie_toast)); 148 | 149 | } 150 | else { 151 | if (player==Constants.COMPUTER){ 152 | setScoreBoard(); 153 | showToast(getString(R.string.player_lost)); 154 | } 155 | else { 156 | setScoreBoard(); 157 | showToast(getString(R.string.player_won)); 158 | 159 | } 160 | 161 | } 162 | disableGameBoardTiles(); 163 | Handler handler = new Handler(); 164 | handler.postDelayed(new Runnable() { 165 | @Override 166 | public void run() { 167 | showRetry(); 168 | 169 | } 170 | },2000); 171 | } 172 | 173 | private void disableGameBoardTiles() { 174 | activity_binding.tiles.gameBoardLayout.setEnabled(false); 175 | } 176 | 177 | private void enableGameBoardTiles(){ 178 | activity_binding.tiles.gameBoardLayout.setEnabled(true); 179 | } 180 | private void setScoreBoard() { 181 | activity_binding.tiles.computerScoreCount.setText(String.valueOf(ticTacToe.getComputer_score())); 182 | activity_binding.tiles.playerScoreView.setText(String.valueOf(ticTacToe.getPlayer_score())); 183 | activity_binding.tiles.tieCountView.setText(String.valueOf(ticTacToe.getTie_score())); 184 | Log.d(TAG,"Scored Board Updated"); 185 | } 186 | 187 | private void showToast(String message) { 188 | Toast.makeText(getApplicationContext(),message,Toast.LENGTH_SHORT).show(); 189 | Log.d(TAG,"Showing End game toast message"); 190 | } 191 | 192 | @Override 193 | public void nextPlayerMove(int previously_played_player) { 194 | navigateNext(previously_played_player); 195 | } 196 | 197 | private void navigateNext(int previously_played_player) { 198 | if (current_index+1> horizontal_map = new HashMap<>(); 34 | private Map> vertical_map = new HashMap<>(); 35 | private Map> diagonal_map = new HashMap<>(); 36 | private List scoreKeyList = new ArrayList(); 37 | final String VERTICAL_MODE = "vertical"; 38 | final String HORIZONTAL_MODE = "horizontal"; 39 | final String DIAGONAL_MODE = "diagonal"; 40 | final String NOT_NECESSARY = "notnecessary"; 41 | final String USE_VERTICAL_MOVE_CHOICE = "USEVERTICALMOVECHOICE"; 42 | final String USE_HORIZONTAL_MOVE_CHOICE = "USEHORIZONTALMOVECHOICE"; 43 | final String USE_DIAGONAL_MOVE_CHOICE = "USEDIAGONALMOVECHOICE"; 44 | final String NO_RESULT = "NORESULT"; 45 | private Context mcontext; 46 | private TicTacToeInterface activity_interface; 47 | public TicTacToe(Context context){ 48 | //Initializing Empty Constructor 49 | this.mcontext = context; 50 | this.activity_interface = (TicTacToeInterface) mcontext; 51 | setAxisMap(); 52 | resetGameBoard(); 53 | } 54 | 55 | private void setAxisMap() { 56 | player_score = 0; 57 | computer_score = 0; 58 | tie_score = 0; 59 | setHorizontalMap(); 60 | setVerticalMap(); 61 | setDiagonalMap(); 62 | } 63 | 64 | 65 | public void resetGameBoard() { 66 | //method can be used to reset gameboard and player score 67 | this.isCenterMatrixIndexOcuppied = false; 68 | this.scoreKeyList.clear(); 69 | for (int row = 0; row < 3; row++) { 70 | for (int column = 0; column < 3; column++) { 71 | game_board[row][column] = -1; 72 | //initializing some values to all the board 73 | 74 | } 75 | } 76 | } 77 | 78 | public void addPlayerMove(int row,int column,int player){ 79 | //every time a player/computer makes a move(drawn cross or o on a board the respective value in updated in the game board matrix 3x3) 80 | if (player== Constants.HUMAN){ 81 | //move made by player updating the same 82 | game_board[row][column] = Constants.HUMAN; 83 | Log.d(TAG,"["+row+"]"+"["+column+"]"+Constants.HUMAN); 84 | } 85 | else { 86 | //move made by COMPUTER updating the same 87 | game_board[row][column] = Constants.COMPUTER; 88 | Log.d(TAG,"["+row+"]"+"["+column+"]"+Constants.COMPUTER); 89 | 90 | } 91 | checkPlayerWinSituation(row,column,player); 92 | activity_interface.nextPlayerMove(player); 93 | } 94 | 95 | private void checkPlayerWinSituation(int row, int column, int player) { 96 | // Splitted as a three step check because, if check got succeed in vertical mode, horizontal and diagonal execution will be excluded 97 | 98 | //1.check for score in vertical mode if returned a false 99 | if (player ==Constants.HUMAN){ 100 | last_move_player_row = row; 101 | last_move_player_column = column; 102 | } 103 | 104 | if (game_board[1][1]!=-1 && !isCenterMatrixIndexOcuppied) { 105 | isCenterMatrixIndexOcuppied = true; 106 | } 107 | 108 | boolean is_match_vertical_mode = verifyVerticalHorizontalMatch(row,column,player,VERTICAL_MODE); 109 | if (!is_match_vertical_mode){ 110 | //2. vertical verificatin failed, doing horizontal mode 111 | boolean is_match_horizontal_mode = verifyVerticalHorizontalMatch(row,column,player,HORIZONTAL_MODE); 112 | if (!is_match_horizontal_mode){ 113 | //3. if vertical failed looking for diagonal 114 | String key = String.valueOf(row)+String.valueOf(column); 115 | if (this.diagonal_map.containsKey(key)){ 116 | // there is possiblity for diagonal scoring. 117 | obtainDiagonalData(key,player); 118 | } 119 | } 120 | } 121 | 122 | } 123 | 124 | private void obtainDiagonalData(String key, int player) { 125 | List matrixIndexes = this.diagonal_map.get(key); 126 | if (key.equals("11")){ 127 | //if its a mid index in 3x3 matirx we have to check two diagonals in two ways as it is the mid value 128 | //this list will contain six indexes instead of three because it represent two diagonals as the mid point is choosen. 129 | int tic1 = game_board[Integer.parseInt(String.valueOf(matrixIndexes.get(0).charAt(0)))][Integer.parseInt(String.valueOf(matrixIndexes.get(0).charAt(1)))]; 130 | int tac1 = game_board[Integer.parseInt(String.valueOf(matrixIndexes.get(1).charAt(0)))][Integer.parseInt(String.valueOf(matrixIndexes.get(1).charAt(1)))]; 131 | int toe1 = game_board[Integer.parseInt(String.valueOf(matrixIndexes.get(2).charAt(0)))][Integer.parseInt(String.valueOf(matrixIndexes.get(2).charAt(1)))]; 132 | //takes first three indexes as key 133 | String score_key = generateScoreKey(matrixIndexes.subList(0,3)); 134 | if (player == Constants.HUMAN){ 135 | if (tic1==Constants.HUMAN && tac1 ==Constants.HUMAN && toe1==Constants.HUMAN){ 136 | if (!scoreKeyList.contains(score_key)){ 137 | updateScoreMap(score_key,player); 138 | } 139 | } else { 140 | processSecondIndexes(matrixIndexes.subList(3,6),player); 141 | } 142 | } 143 | else if (player == Constants.COMPUTER){ 144 | if (tic1==Constants.COMPUTER && tac1 ==Constants.COMPUTER && toe1==Constants.COMPUTER){ 145 | if (!scoreKeyList.contains(score_key)){ 146 | updateScoreMap(score_key,player); 147 | } 148 | } else { 149 | processSecondIndexes(matrixIndexes.subList(3,6),player); 150 | } 151 | 152 | 153 | } 154 | 155 | } 156 | else { 157 | int tic1 = game_board[Integer.parseInt(String.valueOf(matrixIndexes.get(0).charAt(0)))][Integer.parseInt(String.valueOf(matrixIndexes.get(0).charAt(1)))]; 158 | int tac1 = game_board[Integer.parseInt(String.valueOf(matrixIndexes.get(1).charAt(0)))][Integer.parseInt(String.valueOf(matrixIndexes.get(1).charAt(1)))]; 159 | int toe1 = game_board[Integer.parseInt(String.valueOf(matrixIndexes.get(2).charAt(0)))][Integer.parseInt(String.valueOf(matrixIndexes.get(2).charAt(1)))]; 160 | String score_key = generateScoreKey(matrixIndexes); 161 | checkGameBoard(tic1,tac1,toe1,player,score_key); 162 | } 163 | } 164 | 165 | private void processSecondIndexes(List matrixIndexes, int player) { 166 | int tic1 = game_board[Integer.parseInt(String.valueOf(matrixIndexes.get(0).charAt(0)))][Integer.parseInt(String.valueOf(matrixIndexes.get(0).charAt(1)))]; 167 | int tac1 = game_board[Integer.parseInt(String.valueOf(matrixIndexes.get(1).charAt(0)))][Integer.parseInt(String.valueOf(matrixIndexes.get(1).charAt(1)))]; 168 | int toe1 = game_board[Integer.parseInt(String.valueOf(matrixIndexes.get(2).charAt(0)))][Integer.parseInt(String.valueOf(matrixIndexes.get(2).charAt(1)))]; 169 | String score_key = generateScoreKey(matrixIndexes); 170 | if (!scoreKeyList.contains(score_key)) { 171 | checkGameBoard(tic1,tac1,toe1,player,score_key); 172 | 173 | } 174 | Log.d(TAG,"processing second set of list from IndexesList"); 175 | 176 | 177 | } 178 | 179 | 180 | private boolean verifyVerticalHorizontalMatch(int row, int column, int player, String mode) { 181 | //horizontal and vertical map contains board indexes which can be obtained from row,column value exampel (3,3) will become 33 and it will have its 182 | String key = String.valueOf(row)+String.valueOf(column); 183 | //whenever a current horizontal or vertical marking is identified as correct, am adding the key to scoredkeylist 184 | List matrixIndexes = new ArrayList(); 185 | if (mode.equals(HORIZONTAL_MODE)) { 186 | //gets indexes required to verify for score in horizontal line, gameboard 187 | matrixIndexes = this.horizontal_map.get(key); 188 | } else { 189 | //gets indexes required to verify for score in vertical line, gameboard 190 | matrixIndexes = this.vertical_map.get(key); 191 | } 192 | int tic = game_board[Integer.parseInt(String.valueOf(matrixIndexes.get(0).charAt(0)))][Integer.parseInt(String.valueOf(matrixIndexes.get(0).charAt(1)))]; 193 | int tac = game_board[Integer.parseInt(String.valueOf(matrixIndexes.get(1).charAt(0)))][Integer.parseInt(String.valueOf(matrixIndexes.get(1).charAt(1)))]; 194 | int toe = game_board[Integer.parseInt(String.valueOf(matrixIndexes.get(2).charAt(0)))][Integer.parseInt(String.valueOf(matrixIndexes.get(2).charAt(1)))]; 195 | String score_key = generateScoreKey(matrixIndexes); 196 | if (!scoreKeyList.contains(score_key)) { 197 | Log.d(TAG, "Score Key:" + score_key); 198 | return checkGameBoard(tic,tac,toe,player,score_key); 199 | } 200 | else { 201 | return false; 202 | } 203 | } 204 | 205 | private boolean checkGameBoard(int tic,int tac ,int toe,int player,String score_key){ 206 | if (player == Constants.HUMAN && tic == Constants.HUMAN && tac == Constants.HUMAN && toe == Constants.HUMAN) { 207 | updateScoreMap(score_key, player); 208 | return true; 209 | } else if (player == Constants.COMPUTER && tic == Constants.COMPUTER && tac == Constants.COMPUTER && toe == Constants.COMPUTER) { 210 | updateScoreMap(score_key, player); 211 | return true; 212 | } 213 | else { 214 | return false; 215 | } 216 | 217 | } 218 | 219 | 220 | 221 | private void updateScoreMap(String score_key, int player) { 222 | //make sure the current score key is already verified and given score. 223 | if (!this.scoreKeyList.contains(score_key)){ 224 | this.scoreKeyList.add(score_key); 225 | if (player==Constants.COMPUTER){ 226 | increementComputerScore(); 227 | boolean is_tie = false; 228 | this.activity_interface.endGame(is_tie,score_key,player); 229 | 230 | //todo communicated with UI display Animation for success and change score 231 | } 232 | else { 233 | increementPlayerScore(); 234 | boolean is_tie = false; 235 | this.activity_interface.endGame(is_tie,score_key,player); 236 | //todo communicated with UI display Animation for success and change score 237 | } 238 | } 239 | 240 | } 241 | 242 | private String generateScoreKey(List verticalIndexes) { 243 | Log.d(TAG,"Scored Key List:"+verticalIndexes); 244 | return verticalIndexes.get(0)+"_"+verticalIndexes.get(1)+"_"+verticalIndexes.get(2); 245 | } 246 | 247 | 248 | public int getPlayer_score() { 249 | return player_score; 250 | } 251 | 252 | public int getComputer_score() { 253 | return computer_score; 254 | } 255 | 256 | public int getTie_score() { 257 | return tie_score; 258 | } 259 | 260 | 261 | private void increementPlayerScore(){ 262 | this.player_score++; 263 | Log.d(TAG,"Player score:"+computer_score); 264 | 265 | } 266 | 267 | private void increementComputerScore(){ 268 | this.computer_score++; 269 | Log.d(TAG,"Computer score:"+computer_score); 270 | } 271 | 272 | public void increementTieScore(){ 273 | this.tie_score++; 274 | Log.d(TAG,"Tie score increemented"); 275 | } 276 | 277 | public String generateMachineMove(){ 278 | //starts with the defence move ocuppying the center index(11) will block 279 | if (!isCenterMatrixIndexOcuppied){ 280 | return "11"; 281 | } 282 | else { 283 | String chosen_key = generateComputerNextMove(); 284 | Log.d(TAG,"Computer Generated Next Move:"+chosen_key); 285 | /* if (!chosen_key.equals(NO_RESULT)){ 286 | int row = Integer.parseInt(String.valueOf(chosen_key.charAt(0))); 287 | int column = Integer.parseInt(String.valueOf(chosen_key.charAt(1))); 288 | addPlayerMove(row,column,Constants.COMPUTER); */ 289 | return chosen_key; 290 | 291 | } 292 | 293 | 294 | } 295 | 296 | private String generateComputerNextMove() { 297 | String human_move_key = String.valueOf(last_move_player_row)+String.valueOf(last_move_player_column); 298 | MachineMoveChoice vertical_move_choice = null; 299 | MachineMoveChoice horizontal_move_choice = null; 300 | MachineMoveChoice diagonal_move_choice = null; 301 | if (vertical_map.containsKey(human_move_key)){ 302 | vertical_move_choice = isBlockStrategyNecessaryVertical(human_move_key,VERTICAL_MODE); 303 | } 304 | else { 305 | vertical_move_choice = new MachineMoveChoice(true); 306 | } 307 | if (horizontal_map.containsKey(human_move_key)){ 308 | horizontal_move_choice = isBlockStrategyNecessaryVertical(human_move_key,HORIZONTAL_MODE); 309 | } 310 | else { 311 | horizontal_move_choice = new MachineMoveChoice(true); 312 | } 313 | if (diagonal_map.containsKey(human_move_key)){ 314 | diagonal_move_choice = isBlockStrategyNecessaryDiagonal(human_move_key); 315 | } 316 | else { 317 | diagonal_move_choice = new MachineMoveChoice(true); 318 | } 319 | 320 | 321 | if (!vertical_move_choice.isNull() && vertical_move_choice.isRedAlert()){ 322 | return vertical_move_choice.getBoardKey(); 323 | } 324 | else if (!horizontal_move_choice.isNull() && horizontal_move_choice.isRedAlert()){ 325 | return horizontal_move_choice.getBoardKey(); 326 | } 327 | else if (!diagonal_move_choice.isNull() && diagonal_move_choice.isRedAlert()){ 328 | return diagonal_move_choice.getBoardKey(); 329 | } 330 | else if (!vertical_move_choice.isNull() && vertical_move_choice.isScore()){ 331 | return vertical_move_choice.getBoardKey(); 332 | } 333 | else if (!horizontal_move_choice.isNull() && horizontal_move_choice.isScore()){ 334 | return horizontal_move_choice.getBoardKey(); 335 | } 336 | else if (!diagonal_move_choice.isNull() && diagonal_move_choice.isScore()){ 337 | return diagonal_move_choice.getBoardKey(); 338 | } 339 | else if (!vertical_move_choice.isNull() && !diagonal_move_choice.isRedAlert()){ 340 | return vertical_move_choice.getBoardKey(); 341 | } 342 | else if (!horizontal_move_choice.isNull() && !horizontal_move_choice.isRedAlert()){ 343 | return horizontal_move_choice.getBoardKey(); 344 | } 345 | else if (!diagonal_move_choice.isNull() && !diagonal_move_choice.isRedAlert()){ 346 | return diagonal_move_choice.getBoardKey(); 347 | } 348 | else { 349 | 350 | return generateRandomMove(); 351 | } 352 | 353 | } 354 | 355 | private String generateRandomMove() { 356 | List un_occupied_place = new ArrayList(); 357 | for (int row = 0; row <3 ; row++) { 358 | for (int column=0;column<3;column++){ 359 | if (game_board[row][column]==Constants.INDEX_UNOCCUPIED){ 360 | String key = String.valueOf(row)+String.valueOf(column); 361 | un_occupied_place.add(key); 362 | } 363 | } 364 | } 365 | Collections.shuffle(un_occupied_place); 366 | return un_occupied_place.get(0); 367 | } 368 | 369 | private MachineMoveChoice isBlockStrategyNecessaryDiagonal(String human_move_key) { 370 | 371 | if (isCenterMatrixIndexOcuppied && game_board[1][1]==Constants.HUMAN){ 372 | return isDiagonalBlockStrategy(human_move_key); 373 | } 374 | else if (isCenterMatrixIndexOcuppied && game_board[1][1]==Constants.COMPUTER){ 375 | return isDiagonalBlockStrategy(human_move_key); 376 | } 377 | else { 378 | return getMachineOccupiablePosition(this.diagonal_map.get(human_move_key)); 379 | } 380 | 381 | } 382 | 383 | private MachineMoveChoice isDiagonalBlockStrategy(String human_move_key) { 384 | if (human_move_key.equals("11")){ 385 | //diagonal can be on both side have to check 386 | List matrixIndexes = new ArrayList(); 387 | matrixIndexes = this.diagonal_map.get("11"); 388 | List diagonal_one = matrixIndexes.subList(0,3); 389 | List diagonal_two = matrixIndexes.subList(3,6); 390 | MachineMoveChoice machine_diagonal_one = verifyDiagonalMatch(diagonal_one); 391 | MachineMoveChoice machine_daigonal_two = verifyDiagonalMatch(diagonal_two); 392 | if (machine_diagonal_one.isRedAlert()){ 393 | return machine_diagonal_one; 394 | } 395 | else if (machine_daigonal_two.isRedAlert()){ 396 | return machine_daigonal_two; 397 | } 398 | else { 399 | return machine_diagonal_one; 400 | } 401 | } 402 | else { 403 | List matrixIndexes = this.diagonal_map.get(human_move_key); 404 | return verifyDiagonalMatch(matrixIndexes); 405 | } 406 | } 407 | 408 | private MachineMoveChoice verifyDiagonalMatch(List matrixIndexes) { 409 | int tic = game_board[Integer.parseInt(String.valueOf(matrixIndexes.get(0).charAt(0)))][Integer.parseInt(String.valueOf(matrixIndexes.get(0).charAt(1)))]; 410 | int tac = game_board[Integer.parseInt(String.valueOf(matrixIndexes.get(1).charAt(0)))][Integer.parseInt(String.valueOf(matrixIndexes.get(1).charAt(1)))]; 411 | int toe = game_board[Integer.parseInt(String.valueOf(matrixIndexes.get(2).charAt(0)))][Integer.parseInt(String.valueOf(matrixIndexes.get(2).charAt(1)))]; 412 | if (tic == Constants.HUMAN && tac == Constants.HUMAN && toe == Constants.INDEX_UNOCCUPIED){ 413 | Log.d(TAG,"Requires Diagonal Block At:"+matrixIndexes.get(0)); 414 | return new MachineMoveChoice(true,matrixIndexes.get(2),false); 415 | } 416 | else if (tic == Constants.COMPUTER && tac == Constants.COMPUTER && toe == Constants.INDEX_UNOCCUPIED){ 417 | Log.d(TAG,"Requires Diagonal Block At:"+matrixIndexes.get(0)); 418 | MachineMoveChoice record = new MachineMoveChoice(false,matrixIndexes.get(2),false); 419 | record.setScore(true); 420 | return record; 421 | } 422 | 423 | else if (tic==Constants.HUMAN && tac==Constants.INDEX_UNOCCUPIED && toe==Constants.HUMAN){ 424 | Log.d(TAG,"Requires Diagonal Block At:"+matrixIndexes.get(1)); 425 | return new MachineMoveChoice(true,matrixIndexes.get(1),false); 426 | } 427 | else if (tic==Constants.COMPUTER && tac==Constants.INDEX_UNOCCUPIED && toe==Constants.COMPUTER){ 428 | Log.d(TAG,"Requires Diagonal Block At:"+matrixIndexes.get(1)); 429 | MachineMoveChoice record = new MachineMoveChoice(false,matrixIndexes.get(1),false); 430 | record.setScore(true); 431 | return record; 432 | } 433 | 434 | else if (tic==Constants.INDEX_UNOCCUPIED && tac==Constants.HUMAN && toe==Constants.HUMAN){ 435 | Log.d(TAG,"Requires Diagonal Block At:"+matrixIndexes.get(0)); 436 | return new MachineMoveChoice(true,matrixIndexes.get(0),false); 437 | } 438 | else if (tic==Constants.INDEX_UNOCCUPIED && tac==Constants.COMPUTER && toe==Constants.COMPUTER){ 439 | Log.d(TAG,"Requires Diagonal Block At:"+matrixIndexes.get(0)); 440 | MachineMoveChoice record = new MachineMoveChoice(false,matrixIndexes.get(0),false); 441 | record.setScore(true); 442 | return record; 443 | 444 | } 445 | else { 446 | return getMachineOccupiablePosition(matrixIndexes); 447 | } 448 | } 449 | 450 | private MachineMoveChoice isBlockStrategyNecessaryVertical(String human_move_key,String mode) { 451 | List matrixIndexes = new ArrayList(); 452 | if (mode.equals(VERTICAL_MODE)){ 453 | matrixIndexes = this.vertical_map.get(human_move_key); 454 | } 455 | else { 456 | matrixIndexes = this.horizontal_map.get(human_move_key); 457 | } 458 | int tic = game_board[Integer.parseInt(String.valueOf(matrixIndexes.get(0).charAt(0)))][Integer.parseInt(String.valueOf(matrixIndexes.get(0).charAt(1)))]; 459 | int tac = game_board[Integer.parseInt(String.valueOf(matrixIndexes.get(1).charAt(0)))][Integer.parseInt(String.valueOf(matrixIndexes.get(1).charAt(1)))]; 460 | int toe = game_board[Integer.parseInt(String.valueOf(matrixIndexes.get(2).charAt(0)))][Integer.parseInt(String.valueOf(matrixIndexes.get(2).charAt(1)))]; 461 | if (tic == Constants.INDEX_UNOCCUPIED && tac == Constants.HUMAN && toe == Constants.HUMAN){ 462 | Log.d(TAG,"Requires Vertical Block At:"+matrixIndexes.get(0)); 463 | return new MachineMoveChoice(true,matrixIndexes.get(0),false); 464 | } 465 | else if (tic == Constants.INDEX_UNOCCUPIED && tac == Constants.COMPUTER && toe == Constants.COMPUTER){ 466 | Log.d(TAG,"Score Vertical Block:"+matrixIndexes.get(0)); 467 | MachineMoveChoice record = new MachineMoveChoice(false,matrixIndexes.get(0),false); 468 | record.setScore(true); 469 | return record; 470 | } 471 | else if(tic == Constants.HUMAN && tac == Constants.INDEX_UNOCCUPIED && toe == Constants.HUMAN) { 472 | Log.d(TAG,"Requires Vertical Block:"+matrixIndexes.get(1)); 473 | return new MachineMoveChoice(true,matrixIndexes.get(1),false); 474 | } 475 | else if(tic == Constants.COMPUTER && tac == Constants.INDEX_UNOCCUPIED && toe == Constants.COMPUTER) { 476 | Log.d(TAG,"Requires Vertical Score:"+matrixIndexes.get(1)); 477 | MachineMoveChoice record = new MachineMoveChoice(false,matrixIndexes.get(1),false); 478 | record.setScore(true); 479 | return record; 480 | } 481 | else if (tic == Constants.HUMAN && tac == Constants.HUMAN && toe == Constants.INDEX_UNOCCUPIED){ 482 | Log.d(TAG,"Requires Vertical Block:"+matrixIndexes.get(2)); 483 | return new MachineMoveChoice(true,matrixIndexes.get(2),false); 484 | } 485 | else if (tic == Constants.COMPUTER && tac == Constants.COMPUTER && toe == Constants.INDEX_UNOCCUPIED) { 486 | Log.d(TAG, "Requires Vertical Score:" + matrixIndexes.get(2)); 487 | MachineMoveChoice record = new MachineMoveChoice(false,matrixIndexes.get(2),false); 488 | record.setScore(true); 489 | return record; 490 | } 491 | 492 | else if (tic == Constants.HUMAN && tac == Constants.INDEX_UNOCCUPIED && toe==Constants.INDEX_UNOCCUPIED) { 493 | String random_key = chooseRandomInTwoNumbers(new ArrayList(Arrays.asList(matrixIndexes.get(1),matrixIndexes.get(2)))); 494 | Log.d(TAG,"Doesn't Require Vertical Block:"+random_key); 495 | return new MachineMoveChoice(false,random_key,false); 496 | 497 | } 498 | else if (tic == Constants.INDEX_UNOCCUPIED && tac==Constants.HUMAN && toe==Constants.INDEX_UNOCCUPIED){ 499 | String random_key = chooseRandomInTwoNumbers(new ArrayList(Arrays.asList(matrixIndexes.get(0),matrixIndexes.get(2)))); 500 | Log.d(TAG,"Doesn't Require Vertical Block:"+random_key); 501 | return new MachineMoveChoice(false,random_key,false); 502 | 503 | } 504 | else if (tic == Constants.INDEX_UNOCCUPIED && tac==Constants.INDEX_UNOCCUPIED && toe==Constants.HUMAN){ 505 | String random_key = chooseRandomInTwoNumbers(new ArrayList(Arrays.asList(matrixIndexes.get(0),matrixIndexes.get(1)))); 506 | Log.d(TAG,"Doesn't Require Vertical Block:"+random_key); 507 | return new MachineMoveChoice(false,random_key,false); 508 | 509 | } 510 | else { 511 | Log.d(TAG,"Getting Occupiable position from matrixIndexes:"+matrixIndexes); 512 | return getMachineOccupiablePosition(matrixIndexes); 513 | } 514 | } 515 | 516 | private MachineMoveChoice getMachineOccupiablePosition(List matrixIndexes) { 517 | // set the field isNull to false; 518 | MachineMoveChoice machine_move = new MachineMoveChoice(true); 519 | List unoccupied_indexes = new ArrayList(); 520 | for (String key:matrixIndexes) { 521 | int row = Integer.parseInt(String.valueOf(key.charAt(0))); 522 | int column = Integer.parseInt(String.valueOf(key.charAt(1))); 523 | if (game_board[row][column]==Constants.INDEX_UNOCCUPIED){ 524 | // machine_move = new MachineMoveChoice(false,key,false); 525 | unoccupied_indexes.add(key); 526 | } 527 | 528 | } 529 | if (unoccupied_indexes.size()>0){ 530 | Collections.shuffle(unoccupied_indexes); 531 | String shuffled_unoccupied_key = unoccupied_indexes.get(0); 532 | machine_move.setBoardKey(shuffled_unoccupied_key); 533 | machine_move.setScore(false); 534 | machine_move.setNull(false); 535 | machine_move.setRedAlert(false); 536 | 537 | } 538 | 539 | return machine_move; 540 | } 541 | 542 | private String chooseRandomInTwoNumbers(ArrayList integers) { 543 | Collections.shuffle(integers); 544 | return integers.get(0); 545 | } 546 | 547 | private void setHorizontalMap() { 548 | horizontal_map.put("00",new ArrayList(Arrays.asList("00","01","02"))); 549 | horizontal_map.put("01",new ArrayList(Arrays.asList("00","01","02"))); 550 | horizontal_map.put("02",new ArrayList(Arrays.asList("00","01","02"))); 551 | horizontal_map.put("10",new ArrayList(Arrays.asList("10","11","12"))); 552 | horizontal_map.put("11",new ArrayList(Arrays.asList("10","11","12"))); 553 | horizontal_map.put("12",new ArrayList(Arrays.asList("10","11","12"))); 554 | horizontal_map.put("20",new ArrayList(Arrays.asList("20","21","22"))); 555 | horizontal_map.put("21",new ArrayList(Arrays.asList("20","21","22"))); 556 | horizontal_map.put("22",new ArrayList(Arrays.asList("20","21","22"))); 557 | Log.d(TAG,"Horizontal Map for each index set"); 558 | } 559 | 560 | private void setVerticalMap(){ 561 | // vertical map for column 0 562 | vertical_map.put("00",new ArrayList(Arrays.asList("00","10","20"))); 563 | vertical_map.put("10",new ArrayList(Arrays.asList("00","10","20"))); 564 | vertical_map.put("20",new ArrayList(Arrays.asList("00","10","20"))); 565 | //vertical map for column 1 566 | vertical_map.put("01",new ArrayList(Arrays.asList("01","11","21"))); 567 | vertical_map.put("11",new ArrayList(Arrays.asList("01","11","21"))); 568 | vertical_map.put("21",new ArrayList(Arrays.asList("01","11","21"))); 569 | //vertical map for column 2 570 | vertical_map.put("02",new ArrayList(Arrays.asList("02","12","22"))); 571 | vertical_map.put("12",new ArrayList(Arrays.asList("02","12","22"))); 572 | vertical_map.put("22",new ArrayList(Arrays.asList("02","12","22"))); 573 | Log.d(TAG,"Vertical Map set"); 574 | } 575 | 576 | private void setDiagonalMap(){ 577 | diagonal_map.put("00",new ArrayList(Arrays.asList("00","11","22"))); 578 | diagonal_map.put("11",new ArrayList(Arrays.asList("00","11","22","02","11","20"))); 579 | diagonal_map.put("22",new ArrayList(Arrays.asList("00","11","22"))); 580 | diagonal_map.put("20",new ArrayList(Arrays.asList("02","11","20"))); 581 | diagonal_map.put("02",new ArrayList(Arrays.asList("02","11","20"))); 582 | Log.d(TAG,"Diagonal Map"); 583 | 584 | } 585 | 586 | 587 | 588 | } 589 | --------------------------------------------------------------------------------