├── app
├── .gitignore
├── src
│ └── main
│ │ ├── res
│ │ ├── drawable
│ │ │ ├── pigeon_piece1.png
│ │ │ ├── pigeon_piece2.png
│ │ │ ├── pigeon_piece3.png
│ │ │ ├── pigeon_piece4.png
│ │ │ ├── pigeon_piece5.png
│ │ │ ├── pigeon_piece6.png
│ │ │ ├── pigeon_piece7.png
│ │ │ ├── pigeon_piece8.png
│ │ │ └── pigeon_piece9.png
│ │ ├── 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
│ │ │ ├── strings.xml
│ │ │ ├── colors.xml
│ │ │ └── styles.xml
│ │ └── layout
│ │ │ └── activity_sample_puzzle.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── com
│ │ └── davenotdavid
│ │ └── samplepuzzle
│ │ ├── TileImageAdapter.kt
│ │ ├── GestureDetectGridView.kt
│ │ └── SamplePuzzleActivity.kt
├── build.gradle
└── proguard-rules.pro
├── settings.gradle
├── .idea
├── copyright
│ └── profiles_settings.xml
├── caches
│ └── build_file_checksums.ser
├── vcs.xml
├── runConfigurations.xml
├── modules.xml
├── compiler.xml
├── gradle.xml
├── misc.xml
├── jarRepositories.xml
└── codeStyles
│ └── Project.xml
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── gradle.properties
├── README.md
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/davenotdavid/sample-puzzle/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/.idea/caches/build_file_checksums.ser:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/davenotdavid/sample-puzzle/HEAD/.idea/caches/build_file_checksums.ser
--------------------------------------------------------------------------------
/app/src/main/res/drawable/pigeon_piece1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/davenotdavid/sample-puzzle/HEAD/app/src/main/res/drawable/pigeon_piece1.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/pigeon_piece2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/davenotdavid/sample-puzzle/HEAD/app/src/main/res/drawable/pigeon_piece2.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/pigeon_piece3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/davenotdavid/sample-puzzle/HEAD/app/src/main/res/drawable/pigeon_piece3.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/pigeon_piece4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/davenotdavid/sample-puzzle/HEAD/app/src/main/res/drawable/pigeon_piece4.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/pigeon_piece5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/davenotdavid/sample-puzzle/HEAD/app/src/main/res/drawable/pigeon_piece5.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/pigeon_piece6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/davenotdavid/sample-puzzle/HEAD/app/src/main/res/drawable/pigeon_piece6.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/pigeon_piece7.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/davenotdavid/sample-puzzle/HEAD/app/src/main/res/drawable/pigeon_piece7.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/pigeon_piece8.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/davenotdavid/sample-puzzle/HEAD/app/src/main/res/drawable/pigeon_piece8.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/pigeon_piece9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/davenotdavid/sample-puzzle/HEAD/app/src/main/res/drawable/pigeon_piece9.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/davenotdavid/sample-puzzle/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/davenotdavid/sample-puzzle/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/davenotdavid/sample-puzzle/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/davenotdavid/sample-puzzle/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/davenotdavid/sample-puzzle/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/davenotdavid/sample-puzzle/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/davenotdavid/sample-puzzle/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/davenotdavid/sample-puzzle/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/davenotdavid/sample-puzzle/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/davenotdavid/sample-puzzle/HEAD/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 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Sample Puzzle
3 |
4 | Invalid move
5 | YOU WIN!
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Oct 12 20:03:52 EDT 2020
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-6.1.1-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_sample_puzzle.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/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 | android.enableJetifier=true
13 | android.useAndroidX=true
14 | org.gradle.jvmargs=-Xmx1536m
15 |
16 | # When configured, Gradle will run in incubating parallel mode.
17 | # This option should only be used with decoupled projects. More details, visit
18 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
19 | # org.gradle.parallel=true
20 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 | apply plugin: 'kotlin-android'
3 | apply plugin: 'kotlin-android-extensions'
4 |
5 | android {
6 | compileSdkVersion 29
7 | defaultConfig {
8 | applicationId "com.davenotdavid.samplepuzzle"
9 | minSdkVersion 16
10 | targetSdkVersion 29
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | implementation fileTree(dir: 'libs', include: ['*.jar'])
24 | implementation 'androidx.appcompat:appcompat:1.2.0'
25 |
26 | implementation "androidx.core:core-ktx:+"
27 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
28 | }
29 |
30 | repositories {
31 | mavenCentral()
32 | }
33 |
--------------------------------------------------------------------------------
/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 C:\Users\Dave\AppData\Local\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/src/main/java/com/davenotdavid/samplepuzzle/TileImageAdapter.kt:
--------------------------------------------------------------------------------
1 | package com.davenotdavid.samplepuzzle
2 |
3 | import android.view.View
4 | import android.view.ViewGroup
5 | import android.widget.AbsListView
6 | import android.widget.BaseAdapter
7 | import android.widget.ImageView
8 |
9 | class TileImageAdapter(private val tileImages: List,
10 | private val boardColumnWidth: Int,
11 | private val boardColumnHeight: Int) : BaseAdapter() {
12 |
13 | override fun getCount(): Int = tileImages.size
14 |
15 | override fun getItem(position: Int): ImageView = tileImages[position]
16 |
17 | override fun getItemId(position: Int): Long = position.toLong()
18 |
19 | override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
20 | val tileImageView = if (convertView == null) {
21 | tileImages[position]
22 | } else {
23 | convertView as ImageView
24 | }
25 | tileImageView.layoutParams = AbsListView.LayoutParams(boardColumnWidth, boardColumnHeight)
26 |
27 | return tileImageView
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 2D-Puzzle Sample
2 | 💁♂️ Sample project that's basically a 2D-puzzle with a Bejeweled-like swipe UI that's responsive for all Android device screens.
3 |
4 | **🎉 UPDATE AS OF 10/12/2020: Considering that this project is pretty old and it's now 2020, I've pushed out a branch that contains refactored code (i.e. newer SDK versioning, AndroidX migration, Kotlin converted, a more OOP approach, and etc.) here: https://github.com/DaveNOTDavid/sample-puzzle/tree/refactor_attempt**
5 |
6 | 👾 Among Us edition: https://github.com/DaveNOTDavid/sample-puzzle/tree/refactor_attempt_edition_among_us
7 |
8 | 📺 YouTube tutorial video: https://www.youtube.com/watch?v=YKbFx8PDTIo
9 |
10 | 
11 |
12 | # Donation
13 |
14 | 🙏 *Curious for more? Spare me a coffee to support what I do today:*
15 |
16 |
17 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/.idea/jarRepositories.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/app/src/main/java/com/davenotdavid/samplepuzzle/GestureDetectGridView.kt:
--------------------------------------------------------------------------------
1 | package com.davenotdavid.samplepuzzle
2 |
3 | import android.annotation.TargetApi
4 | import android.content.Context
5 | import android.os.Build
6 | import android.util.AttributeSet
7 | import android.view.GestureDetector
8 | import android.view.GestureDetector.SimpleOnGestureListener
9 | import android.view.MotionEvent
10 | import android.widget.GridView
11 | import kotlin.math.abs
12 |
13 | class GestureDetectGridView : GridView {
14 |
15 | companion object {
16 | private const val SWIPE_MIN_DISTANCE = 100
17 | private const val SWIPE_MAX_OFF_PATH = 100
18 | private const val SWIPE_THRESHOLD_VELOCITY = 100
19 | }
20 |
21 | private var gestureDetector: GestureDetector? = null
22 | private var swipeListener: OnSwipeListener? = null
23 | private var flingConfirmed = false
24 | private var touchX = 0f
25 | private var touchY = 0f
26 |
27 | constructor(context: Context) : super(context) {
28 | init(context)
29 | }
30 |
31 | constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
32 | init(context)
33 | }
34 |
35 | constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
36 | init(context)
37 | }
38 |
39 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) // API 21
40 | constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
41 | init(context)
42 | }
43 |
44 | override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
45 | val action = ev.actionMasked
46 | gestureDetector?.onTouchEvent(ev)
47 | if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
48 | flingConfirmed = false
49 | } else if (action == MotionEvent.ACTION_DOWN) {
50 | touchX = ev.x
51 | touchY = ev.y
52 | } else {
53 | if (flingConfirmed) {
54 | return true
55 | }
56 | val dX = Math.abs(ev.x - touchX)
57 | val dY = Math.abs(ev.y - touchY)
58 | if (dX > SWIPE_MIN_DISTANCE || dY > SWIPE_MIN_DISTANCE) {
59 | flingConfirmed = true
60 | return true
61 | }
62 | }
63 |
64 | return super.onInterceptTouchEvent(ev)
65 | }
66 |
67 | override fun onTouchEvent(ev: MotionEvent): Boolean = gestureDetector?.onTouchEvent(ev) ?: false
68 |
69 | fun setOnSwipeListener(onSwipeListener: OnSwipeListener?) {
70 | swipeListener = onSwipeListener
71 | }
72 |
73 | interface OnSwipeListener {
74 | fun onSwipe(direction: SwipeDirections, position: Int)
75 | }
76 |
77 | private fun init(context: Context) {
78 | gestureDetector = GestureDetector(context, object : SimpleOnGestureListener() {
79 | override fun onDown(event: MotionEvent): Boolean {
80 | return true
81 | }
82 |
83 | override fun onFling(e1: MotionEvent, e2: MotionEvent, velocityX: Float, velocityY: Float): Boolean {
84 | val position = pointToPosition(Math.round(e1.x), Math.round(e1.y))
85 | if (abs(e1.y - e2.y) > SWIPE_MAX_OFF_PATH) {
86 | if (abs(e1.x - e2.x) > SWIPE_MAX_OFF_PATH || abs(velocityY) < SWIPE_THRESHOLD_VELOCITY) {
87 | return false
88 | }
89 | if (e1.y - e2.y > SWIPE_MIN_DISTANCE) {
90 | swipeListener?.onSwipe(SwipeDirections.UP, position)
91 | } else if (e2.y - e1.y > SWIPE_MIN_DISTANCE) {
92 | swipeListener?.onSwipe(SwipeDirections.DOWN, position)
93 | }
94 | } else {
95 | if (abs(velocityX) < SWIPE_THRESHOLD_VELOCITY) {
96 | return false
97 | }
98 | if (e1.x - e2.x > SWIPE_MIN_DISTANCE) {
99 | swipeListener?.onSwipe(SwipeDirections.LEFT, position)
100 | } else if (e2.x - e1.x > SWIPE_MIN_DISTANCE) {
101 | swipeListener?.onSwipe(SwipeDirections.RIGHT, position)
102 | }
103 | }
104 |
105 | return super.onFling(e1, e2, velocityX, velocityY)
106 | }
107 | })
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 | xmlns:android
32 |
33 | ^$
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 | xmlns:.*
43 |
44 | ^$
45 |
46 |
47 | BY_NAME
48 |
49 |
50 |
51 |
52 |
53 |
54 | .*:id
55 |
56 | http://schemas.android.com/apk/res/android
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 | .*:name
66 |
67 | http://schemas.android.com/apk/res/android
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 | name
77 |
78 | ^$
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 | style
88 |
89 | ^$
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 | .*
99 |
100 | ^$
101 |
102 |
103 | BY_NAME
104 |
105 |
106 |
107 |
108 |
109 |
110 | .*
111 |
112 | http://schemas.android.com/apk/res/android
113 |
114 |
115 | ANDROID_ATTRIBUTE_ORDER
116 |
117 |
118 |
119 |
120 |
121 |
122 | .*
123 |
124 | .*
125 |
126 |
127 | BY_NAME
128 |
129 |
130 |
131 |
132 |
133 |
134 |
--------------------------------------------------------------------------------
/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/java/com/davenotdavid/samplepuzzle/SamplePuzzleActivity.kt:
--------------------------------------------------------------------------------
1 | package com.davenotdavid.samplepuzzle
2 |
3 | import android.content.Context
4 | import android.os.Bundle
5 | import android.view.ViewTreeObserver.OnGlobalLayoutListener
6 | import android.widget.ImageView
7 | import android.widget.Toast
8 | import androidx.annotation.StringRes
9 | import androidx.appcompat.app.AppCompatActivity
10 | import com.davenotdavid.samplepuzzle.GestureDetectGridView.OnSwipeListener
11 | import kotlinx.android.synthetic.main.activity_sample_puzzle.*
12 | import java.util.Random
13 |
14 | enum class SwipeDirections {
15 | UP, DOWN, LEFT, RIGHT
16 | }
17 |
18 | class SamplePuzzleActivity : AppCompatActivity() {
19 |
20 | companion object {
21 | private const val TOTAL_COLUMNS = 3
22 | private const val DIMENSIONS = TOTAL_COLUMNS * TOTAL_COLUMNS
23 |
24 | private var boardColumnWidth = 0
25 | private var boardColumnHeight = 0
26 | }
27 |
28 | private val tileListIndexes = mutableListOf()
29 |
30 | private val isSolved: Boolean
31 | get() {
32 | var solved = false
33 | for (i in tileListIndexes.indices) {
34 | if (tileListIndexes[i] == i) {
35 | solved = true
36 | } else {
37 | solved = false
38 | break
39 | }
40 | }
41 |
42 | return solved
43 | }
44 |
45 | override fun onCreate(savedInstanceState: Bundle?) {
46 | super.onCreate(savedInstanceState)
47 | setContentView(R.layout.activity_sample_puzzle)
48 |
49 | init()
50 | scrambleTileBoard()
51 | setTileBoardDimensions()
52 | }
53 |
54 | private fun init() {
55 | gesture_detect_grid_view.apply {
56 | numColumns = TOTAL_COLUMNS
57 | setOnSwipeListener(object : OnSwipeListener {
58 | override fun onSwipe(direction: SwipeDirections, position: Int) {
59 | moveTiles(direction, position)
60 | }
61 | })
62 | }
63 |
64 | tileListIndexes += 0 until DIMENSIONS
65 | }
66 |
67 | private fun scrambleTileBoard() {
68 | var index: Int
69 | var tempIndex: Int
70 | val random = Random()
71 |
72 | for (i in tileListIndexes.size - 1 downTo 1) {
73 | index = random.nextInt(i + 1)
74 | tempIndex = tileListIndexes[index]
75 | tileListIndexes[index] = tileListIndexes[i]
76 | tileListIndexes[i] = tempIndex
77 | }
78 | }
79 |
80 | private fun setTileBoardDimensions() {
81 | val observer = gesture_detect_grid_view.viewTreeObserver
82 | observer.addOnGlobalLayoutListener(object : OnGlobalLayoutListener {
83 | override fun onGlobalLayout() {
84 | gesture_detect_grid_view.viewTreeObserver.removeOnGlobalLayoutListener(this)
85 |
86 | val displayWidth = gesture_detect_grid_view.measuredWidth
87 | val displayHeight = gesture_detect_grid_view.measuredHeight
88 | val statusbarHeight = getStatusBarHeight(applicationContext)
89 | val requiredHeight = displayHeight - statusbarHeight
90 |
91 | boardColumnWidth = displayWidth / TOTAL_COLUMNS
92 | boardColumnHeight = requiredHeight / TOTAL_COLUMNS
93 |
94 | displayTileBoard()
95 | }
96 | })
97 | }
98 |
99 | private fun getStatusBarHeight(context: Context): Int {
100 | val resources = context.resources
101 | var result = 0
102 | val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
103 | if (resourceId > 0) {
104 | result = resources.getDimensionPixelSize(resourceId)
105 | }
106 |
107 | return result
108 | }
109 |
110 | /**
111 | * Used for both init and every time a new swap move is made by the user.
112 | */
113 | private fun displayTileBoard() {
114 | val tileImages = mutableListOf()
115 | var tileImage: ImageView
116 |
117 | tileListIndexes.forEach { i ->
118 | tileImage = ImageView(this)
119 |
120 | when (i) {
121 | 0 -> tileImage.setBackgroundResource(R.drawable.pigeon_piece1)
122 | 1 -> tileImage.setBackgroundResource(R.drawable.pigeon_piece2)
123 | 2 -> tileImage.setBackgroundResource(R.drawable.pigeon_piece3)
124 | 3 -> tileImage.setBackgroundResource(R.drawable.pigeon_piece4)
125 | 4 -> tileImage.setBackgroundResource(R.drawable.pigeon_piece5)
126 | 5 -> tileImage.setBackgroundResource(R.drawable.pigeon_piece6)
127 | 6 -> tileImage.setBackgroundResource(R.drawable.pigeon_piece7)
128 | 7 -> tileImage.setBackgroundResource(R.drawable.pigeon_piece8)
129 | 8 -> tileImage.setBackgroundResource(R.drawable.pigeon_piece9)
130 | }
131 |
132 | tileImages.add(tileImage)
133 | }
134 |
135 | gesture_detect_grid_view.adapter = TileImageAdapter(tileImages, boardColumnWidth, boardColumnHeight)
136 | }
137 |
138 | private fun displayToast(@StringRes textResId: Int) {
139 | Toast.makeText(this, getString(textResId), Toast.LENGTH_SHORT).show()
140 | }
141 |
142 | private fun moveTiles(direction: SwipeDirections, position: Int) {
143 | // Upper-left-corner tile
144 | if (position == 0) {
145 | when (direction) {
146 | SwipeDirections.RIGHT -> swapTile(position, 1)
147 | SwipeDirections.DOWN -> swapTile(position, TOTAL_COLUMNS)
148 | else -> displayToast(R.string.invalid_move)
149 | }
150 | // Upper-center tiles
151 | } else if (position > 0 && position < TOTAL_COLUMNS - 1) {
152 | when (direction) {
153 | SwipeDirections.LEFT -> swapTile(position, -1)
154 | SwipeDirections.DOWN -> swapTile(position, TOTAL_COLUMNS)
155 | SwipeDirections.RIGHT -> swapTile(position, 1)
156 | else -> displayToast(R.string.invalid_move)
157 | }
158 | // Upper-right-corner tile
159 | } else if (position == TOTAL_COLUMNS - 1) {
160 | when (direction) {
161 | SwipeDirections.LEFT -> swapTile(position, -1)
162 | SwipeDirections.DOWN -> swapTile(position, TOTAL_COLUMNS)
163 | else -> displayToast(R.string.invalid_move)
164 | }
165 | // Left-side tiles
166 | } else if (position > TOTAL_COLUMNS - 1 && position < DIMENSIONS - TOTAL_COLUMNS && position % TOTAL_COLUMNS == 0) {
167 | when (direction) {
168 | SwipeDirections.UP -> swapTile(position, -TOTAL_COLUMNS)
169 | SwipeDirections.RIGHT -> swapTile(position, 1)
170 | SwipeDirections.DOWN -> swapTile(position, TOTAL_COLUMNS)
171 | else -> displayToast(R.string.invalid_move)
172 | }
173 | // Right-side AND bottom-right-corner tiles
174 | } else if (position == TOTAL_COLUMNS * 2 - 1 || position == TOTAL_COLUMNS * 3 - 1) {
175 | when (direction) {
176 | SwipeDirections.UP -> swapTile(position, -TOTAL_COLUMNS)
177 | SwipeDirections.LEFT -> swapTile(position, -1)
178 | SwipeDirections.DOWN -> {
179 | // Tolerates only the right-side tiles to swap downwards as opposed to the bottom-
180 | // right-corner tile.
181 | if (position <= DIMENSIONS - TOTAL_COLUMNS - 1) {
182 | swapTile(position, TOTAL_COLUMNS)
183 | } else {
184 | displayToast(R.string.invalid_move)
185 | }
186 | }
187 | else -> displayToast(R.string.invalid_move)
188 | }
189 | // Bottom-left corner tile
190 | } else if (position == DIMENSIONS - TOTAL_COLUMNS) {
191 | when (direction) {
192 | SwipeDirections.UP -> swapTile(position, -TOTAL_COLUMNS)
193 | SwipeDirections.RIGHT -> swapTile(position, 1)
194 | else -> displayToast(R.string.invalid_move)
195 | }
196 | // Bottom-center tiles
197 | } else if (position < DIMENSIONS - 1 && position > DIMENSIONS - TOTAL_COLUMNS) {
198 | when (direction) {
199 | SwipeDirections.UP -> swapTile(position, -TOTAL_COLUMNS)
200 | SwipeDirections.LEFT -> swapTile(position, -1)
201 | SwipeDirections.RIGHT -> swapTile(position, 1)
202 | else -> displayToast(R.string.invalid_move)
203 | }
204 | // Center tiles
205 | } else {
206 | when (direction) {
207 | SwipeDirections.UP -> swapTile(position, -TOTAL_COLUMNS)
208 | SwipeDirections.LEFT -> swapTile(position, -1)
209 | SwipeDirections.RIGHT -> swapTile(position, 1)
210 | else -> swapTile(position, TOTAL_COLUMNS)
211 | }
212 | }
213 | }
214 |
215 | private fun swapTile(currentPosition: Int, swap: Int) {
216 | val newPosition = tileListIndexes[currentPosition + swap]
217 | tileListIndexes[currentPosition + swap] = tileListIndexes[currentPosition]
218 | tileListIndexes[currentPosition] = newPosition
219 | displayTileBoard()
220 |
221 | if (isSolved) {
222 | displayToast(R.string.winner)
223 | }
224 | }
225 | }
226 |
--------------------------------------------------------------------------------