Untitled

 avatar
unknown
kotlin
4 years ago
2.6 kB
6
Indexable
fun startNewGame(whitePlayer: String,
        blackPlayer: String,
        pieces: Array<Pair<String, String>?>,
        totalPiecesAndTurn: Array<Int?>,
        numColumns: Int,
        numLines: Int,
        showLegend: Boolean = false,
        showPieces: Boolean = false) {

    var winnerExists = false
    do {
        var isCorrectInput = true
        do {
            val turnPlayer = if (totalPiecesAndTurn[2] == 0) whitePlayer else blackPlayer

            println("${buildBoard(numColumns, numLines, showLegend, showPieces, pieces)}\n\n" +
                    "$turnPlayer, choose a piece (e.g 2D).\n" +
                    "Menu-> m;\n")
            var input = readLine()
            if (isMainMenuChoice(input!!)) {
                return
            }
            var origin: Pair<Int, Int>? = null
            var originPiece: Pair<String, String>? = null
            if (isCorrectInput && isValidCoordinates(input)) {
                origin = getCoordinates(input)
                originPiece = pieces[(origin!!.second - 1) * numLines + (origin.first - 1)]
            } else {
                isCorrectInput = false
            }
            if (originPiece == null ||
                    !isCoordinateInsideChess(origin!!, numColumns, numLines)) {
                isCorrectInput = false
            }
            if (isCorrectInput &&
                    checkRightPieceSelected(originPiece!!.second, totalPiecesAndTurn[2]!!)) {
                println("$turnPlayer, choose a target piece (e.g 2D).\n" +
                        "Menu-> m;\n")
                input = readLine()
            } else {
                isCorrectInput = false
            }
            var target: Pair<Int, Int>? = null
            if (isCorrectInput && isValidCoordinates(input!!)) {
                target = getCoordinates(input)
            } else {
                isCorrectInput = false
            }
            if (isCorrectInput &&
                    isValidTargetPiece(originPiece!!, origin!!,
                            target!!, pieces, numColumns, numLines)) {
                if (checkPieceMovement(origin, target, originPiece, pieces, numColumns, numLines)) {
                    println("Valid move")
                } else {
                    isCorrectInput = false
                }
            } else {
                isCorrectInput = false
            }
            if (!isCorrectInput) {
                println(invalid())
            }
        } while (isCorrectInput)
    } while (!winnerExists)
}
Editor is loading...