首页 > 解决方案 > Kotlin 中不调用带参数的函数 - Android

问题描述

    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()
    }
}

嗨,我为 tictactoe 游戏编写了这个示例代码,除了函数 checkWinner 之外的所有工作,该函数接受两个参数 ArrayList 和 currentPlayer 来检查获胜者。在每个玩家转身后调用此函数,以检查 tictactoe 游戏中获胜的点击单元格。我不知道错误在哪里!先感谢您

标签: androidkotlintic-tac-toe

解决方案


我想你忘了检查倾斜
添加

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

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

根据您的 if 条件
,并确保按钮 id 以正确的顺序写入


推荐阅读