I run into a problem in the code below.
fun main() {
val table = mutableListOf(
mutableListOf(' ', ' ', ' '),
mutableListOf(' ', ' ', ' '),
mutableListOf(' ', ' ', ' ')
)
println("---------")
println("| " + table[0][0] + " " + table[0][1] + " " + table[0][2] + " |")
println("| " + table[1][0] + " " + table[1][1] + " " + table[1][2] + " |")
println("| " + table[2][0] + " " + table[2][1] + " " + table[2][2] + " |")
println("---------")
print("Enter the coordinates: ")
var coordinates = readLine()!!.split(" ").toMutableList()
var x = coordinates[0].toInt()
var y = coordinates[1].toInt()
while (x > 3 || x < 1 || y > 3 || y < 1) {
println("Coordinates should be from 1 to 3!")
print("Enter the coordinates: ")
coordinates = readLine()!!.split(" ").toMutableList()
x = coordinates[0].toInt()
y = coordinates[1].toInt()
}
while (table[x-1][y-1] == 'X' || table[x-1][y-1] == 'O') {
println("This cell is occupied! Choose another one!")
print("Enter the coordinates: ")
coordinates = readLine()!!.split(" ").toMutableList()
x = coordinates[0].toInt()
y = coordinates[1].toInt()
}
table[x-1][y-1] = 'X'
println("---------")
println("| " + table[0][0] + " " + table[0][1] + " " + table[0][2] + " |")
println("| " + table[1][0] + " " + table[1][1] + " " + table[1][2] + " |")
println("| " + table[2][0] + " " + table[2][1] + " " + table[2][2] + " |")
println("---------")
if (table[0][0] == 'X' && table[0][1] == 'X' && table[0][2] == 'X' ||
table[1][0] == 'X' && table[1][1] == 'X' && table[1][2] == 'X' ||
table[2][0] == 'X' && table[2][1] == 'X' && table[2][2] == 'X' ||
table[0][0] == 'X' && table[1][0] == 'X' && table[2][0] == 'X' ||
table[0][1] == 'X' && table[1][1] == 'X' && table[2][1] == 'X' ||
table[0][2] == 'X' && table[1][2] == 'X' && table[2][2] == 'X' ||
table[0][0] == 'X' && table[1][1] == 'X' && table[2][2] == 'X' ||
table[2][0] == 'X' && table[1][1] == 'X' && table[0][2] == 'X'
) {
println("X wins")
} else if (table[0][0] == 'O' && table[0][1] == 'O' && table[0][2] == 'O' ||
table[1][0] == 'O' && table[1][1] == 'O' && table[1][2] == 'O' ||
table[2][0] == 'O' && table[2][1] == 'O' && table[2][2] == 'O' ||
table[0][0] == 'O' && table[1][0] == 'O' && table[2][0] == 'O' ||
table[0][1] == 'O' && table[1][1] == 'O' && table[2][1] == 'O' ||
table[0][2] == 'O' && table[1][2] == 'O' && table[2][2] == 'O' ||
table[0][0] == 'O' && table[1][1] == 'O' && table[2][2] == 'O' ||
table[2][0] == 'O' && table[1][1] == 'O' && table[0][2] == 'O'
) {
println("O wins")
}
}
I can check the input is not smaller or higher then 3 (coordinates) and also able to check the field is not occupied. But how can I check the input is not a string with a while loop like checking field occupation and coordinate. Thanks in advance!