Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare a arraylist with nested arraylist in kotlin?

I am new to kotlin . I am trying to create simple tic tac toe game app.

In many tutorial i have seen most of them using if statement to find the winners.So i tried to modify with predefiend values and check them with players input.


\\ this funtion checks the winner , where player1 and player2 are arraylist.
\\ example : player1 = [1,2,8] and player2 = [4,5,6]

fun checkWinner(player1,player2){

   var possibleCombos = arrayListOf(
            arrayListOf(1,2,3), arrayListOf(4,5,6), arrayListOf(7,8,9),
            arrayListOf(1,4,7), arrayListOf(2,5,8), arrayListOf(3,6,9),
            arrayListOf(7,5,3), arrayListOf(1,5,9))

   for(items in possibleCombos) {

            if(a.containsAll(items)) {

               Toast.makeText(this,"Winner is A $a",Toast.LENGTH_LONG).show()

               }
            else if (b.containsAll(items)) {

               Toast.makeText(this,"Winner is B $b",Toast.LENGTH_LONG).show()

              }
            else {

                Toast.makeText(this,"Game is tie",Toast.LENGTH_SHORT).show()

            }
        }

    }

It is not working and the else part is executing always. I want to toast the result.

Any solution?

like image 250
randomguy123 Avatar asked Aug 08 '26 18:08

randomguy123


1 Answers

Well you're doing it the wrong way, you are checking that each element in that list is actually equal to the either player1 or player2.

So if you execute, [4,5,6] then you'll get 8 toasts in which 1 of them will be winner as checked against [4,5,6] while the other 7 will point to the else condition because [4,5,6] is not [1,2,3], [7,8,9], etc.

val possibleCombos = sequenceOf( // if want listOf, just add .asSequence() before map below, this way will break the mapping if result is obtained.
    listOf(1, 2, 3), listOf(4, 5, 6), listOf(7, 8, 9),
    listOf(1, 4, 7), listOf(2, 5, 8), listOf(3, 6, 9),
    listOf(7, 5, 3), listOf(1, 5, 9))

fun checkWinner(player1: List<Int>, player2: List<Int>) {
    // this will break the mapping once first non-null value is found, in terminal operation firstOrNull()
    val message = possibleCombos.map {
        when {
            player1.containsAll(it) -> "Winner is A $player1"
            player2.containsAll(it) -> "Winner is B $player2"
            else -> null
        }
    }.filterNotNull().firstOrNull() ?: "Game is tie"
    Toast.makeText(this, message, Toast.LENGTH_LONG).show()
}

// Call this function (test sample)
checkWinner(arrayListOf(1, 2, 8, 3), arrayListOf(5, 4, 6, 7)) // may change to listOf as well

Result:

Winner is B [5, 4, 6, 7]

PS: I refactored arrayListOf() to listOf(), because that is what Kotlin-stdlib recommends to use. However you can switch back to the arrayListOf() as well

like image 142
Animesh Sahu Avatar answered Aug 11 '26 08:08

Animesh Sahu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!