android - 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 游戏中获胜的点击单元格。我不知道错误在哪里!先感谢您
解决方案
我想你忘了检查倾斜
添加
playerArrayList.contains(7) && playerArrayList.contains(5) && playerArrayList.contains(3) ||
playerArrayList.contains(1) && playerArrayList.contains(5) && playerArrayList.contains(9)
根据您的 if 条件
,并确保按钮 id 以正确的顺序写入
推荐阅读
- python - 选择 Exploded Criteria Python Dataframe 的前 X 个
- django - 使用 docker 部署
- asp.net-mvc - 在指定 SMTP 详细信息时,在 mvc c# 中发送邮件不起作用
- python - Pandas groupby 在保留多个聚合的组内排序并用 facet 可视化它
- pycharm - 如何将 Tab 键映射到 WebStorm 中的 *select* 自动完成选项,而不是 *inserting* 它们
- visual-studio-code - 在 VScode markdown 中显示 Julia 代码输出
- agora.io - agora websdk 得到 obs 相机而不是我的笔记本电脑相机
- azure - 使用 Azure 的 Helm 导致 401 Unauthorized
- javascript - React Router - 身份验证后的重定向延迟
- java - CompletedStage.completedFuture VS CompletedStage.completedStage