Can't fill 2d-array by brackets in Kotlin despite getter and setter

Viewed 176

It should become a little consolewalker program but I can't fill the 2d-array I created in class Field in class Player by brackets because there "are to many arguments" for set function despite there is one that should fit. Also I have learned that it isn't even necessary in Kotlin. So I am a little bit confused and get no further.

fun main() {
val gameField = Field()

gameField.buildField()

val player = Player(gameField.field)

gameField.printField()

var input: String? = " "

while (input != "x") {

    println("w: ↑ a: ← s: ↓ d: →")
    input = readLine()
    when (input) {
        "w" -> player.counter = 0
        "a" -> player.counter = 1
        "s" -> player.counter = 2
        "d" -> player.counter = 3
        "x" -> return println("Spiel wird beendet")
        " " -> player.move()
        else -> println("Ungültige Eingabe")
    }
    player.setPlayer()
    gameField.printField()
}

}

class Field() {
private var field = Array(10) {Array(10){" "}}

operator fun set(row: Int, col: Int, value: String) {
    field[row][col] = value
}
operator fun get(row: Int, col: Int) = field[row][col]

fun buildField() {
    for (i in field.indices) {
        field[i][0] = "# "
        field[i][field.size - 1] = "# "
        for (j in 1 until field.size - 1) {
            field[i][j] = "  "
            field[0][j] = "# "
            field[field.size - 1][j] = "# "
        }
    }
}

fun printField() {
    for (i in field.indices) {
        for (j in field[i].indices) {
            print(field[i][j])
        }
        println()
    }
}

}

class Player(private var gameField: Array<Array<String>>) {
private val playerSign = arrayOf<String>("↑ ", "← ", "↓ ", "→ ")
var currentRow = 4
var currentColumn = 4
var counter = 0

init {
    setPlayer()
}

fun setPlayer() {
    gameField[currentRow, currentColumn] = playerSign[counter]
}

fun reset() {
    gameField[currentRow, currentColumn] = "  "
}

fun move() {
    reset()
    when(counter) {
        0 -> if (currentRow > 1) currentRow--
        1 -> if (currentColumn > 1) currentColumn--
        2 -> if (currentRow < gameField.size-2) currentRow++
        3 -> if (currentColumn < gameField.size-2) currentColumn++
    }
    setPlayer()
}

}

Thanks in advance!

2 Answers

Problem with your code is that in class Player field is of type Field, its not an array, that is why compiler is giving you the error. if you want to access the field array of Field class then you should do

field.field[3][3] = playerSign[counter]

You could extend you Field class with an overloaded get and set operator (see Operator Overloading | Indexed Access Operator in the language documentation).

class Field() {
  // all your existing code

  operator fun set(row : Int, col : Int,  value : String) {
    field[row][col] = value
  }

  operator fun get(row : Int, col : Int) = field[row][col]
}

As the documentation states:

Square brackets are translated to calls to get and set with appropriate numbers of arguments.

Thus, you now can write

field[currentRow, currentColumn] = playerSign[counter]

in your Player class which is more clean than exposing / accessing the array itself (which could and should now be made private).

Related