whitewind664/sdp

View on GitHub
app/src/main/java/com/github/gogetters/letsgo/tutorial/TutorialLocalPlayer.kt

Summary

Maintainability
A
0 mins
Test Coverage
B
82%
package com.github.gogetters.letsgo.tutorial

import android.util.Log
import com.github.gogetters.letsgo.game.BoardState
import com.github.gogetters.letsgo.game.DelegatedPlayer
import com.github.gogetters.letsgo.game.Move
import com.github.gogetters.letsgo.game.Stone
import com.github.gogetters.letsgo.game.util.InputDelegate

internal class TutorialLocalPlayer(inputDelegate: InputDelegate): DelegatedPlayer(Stone.BLACK, inputDelegate) {

    /**
     *  Contains the recommended moves per turn of the local player
     */
    private var recommendedMoves: List<List<Move>> = emptyList()
    private var turnCount = 0

    override fun requestMove(board: BoardState): Move {
        var move: Move
        do {
            move = super.requestMove(board)
            val isGoodChoice = isGoodChoice(move)
            if (!isGoodChoice)
                notifyBadMove("This is not the best move... Think again!")
        } while(!isGoodChoice)
        turnCount++
        return move
    }

    fun setRecommendedMoves(recommendedMoves: List<List<Move>>) {
        this.recommendedMoves = recommendedMoves
        turnCount = 0
    }

    private fun isGoodChoice(move: Move): Boolean {
        val recommendedMovesInTurn = if (turnCount < recommendedMoves.size) recommendedMoves[turnCount] else emptyList()
        return recommendedMovesInTurn.isEmpty() || move in recommendedMovesInTurn
    }

    /**
     * Notifies about a move that is not illegal, but just not a good choice in this situation.
     */
    private fun notifyBadMove(comment: String) {
        Log.i("TutorialLocalPlayer", "This is not a good choice")
    }
}