有人告诉我我的代码包含很多强制展开。如果我确定操作的值不会为零,我认为可以这样做:
private var x: Int?
private var y: Int?
@IBAction func startButtonPressed(_ sender: UIButton) {
guard let numberOfRooms = selectedRooms.text, !numberOfRooms.isEmpty else {
return selectedRooms.placeholder = "type it, dude"
}
let rooms = Int(numberOfRooms)
x = Int(ceil(sqrt(Double(rooms!))))
y = x //grab some values from user input
maze = MazeGenerator(x!, y!) //generate a maze
hp = 2 * (x! * y!) //get hp value depending on user input
currentX = getRandomX(x!) //get random value in 0...x
currentY = getRandomY(y!)
currentCell = maze?.maze[currentX!][currentY!] //game starts in a random part of the maze
refreshButtons() //refresh UI
maze!.display() //print maze scheme in debug console
}
看起来不错吗?如果没有,应该怎么做?