【发布时间】:2022-01-02 17:56:30
【问题描述】:
我在下面的代码中遇到了问题。
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")
}
}
我可以检查输入是否小于或大于 3(坐标),也可以检查字段是否被占用。但是如何检查输入不是带有while循环的字符串,例如检查字段占用和坐标。提前致谢!
【问题讨论】:
-
表是 char[][] - 对吧?
char* pC = &Table[0][0]; for(int i = 0; i<9; i++){if(*pC = /*check for the ascii code - here you can define a range*/)throw new std::exception();}else{pC++;}之类的东西,对不起,我有点脱离原生 C 代码,但应该看起来像这样。你可以做不同的事情,但请 - 如果你在矩阵中思考,请使用 foreach-loops - 不要做。 -
对不起,我没有提到,代码在 Kotlin 中。我理解你的一些代码,但它是用 C 语言编写的。我看到你的 for 循环遍历表元素。但是我需要检查输入的是字符串还是数字,如果不是数字,则需要要求输入有效的数字然后用X或O更新表格。这部分代码现在不写了。跨度>
-
感觉这样真的可以使用一些技巧来编写更好的代码,你可以尝试将这个(实现下面的答案)发布到 codereview.stackexchange.com,因为在这里指出有点太多了在 cmets 中。但至少我建议用枚举 { X,0,EMPTY } 为字段提供 3 个状态,而不是使用原始字符串,除非必须,否则不要使用 mutableLists,所以不要使用 split()
-
好吧,我犯了一个错误,但人类可以犯错误。我的英语并不完美,因为它不是我的母语。但老实说,您可以看到 C 和 Kotlin 之间的区别!我很感激你的建议!但我需要以这种方式解决问题。如果知道使用数字,我认为没有人会愚蠢地使用字符串输入。但不是我写这个任务。我必须以书面方式解决它。检查输入是否不是字符串,检查是否超出范围,检查字段是否被占用。
标签: string function kotlin mutablelist