Function with parameters is not called in Kotlin - Android

Viewed 88
    package com.begors.titactoe

import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

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

    var currentPlayer = 1

    //Defining two lists of ids that each player will select
    var playerOneClickedListId = ArrayList<Int>()
    var playerTwoClickedListId = ArrayList<Int>()

    fun buClick(view: View) {

        //getting id of the current clicked button
        var clickedButton = view as Button
        var clickedButtonId = clickedButton.id.toString().toInt()

        //Playing game
        if (currentPlayer == 1){
            playerOneClickedListId.add(clickedButtonId)
            clickedButton.setText("X")
            clickedButton.isEnabled=false
            checkWinner(playerOneClickedListId,currentPlayer) // All works perfectly except this line!!!!!!!
            currentPlayer=2
        }else{
            playerTwoClickedListId.add(clickedButtonId)
            clickedButton.setText("O")
            clickedButton.isEnabled=false
            checkWinner(playerTwoClickedListId,currentPlayer)// All works perfectly except this line!!!!!!!
            currentPlayer=1
    }
}
    //Function that checks for the winner based on their selection - DOES NOT WORK
    fun checkWinner(playerArrayList: ArrayList<Int>, currentPlayer : Int){
        if (playerArrayList.contains(1) && playerArrayList.contains(2) && playerArrayList.contains(3) ||
            playerArrayList.contains(4) && playerArrayList.contains(5) && playerArrayList.contains(6) ||
            playerArrayList.contains(7) && playerArrayList.contains(8) && playerArrayList.contains(9) ||
            playerArrayList.contains(1) && playerArrayList.contains(4) && playerArrayList.contains(7) ||
            playerArrayList.contains(2) && playerArrayList.contains(5) && playerArrayList.contains(8) ||
            playerArrayList.contains(3) && playerArrayList.contains(6) && playerArrayList.contains(9))
        {
            Toast.makeText(this, "Winner is player $currentPlayer", Toast.LENGTH_LONG).show()
        }
    }

    // Button event that will reset the game
    fun resetGame(view : View){
        bu1.isEnabled=true
        bu1.setText("")
        bu2.isEnabled=true
        bu2.setText("")
        bu3.isEnabled=true
        bu3.setText("")
        bu4.isEnabled=true
        bu4.setText("")
        bu5.isEnabled=true
        bu5.setText("")
        bu6.isEnabled=true
        bu6.setText("")
        bu7.isEnabled=true
        bu7.setText("")
        bu8.isEnabled=true
        bu8.setText("")
        bu9.isEnabled=true
        bu9.setText("")
        playerOneClickedListId.clear()
        playerTwoClickedListId.clear()
    }
}

Hi, I wrote this sample code for tictactoe game, all works except the function checkWinner that takes two parameters an ArrayList and currentPlayer to check for the winner. this function is called after each player turn to check for the winning clicked cells in tictactoe game. I do not know where the error is! Thank you in advance

1 Answers

I suppose you forgot to check the slants
add

playerArrayList.contains(7) && playerArrayList.contains(5) && playerArrayList.contains(3) ||

playerArrayList.contains(1) && playerArrayList.contains(5) && playerArrayList.contains(9)

to your if condition
and make sure that the buttons id written in the correct order

Related