samedi 27 octobre 2018

Checking empty string in kotlin doesn't work

i am begginer into kotlin development and i am doing a simple TicTacToe game with kotlin and i want to check if the first three button texts are equal and at the same time one of them is not empty but when i use !button1.text.toString().isEmpty it doesn't work. don't know why. help me. here are my kotlin and xml codes

package com.example.tictac

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_tic_tac.*

class TicTacActivity : AppCompatActivity() {
var isFirstPlayer = true;

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_tic_tac)
    init()
}


fun init() {

    buttonClick(button1)
    buttonClick(button2)
    buttonClick(button3)
    buttonClick(button4)
    buttonClick(button5)
    buttonClick(button6)
    buttonClick(button7)
    buttonClick(button8)
    buttonClick(button9)

}

fun buttonClick(button: Button) {
    button.setOnClickListener() {
        if (isFirstPlayer) {
            button.text = "X"
            isFirstPlayer = false
        } else {
            button.text = "O"
            isFirstPlayer = true
        }
        button.isClickable = false
    }

    if (!button1.text.toString().isEmpty() && 
button1.text.toString().equals(button2.text.toString()) && 
button2.text.toString().equals(button3.text.toString())) {
        winner(button1)
    }
}

fun winner(button: Button) {
    Toast.makeText(this, button.text.toString(), Toast.LENGTH_LONG).show()
}
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".TicTacActivity"
        android:orientation="vertical"
>
    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_weight="1">

        <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textSize="49sp"
                />

        <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textSize="49sp"
        />

        <Button
                android:id="@+id/button3"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textSize="49sp"/>

    </LinearLayout>

    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_weight="1">

        <Button android:id="@+id/button4"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textSize="49sp"/>

        <Button android:id="@+id/button5"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textSize="49sp"/>

        <Button android:id="@+id/button6"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textSize="49sp"/>

    </LinearLayout>

    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_weight="1">

        <Button android:id="@+id/button7"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textSize="49sp"/>

        <Button android:id="@+id/button8"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textSize="49sp"/>

        <Button android:id="@+id/button9"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textSize="49sp"/>

    </LinearLayout>


</LinearLayout>

Aucun commentaire:

Enregistrer un commentaire