I was told my code contains a lot of force unwrapping. I thought it's okay to do that if I am sure that the value operated won't be nil:
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
}
Does it look fine? If not, what should be done?