我在Android Studio中使用tic Tac Toe之类的Kotlin构建了一个应用程序。我有2位玩家,我想在线进行游戏。如何使用Firebase做到这一点?
如果使用Firebase数据库,如何在应用程序中选择2个随机用户进行游戏。
这是我的代码,用于解释2位玩家如何玩游戏:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_activiti_2player_5x5)
playingplayer = PLAYINGPLAYER.FIRST_PLAYER
}
fun ButtonClickes(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
}
startService(Intent(this@activiti_2player_5x5, SoundButtonService::class.java))
action(optionNumber, btnselected)
}
fun action(optionNumber: Int, btnselected: Button) {
if (playingplayer == PLAYINGPLAYER.FIRST_PLAYER) {
btnselected.setBackgroundResource(R.drawable.button_game_green)
buttonClicked.add(optionNumber)
playerOneLastClicked = true
playingplayer = PLAYINGPLAYER.SECOND_PLAYER
} else if (playingplayer == PLAYINGPLAYER.SECOND_PLAYER) {
btnselected.setBackgroundResource(R.drawable.button_game_red)
buttonClicked.add(optionNumber)
playerOneLastClicked = false
playingplayer = PLAYINGPLAYER.FIRST_PLAYER
}
imagebtnState()
}