【问题标题】:Function with parameters is not called in Kotlin - AndroidKotlin 中不调用带参数的函数 - Android
【发布时间】:2020-11-26 16:17:22
【问题描述】:
    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 游戏中获胜的点击单元格。 我不知道错误在哪里! 提前谢谢你

【问题讨论】:

    标签: android kotlin tic-tac-toe


    【解决方案1】:

    我想你忘了检查斜线
    添加

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

    到你的 if 条件
    并确保按钮 id 以正确的顺序书写

    【讨论】:

    • 非常感谢!问题在于我应该将它们声明为字符串而不是 Int 的 Id
    猜你喜欢
    • 2020-02-13
    • 2016-11-29
    • 1970-01-01
    • 2020-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-12
    • 2016-11-13
    相关资源
    最近更新 更多