Skip to content

Instantly share code, notes, and snippets.

@fisherds
Last active March 8, 2018 16:57
Show Gist options
  • Select an option

  • Save fisherds/f646bcd4cbf83b4353d5 to your computer and use it in GitHub Desktop.

Select an option

Save fisherds/f646bcd4cbf83b4353d5 to your computer and use it in GitHub Desktop.
Code used in the ScoreKeeper Lab
import UIKit
class ViewController: UIViewController {
// Interface Builder Connections redacted
var roundNumber = 1
var playerScores = [0, 0, 0, 0]
var scoreTextFields = [UITextField]()
var scoresListTextViews = [UITextView]()
override func viewDidLoad() {
super.viewDidLoad()
scoreTextFields = [player1ScoreTextField, player2ScoreTextField, player3ScoreTextField, player4ScoreTextField]
scoresListTextViews = [player1ScoresTextView, player2ScoresTextView, player3ScoresTextView, player4ScoresTextView]
resetGame()
}
func resetGame() {
roundNumber = 1
roundLabel.text = "Round \(roundNumber)"
for i in 0..<4 {
playerScores[i] = 0
scoreTextFields[i].text = ""
scoresListTextViews[i].text = ""
}
}
func enterScores() {
for i in 0..<4 {
if let score = Int(scoreTextFields[i].text!) {
playerScores[i] += score
}
scoresListTextViews[i].text = scoresListTextViews[i].text == "" ? "\(playerScores[i])" : "\(scoresListTextViews[i].text!)\n\(playerScores[i])"
scoresListTextViews[i].textAlignment = .center
scoreTextFields[i].text = ""
}
roundNumber += 1
roundLabel.text = "Round \(roundNumber)"
self.view.endEditing(true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment