Skip to content

Commit

Permalink
Pause game when backgrounded
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Jul 21, 2024
1 parent 463bfa6 commit b67b18f
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions Chess/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import UIKit

class ViewController: UIViewController {
private var game: Game = .init()
private var isPaused: Bool = false

@IBOutlet var boardView: BoardView?
@IBOutlet var undoButton: UIButton?
Expand All @@ -34,6 +35,7 @@ class ViewController: UIViewController {
whiteToggle?.selectedSegmentIndex = game.whiteIsHuman ? 0 : 1
blackToggle?.selectedSegmentIndex = game.blackIsHuman ? 0 : 1
boardView?.board = game.board
isPaused = game.inProgress
updateUI()
update()

Expand All @@ -47,11 +49,16 @@ class ViewController: UIViewController {

@objc private func didEnterBackground() {
try? save(to: saveURL)
if game.inProgress {
pause()
}
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
try? save(to: saveURL)
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if isPaused {
pause()
}
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
Expand Down Expand Up @@ -180,6 +187,24 @@ private extension ViewController {
}
}

func pause() {
isPaused = true
let alert = UIAlertController(
title: "Game in Progress",
message: "\(game.turn.rawValue.capitalized)'s turn",
preferredStyle: .alert
)
alert.addAction(UIAlertAction(title: "Resume", style: .default) { _ in
self.isPaused = false
self.update()
})
alert.addAction(UIAlertAction(title: "Restart", style: .default) { _ in
self.isPaused = false
self.resetGame()
})
present(alert, animated: true)
}

func setSelection(_ position: Position?) {
let moves = position.map(game.movesForPiece(at:)) ?? []
UIView.animate(withDuration: 0.2, animations: {
Expand All @@ -189,7 +214,7 @@ private extension ViewController {
}

func makeComputerMove() {
if !game.playerIsHuman(), let move = game.nextMove(for: game.turn) {
if !isPaused, !game.playerIsHuman(), let move = game.nextMove(for: game.turn) {
makeMove(move)
} else {
updateUI()
Expand Down

0 comments on commit b67b18f

Please sign in to comment.