I'm making a battleship game on Xcode 11 using Storyboards, and I've pretty much done everything except a Game Over screen and stop registering the guesses once the counter reaches 0. This is also my first time making a game on my own. Here's one of the IBActions I made for the buttons:
@IBAction func square1Button(_ sender: Any) {
if randomNum == 1 {
square1.image = UIImage(named: "ShipPatrolHull")
} else {
guesses -= 1
Turns.text = String(guesses)
if guesses == 0 {
}
}
}
The if statement in the else statement is empty because I don't know what to add there to make the variable guesses stop going down and to make a Game Over screen to show. I've also added a Storyboard called GameOver and is linked back to the main storyboard.
You could display a Game Over UIAlertController and then have the game reset when they select "Play Again." However, you mention that you already have another Storyboard for a separate Game Over screen. All you need to do is present that Game Over view controller. Make sure to set the storyboard identifier for the view controller you are trying to present so you can access it in code. See below for guidance,
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = self.storyboard.instantiateViewController(withIdentifier: "GameOverView")
From here you can easily present it.
self.present(controller, animated: true, completion: nil)
Lastly, to make the guesses stop going down, reorganize your if-statements in order to only subtract one from the guesses if the guesses value is not equal to zero, otherwise... game over.
if guesses != 0 {
Turns.text = String(guesses)
guesses -= 1
} else {
//Game over, reset, etc.
}