0
votes

I have 6 image buttons with drawables, I'm trying to compare them using getDrawable(). Here's my xml code

'''

< ?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TicTacToe">

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="25dp"
    android:layout_marginTop="147dp"
    android:layout_marginEnd="5dp"
    android:layout_marginBottom="14dp"
    android:onClick="change"
    app:layout_constraintBottom_toTopOf="@+id/imageButton2"
    app:layout_constraintEnd_toStartOf="@+id/imageButton01"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/blank" />

<ImageButton
    android:id="@+id/imageButton01"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginTop="147dp"
    android:layout_marginEnd="5dp"
    android:layout_marginBottom="14dp"
    android:onClick="change"
    app:layout_constraintBottom_toTopOf="@+id/imageButton02"
    app:layout_constraintEnd_toStartOf="@+id/imageButton001"
    app:layout_constraintStart_toEndOf="@+id/imageButton1"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/blank" />

<ImageButton
    android:id="@+id/imageButton2"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="24dp"
    android:layout_marginEnd="6dp"
    android:layout_marginBottom="10dp"
    android:onClick="change"
    app:layout_constraintBottom_toTopOf="@+id/imageButton3"
    app:layout_constraintEnd_toStartOf="@+id/imageButton02"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageButton1"
    app:srcCompat="@drawable/blank" />

<ImageButton
    android:id="@+id/imageButton001"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginTop="148dp"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="13dp"
    android:onClick="change"
    app:layout_constraintBottom_toTopOf="@+id/imageButton002"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/imageButton01"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/blank" />

<ImageButton
    android:id="@+id/imageButton02"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginEnd="7dp"
    android:layout_marginBottom="10dp"
    android:onClick="change"
    app:layout_constraintBottom_toTopOf="@+id/imageButton03"
    app:layout_constraintEnd_toStartOf="@+id/imageButton002"
    app:layout_constraintStart_toEndOf="@+id/imageButton2"
    app:layout_constraintTop_toBottomOf="@+id/imageButton01"
    app:srcCompat="@drawable/blank" />

<ImageButton
    android:id="@+id/imageButton002"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginEnd="14dp"
    android:layout_marginBottom="10dp"
    android:onClick="change"
    app:layout_constraintBottom_toTopOf="@+id/imageButton003"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/imageButton02"
    app:layout_constraintTop_toBottomOf="@+id/imageButton001"
    app:srcCompat="@drawable/blank" />

<ImageButton
    android:id="@+id/imageButton3"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="24dp"
    android:layout_marginEnd="6dp"
    android:layout_marginBottom="200dp"
    android:onClick="change"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/imageButton03"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageButton2"
    app:srcCompat="@drawable/blank" />

<ImageButton
    android:id="@+id/imageButton03"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginEnd="7dp"
    android:layout_marginBottom="200dp"
    android:onClick="change"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/imageButton003"
    app:layout_constraintStart_toEndOf="@+id/imageButton3"
    app:layout_constraintTop_toBottomOf="@+id/imageButton02"
    app:srcCompat="@drawable/blank" />

<ImageButton
    android:id="@+id/imageButton003"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginEnd="14dp"
    android:layout_marginBottom="200dp"
    android:onClick="change"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/imageButton03"
    app:layout_constraintTop_toBottomOf="@+id/imageButton002"
    app:srcCompat="@drawable/blank" />

</androidx.constraintlayout.widget.ConstraintLayout

'''

kotlin code

'''

class TicTacToe : AppCompatActivity() {

var spot1: ImageButton? =null
var spot01: ImageButton? =null
var spot001: ImageButton? =null
var spot2: ImageButton? =null
var spot02: ImageButton? =null
var spot002: ImageButton? =null
var spot3: ImageButton? =null
var spot03: ImageButton? =null
var spot003: ImageButton? =null

override fun onCreate(savedInstanceState: Bundle?)

{

    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_tic_tac_toe)
    spot1 = findViewById<ImageButton>(R.id.imageButton1)
    spot01 = findViewById<ImageButton>(R.id.imageButton01)
    spot001 = findViewById<ImageButton>(R.id.imageButton001)
    spot2 = findViewById<ImageButton>(R.id.imageButton2)
    spot02 = findViewById<ImageButton>(R.id.imageButton02)
    spot002 = findViewById<ImageButton>(R.id.imageButton002)
    spot3 = findViewById<ImageButton>(R.id.imageButton3)
    spot03 = findViewById<ImageButton>(R.id.imageButton03)

    spot003 = findViewById<ImageButton>(R.id.imageButton003)

}

val photos: IntArray= intArrayOf(R.drawable.x, R.drawable.o, R.drawable.blank)

var turn = 1
var winState = false

fun change(view: View){
    var spot = findViewById<ImageButton>(view.getId()) //as Button

    if(turn == 1) {
        spot.setImageResource(photos[0])
        turn = turn * -1
    }

    else{
        spot.setImageResource(photos[1])
        turn = turn * -1
    }

    if(spot1?.getDrawable() == spot01?.getDrawable() &&
        spot1?.getDrawable() == spot001?.getDrawable() &&
        spot1?.getDrawable() != photos[2].toDrawable())
    {
        winState = true;
        Toast.makeText(getApplicationContext(),"Winner",Toast.LENGTH_SHORT).show();
    }
}

}

'''

1
I don't think you can compare Drawables like this, they will return different Drawable objects and thus the comparison will always be false. What I would do is make a class Spot with ImageButton and Drawable fields and use the same Drawable object to initialize Spots whose Drawable is the same. Then compare these fields; as they were initialized with same object the comparison should return true.Cole Tustin

1 Answers

0
votes

Drawable is an Object, and as such needs to be compared using the equals() method. Anyway, I am not sure that it will work either, because I am not sure of how a Drawable implements that method, so it might not be suitable for your case.

Also, you are using your view to check the state of your business logic, and that's not ideal at all. The best solution would be to have a Map or a matrix (let's say, an array with a dimension of [3][3]), where you store the actual value of the single spots, and then you reconcile the drawables with the values in the matrix.

I know that this would affect everything in your code and you have to heavely change it, so a quickier solution could be to use android:tag on your ImageButtons. You can specify any Object on it, so you could also use and Integer and compare it, always using the equals() method. This means you can rely on the tag for your logic.