我有一个包含2个玩家的应用游戏,我想使用自动播放的设备,我在下面放置了此代码,但是当我玩该设备时该设备不会自动播放,必须在我自动播放后才能玩,但是当我单击随机按钮,然后他单击随机按钮。
我希望设备在轮到他时自动播放
fun buttonClickes1player5x5(v: View) {
val btnselected = v as Button
var optionNumber = 0
when (btnselected.id) {
R.id.btn1 -> optionNumber = 1
R.id.btn2 -> optionNumber = 2
R.id.btn3 -> optionNumber = 3
R.id.btn4 -> optionNumber = 4
R.id.btn5 -> optionNumber = 5
R.id.btn6 -> optionNumber = 6
R.id.btn7 -> optionNumber = 7
R.id.btn8 -> optionNumber = 8
R.id.btn9 -> optionNumber = 9
R.id.btn10 -> optionNumber = 10
}
action(optionNumber, btnselected)
}
fun action(optionNumber: Int, _btnselected: Button) {
var btnselected = _btnselected
if (playingplayer == PLAYINGPLAYER_1PLAYER_5X5.FIRST_PLAYER) {
btnselected.setBackgroundResource(R.drawable.button_game_green)
buttonClickes1Player.add(optionNumber)
btnselected.isEnabled = false
playingplayer = PLAYINGPLAYER.SECOND_PLAYER
} else if (playingplayer == PLAYINGPLAYER.SECOND_PLAYER) {
var noSelectedButton: ArrayList<Int> = ArrayList()
for (buttonNumber in 1..40) {
if (!(buttonClickes1Player.contains(buttonNumber))) {
noSelectedButton.add(buttonNumber)
}
}
val r = Random()
val randomIndex = r.nextInt(noSelectedButton.size + 1)
var buttonNumber = noSelectedButton[randomIndex]
when (buttonNumber) {
1 -> btnselected = btn1
2 -> btnselected = btn2
3 -> btnselected = btn3
4 -> btnselected = btn4
5 -> btnselected = btn5
6 -> btnselected = btn6
7 -> btnselected = btn7
8 -> btnselected = btn8
9 -> btnselected = btn9
10 -> btnselected = btn10
}
btnselected.setBackgroundResource(R.drawable.button_game_red)
buttonClickes1Player.add(buttonNumber)
btnselected.isEnabled = false
playingplayer = PLAYINGPLAYER.FIRST_PLAYER
}
imagebtnState()
}