gpt4 book ai didi

android - 如何使用 Kotlin 修复 Null 不能成为 Android 迷宫游戏中非空类型的值

转载 作者:行者123 更新时间:2023-11-29 23:06:12 28 4
gpt4 key购买 nike

我正在按照本教程在 android 中创建一个迷宫,但是当我在 getNeighbour 方法中返回 null 时出现错误

错误:Null 不能是非空类型 Cell 的值

对我做错的任何帮助表示感谢。

我试过 Cell?但无法让它工作,它给了我更多的错误

这是我的代码:

class GameView: View{
//revisar por que hace cosas raras con los limites de cols y rows
private val COLS:Int = 7
private val ROWS: Int = 12
private var cellSize: Float = 0F
private var hMargin: Float = 0F
private var vMargin: Float = 0F
private val wallPaint = Paint()
private val random:Random = Random()

constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs){
wallPaint.color = Color.WHITE
wallPaint.strokeWidth = 1F
createMaze()

}
fun getNeighbour(cell:Cell): Cell {
var vecinos: ArrayList<Cell> = ArrayList()

//vecino izquierda
if(cell.col > 0) {
if (!cells[cell.col - 1][cell.row].visited) {
vecinos.add(cells[cell.col - 1][cell.row])
}
}
//vecino derecha
if(cell.col < COLS - 1) {
if (!cells[cell.col + 1][cell.row].visited) {
vecinos.add(cells[cell.col + 1][cell.row])
}
}
//vecino arriba
if(cell.row > 0) {
if (!cells[cell.col][cell.row - 1].visited) {
vecinos.add(cells[cell.col ][cell.row - 1])
}
}
//vecino abajo
if(cell.row < ROWS - 1) {
if (!cells[cell.col][cell.row + 1].visited) {
vecinos.add(cells[cell.col][cell.row + 1])
}
}
if (vecinos.size > 0) {
var index = random.nextInt(vecinos.size)
return vecinos[index]
}else {

// i get this error: Null can not be a value of a non-null type
// Cell
return null
}
}

fun removeWall(current:Cell,next:Cell){
if (current.col == next.col && current.row == next.row +1){
current.topWall = false
next.bottomWall = false
}
if (current.col == next.col && current.row == next.row -1){
current.bottomWall = false
next.topWall = false
}
if (current.col == next.col + 1 && current.row == next.row){
current.leftWall = false
next.rightWall = false
}
if (current.col == next.col - 1 && current.row == next.row +1){
current.rightWall = false
next.leftWall = false
}
}

var cells: Array<Array<Cell>> = Array(COLS, {Array(ROWS, {Cell()})})

fun createMaze(){
var stack = Stack<Cell>()
var current:Cell
var next:Cell

for(x in 0 until COLS){
for(y in 0 until ROWS){
cells[x][y] = Cell(x,y)
}
}
current = cells[0][0]
current.visited = true

do{
next = getNeighbour(current)
removeWall(current,next)
stack.push(current)
current = next
current.visited = true

}while (!stack.empty())

}
override fun onDraw(canvas: Canvas) {
canvas.drawColor(Color.DKGRAY)
val ancho = width
val alto = height
println(ancho)
println(alto)
if(ancho/alto < COLS/ROWS){
cellSize = ancho/(COLS+1).toFloat()
}else{
cellSize = alto/(ROWS+1).toFloat()

}
hMargin = (ancho - COLS*cellSize)/2
vMargin = ((alto - ROWS*cellSize)/2)

canvas.translate(hMargin,vMargin)

for(x in 0 until COLS){
for(y in 0 until ROWS){
if (cells[x][y].topWall){
canvas.drawLine(
x*cellSize,
y*cellSize,
(x+1)*cellSize,
y*cellSize,
wallPaint)
}

if (cells[x][y].leftWall){
canvas.drawLine(
x*cellSize,
y*cellSize,
x*cellSize,
(y+1)*cellSize,
wallPaint)
}

if (cells[x][y].bottomWall){
canvas.drawLine(
x*cellSize,
(y+1)*cellSize,
(x+1)*cellSize,
(y+1)*cellSize,
wallPaint)
}

if (cells[x][y].rightWall){
canvas.drawLine(
(x+1)*cellSize,
y*cellSize,
(x+1)*cellSize,
(y+1)*cellSize,
wallPaint)
}
}
}

}

}

class Cell(var col:Int = 0, var row: Int = 0 ){
var topWall = true
var leftWall = true
var bottomWall = true
var rightWall = true
var visited = false

}

最佳答案

从语法上看

// first change
fun getNeighbour(cell:Cell): Cell -> fun getNeighbour(cell:Cell): Cell?

// second change
var next:Cell -> var next:Cell? = null

// third change
next = getNeighbour(current)
if(next != null) {
// you process code goes here
}

但是从设计的角度来看,fun getNeighbour(cell:Cell): Cell 意味着总有一个邻居可以被找到。

如果不是这种情况,您应该将其更改为 Cell? 并且以下代码应该处理 null 情况。

关于android - 如何使用 Kotlin 修复 Null 不能成为 Android 迷宫游戏中非空类型的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56487595/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com