
commonMain.ro.sevens.ai.brain.SevensAiBrain.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ai-jvm Show documentation
Show all versions of ai-jvm Show documentation
AI classes used in sevens
package ro.dragossusi.sevens.ai.brain
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import ro.dragossusi.sevens.ai.SevensAiPlayer
import ro.dragossusi.sevens.game.listener.PlayerListener
import ro.dragossusi.sevens.game.room.OnRoundsChangedListener
import ro.dragossusi.sevens.game.room.Room
import ro.dragossusi.logger.TagLogger
import ro.dragossusi.sevens.payload.game.GameEndResponse
import ro.dragossusi.sevens.payload.game.NewRoundResponse
import ro.dragossusi.sevens.payload.game.PlayerTurnResponse
import ro.dragossusi.sevens.payload.game.SimplePlayerResponse
/**
* sevens-game
*
* Copyright (C) 2020 Rachieru Dragos-Mihai
*
* sevens-game is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* sevens-game is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sevens-game. If not, see [License](http://www.gnu.org/licenses/) .
*
*/
open class SevensAiBrain(
private val player: SevensAiPlayer,
private val room: Room,
private val tagLogger: TagLogger?,
private val operationDelay: Long,
dispather: CoroutineDispatcher
) : RoundedAiBrain(dispather), OnRoundsChangedListener, CoroutineScope {
override suspend fun onRoomStopped() {
tagLogger?.w("TODO Not yet implemented")
}
override fun onGameEnded(response: GameEndResponse) {
}
override fun onGameStarted(players: Array) {
}
override fun onRoundStarted(response: NewRoundResponse) {
launch {
delay(operationDelay)
if (response.currentPlayerId == player.id) {
room.addCard(player.session, player.pickCard())
}
}
}
override fun onPlayerTurn(playerTurn: PlayerTurnResponse) {
launch {
delay(operationDelay)
if (playerTurn.currentPlayerId == player.id) {
val canEnd = playerTurn.canEnd(room.type)
val card = player.pickCard(playerTurn.roundCards, canEnd = canEnd)
room.addCard(
player.session,
card ?: player.hand!!.chooseRandomCard()
)
}
}
}
override fun onRoomConnected(id: Long) {
// tagLogger?.w("TODO onRoomConnected")
}
override fun onRoundEnded(response: NewRoundResponse) {
// tagLogger?.w("TODO onRoundEnded")
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy