为什么井字游戏的第一行在运行时(android studio、kotlin)运行异常?

why first row in tic-tac-toe game works strange in runTime (android studio, kotlin)?

我已经使用 android studio, kotlin 编写了井字游戏的代码。 根据一些 YouTube 视频,逻辑没问题,但在 运行 应用程序之后,当我点击第一行的每个按钮时,它会显示玩家获胜!但对于其他行,逻辑会正常工作。

代码如下:

变量和 onCreate:

    private var buttonArray = arrayOf<ArrayList<Button>>(arrayListOf(), arrayListOf(), arrayListOf())
    private var player1Turn = true
    private var roundCount=0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding2 = ActivityMain2Binding.inflate(layoutInflater)
        val view2 = binding2.root
        setContentView(view2)
        for (i in 0 until 3) {
            for (j in 0 until 3) {
                val buttonId = "button_$i$j"
                val resId = resources.getIdentifier(buttonId, "id", packageName)
                buttonArray[i].add(findViewById(resId))
                buttonArray[i][j].setOnClickListener{eachButtonOnClick(buttonArray[i][j])}
            }
        }
        binding2.buttonReset.setOnClickListener {
            for (i in 0 until 3) {
                for (j in 0 until 3) {
                    buttonArray[i][j].text = ""
                }
            }
            player1Turn = true
            binding2.textViewResult.text="result"
            roundCount=0
        }
    }

点击方法:

    private fun eachButtonOnClick(view: Button) {
        if (view.text.isBlank() && binding2.textViewResult.text=="result") {
            if (player1Turn) {
                view.text = "X"
            } else {
                view.text = "O"
            }
            roundCount++
            if(checkWinner()){
                if (player1Turn) {
                    binding2.textViewResult.text = "player X winner"
                    Toast.makeText(this,"player X winner",Toast.LENGTH_SHORT).show()
                }
                else{
                    binding2.textViewResult.text = "player O winner"
                    Toast.makeText(this,"player O winner",Toast.LENGTH_SHORT).show()
                }
            } else if(roundCount==9) {
                binding2.textViewResult.text = "Draw"
                Toast.makeText(this,"Draw",Toast.LENGTH_SHORT).show()
            }else{
                player1Turn=!player1Turn
            }
        }
    }

中奖方式:

    private fun checkWinner():Boolean{
        for (column in 0 until 3){
            if (buttonArray[column][0].text!=""
                &&buttonArray[column][0].text==buttonArray[column][1].text
                &&buttonArray[column][0].text==buttonArray[column][2].text){
                return true
            }
        }
        for (row in 0 until 3){
            if (buttonArray[0][row].text!=""
                &&buttonArray[0][row].text==buttonArray[0][row].text
                &&buttonArray[0][row].text==buttonArray[0][row].text){
                return true
            }
        }
        if (buttonArray[0][0].text!=""
            &&buttonArray[0][0].text==buttonArray[1][1].text
            &&buttonArray[0][0]==buttonArray[2][2]){
            return true
        }
        if (buttonArray[0][2].text!=""
            &&buttonArray[0][2].text==buttonArray[1][1].text
            &&buttonArray[0][2].text==buttonArray[2][0].text){
            return true
        }
        return false
    }

因为这个

            if (buttonArray[0][row].text!=""
                &&buttonArray[0][row].text==buttonArray[0][row].text
                &&buttonArray[0][row].text==buttonArray[0][row].text){
                return true
            }

您将同一个单元格与其自身进行比较。

你需要

buttonArray[0][row].text!=""
&&buttonArray[0][row].text==buttonArray[1][row].text
&&buttonArray[0][row].text==buttonArray[2][row].text