<file_sep>//
//  MathModel.swift
//  TrueFalseStarter
//
//  Created by <NAME> on 6/28/16.
//  Copyright © 2016 Treehouse. All rights reserved.
//

import GameKit

struct MathModel {
    
    static func randomMathEquationDict() -> [String : String] {
        
        let maxOperand = 10
        var maxAnswer: Int
        var operator_: String
        var correctAnswer: Int
        
        let op1 = GKRandomSource.sharedRandom().nextIntWithUpperBound(maxOperand)
        let op2 = GKRandomSource.sharedRandom().nextIntWithUpperBound(maxOperand)
        
        if GKRandomSource.sharedRandom().nextIntWithUpperBound(2) == 0 {
            // addition
            maxAnswer = maxOperand + maxOperand
            operator_ = "+"
            correctAnswer = op1 + op2
        } else {
            // multiplication
            maxAnswer = maxOperand * maxOperand
            operator_ = "*"
            correctAnswer = op1 * op2
        }
        
        var answerOptions: [Int] = []
        while answerOptions.count < 3 { // TODO: get rid of magic number
            let randNum = GKRandomSource.sharedRandom().nextIntWithUpperBound(maxAnswer)
            if randNum != correctAnswer && !(answerOptions.contains(randNum)) { // we don't want duplicate answers
                answerOptions.append(randNum)
            }
        }
        
        answerOptions = GKRandomSource.sharedRandom().arrayByShufflingObjectsInArray(answerOptions) as! [Int]
        
        return QuestionModel.questionModel(question: "\(op1) \(operator_) \(op2)", correctAnswer: "\(correctAnswer)", incorrectAnswer1: "\(answerOptions[0])", incorrectAnswer2: "\(answerOptions[1])", incorrectAnswer3: "\(answerOptions[2])")
    }
}<file_sep>//
//  ViewController.swift
//  TrueFalseStarter
//
//  Created by <NAME> on 3/9/16.
//  Copyright © 2016 Treehouse. All rights reserved.
//

import UIKit
import GameKit
import AudioToolbox

class ViewController: UIViewController {
    
    let questionsPerRound = 4
    var questionsAsked = 0
    var correctQuestions = 0
    var correctAnswer: String = ""
    var triviaModel = TriviaModel()
    var mathMode: Bool = true
    
    var timer = NSTimer()
    let secondsPerQuestion: NSTimeInterval = 15
    
    var gameSound: SystemSoundID = 0
    var correctAnswerSound: SystemSoundID = 0
    var incorrectAnswerSound: SystemSoundID = 0
    
    @IBOutlet weak var questionField: UILabel!
    @IBOutlet weak var option1Button: UIButton!
    @IBOutlet weak var option2Button: UIButton!
    @IBOutlet weak var option3Button: UIButton!
    @IBOutlet weak var option4Button: UIButton!
    @IBOutlet weak var playMathButton: UIButton!
    @IBOutlet weak var playTriviaButton: UIButton!
    

    override func viewDidLoad() {
        super.viewDidLoad()
        loadGameSounds()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func displayQuestionAndOptions() {
        if mathMode {
            let qD = MathModel.randomMathEquationDict()
            displayQuestionAndOptions(questionDictionary: qD)
        } else if let qD = triviaModel.randomTrivia() {
            displayQuestionAndOptions(questionDictionary: qD)
        } else {
            nextRound(true)
        }
    }
    
    func displayQuestionAndOptions(questionDictionary qD: [String : String]) {
        // begin countdown
        timer = NSTimer.scheduledTimerWithTimeInterval(secondsPerQuestion, target: self, selector: #selector(ViewController.timesUp), userInfo: nil, repeats: false)
        
        // reset buttons
        removeBoardersFromButtons()
        enableButtons()
        
        questionField.text = qD[questionKey]
        option1Button.setTitle(qD[option1Key], forState: .Normal)
        option2Button.setTitle(qD[option2Key], forState: .Normal)
        option3Button.setTitle(qD[option3Key], forState: .Normal)
        option4Button.setTitle(qD[option4Key], forState: .Normal)
        
        correctAnswer = qD["Answer"]!
    }
    
    func displayScore() {
        hideAnswerButtons()
        showPlayButtons()
        
        questionField.text = "Way to go!\nYou got \(correctQuestions) out of \(questionsAsked) correct!"
        
    }
    
    @IBAction func checkAnswer(sender: UIButton? = nil) {
        timer.invalidate()
        
        // Increment the questions asked counter
        questionsAsked += 1
        
        disableButtons()
        
        showCorrectAnswer(correctAnswer)
        
        if let button = sender {
            if (button.currentTitle == correctAnswer) {
                correctQuestions += 1
                questionField.text = "Correct!"
                playCorrectAnswerSound()
            } else {
                highlightOptionButton(button, correctAnswer: false)
                questionField.text = "Sorry, wrong answer!"
                playIncorrectAnswerSound()
            }
        } else {
            questionField.text = "Sorry, time is up!"
            playIncorrectAnswerSound()
        }
        
        loadNextRoundWithDelay(seconds: 2)
    }
    
    func timesUp() {
        checkAnswer()
    }
    
    func nextRound(noMoreQuestionsLeft: Bool = false) {
        if questionsAsked == questionsPerRound || noMoreQuestionsLeft {
            // Game is over
            displayScore()
            // Get trivia model with question that were already asked
            triviaModel = TriviaModel()
        } else {
            // Continue game
            displayQuestionAndOptions()
        }
    }
    
    @IBAction func playAgain(sender: UIButton) {
        mathMode = sender === playMathButton
        
        playGameStartSound()
        
        hidePlayButtons()
        
        showAnswerButtons()
        
        questionsAsked = 0
        correctQuestions = 0
        nextRound()
    }
    

    
    // MARK: Helper Methods
    func hidePlayButtons(hide: Bool = true) {
        playMathButton.hidden = hide
        playTriviaButton.hidden = hide
    }
    
    func showPlayButtons() {
        hidePlayButtons(false)
    }
    
    func showCorrectAnswer(answer: String) {
        if option1Button.currentTitle == answer {
            highlightOptionButton(option1Button, correctAnswer: true)
        } else if option2Button.currentTitle == answer {
            highlightOptionButton(option2Button, correctAnswer: true)
        } else if option3Button.currentTitle == answer {
            highlightOptionButton(option3Button, correctAnswer: true)
        } else if option4Button.currentTitle == answer {
            highlightOptionButton(option4Button, correctAnswer: true)
        }
    }
    
    func hideAnswerButtons(hide: Bool = true) {
        option1Button.hidden = hide
        option2Button.hidden = hide
        option3Button.hidden = hide
        option4Button.hidden = hide
    }
    
    func showAnswerButtons() {
        hideAnswerButtons(false)
    }
    
    func enableButtons(enable: Bool = true) {
        option1Button.enabled = enable
        option2Button.enabled = enable
        option3Button.enabled = enable
        option4Button.enabled = enable
    }
    
    func disableButtons() {
        enableButtons(false)
    }
    
    func removeBoardersFromButtons() {
        option1Button.layer.borderWidth = 0.0
        option2Button.layer.borderWidth = 0.0
        option3Button.layer.borderWidth = 0.0
        option4Button.layer.borderWidth = 0.0
    }
    
    func highlightOptionButton(sender: UIButton, correctAnswer: Bool) {
        if correctAnswer {
            sender.layer.borderColor = UIColor.greenColor().CGColor
        } else {
            sender.layer.borderColor = UIColor.redColor().CGColor
        }
        sender.layer.borderWidth = 2.0
    }
    
    func loadNextRoundWithDelay(seconds seconds: Int) {
        // Converts a delay in seconds to nanoseconds as signed 64 bit integer
        let delay = Int64(NSEC_PER_SEC * UInt64(seconds))
        // Calculates a time value to execute the method given current time and delay
        let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, delay)
        
        // Executes the nextRound method at the dispatch time on the main queue
        dispatch_after(dispatchTime, dispatch_get_main_queue()) {
            self.nextRound()
        }
    }
    
    func loadGameSounds() {
        let pathToSoundFile1 = NSBundle.mainBundle().pathForResource("GameSound", ofType: "wav")
        let soundURL1 = NSURL(fileURLWithPath: pathToSoundFile1!)
        AudioServicesCreateSystemSoundID(soundURL1, &gameSound)
        
        let pathToSoundFile2 = NSBundle.mainBundle().pathForResource("doh1", ofType: "wav")
        let soundURL2 = NSURL(fileURLWithPath: pathToSoundFile2!)
        AudioServicesCreateSystemSoundID(soundURL2, &incorrectAnswerSound)
        
        let pathToSoundFile3 = NSBundle.mainBundle().pathForResource("roger-doger", ofType: "wav")
        let soundURL3 = NSURL(fileURLWithPath: pathToSoundFile3!)
        AudioServicesCreateSystemSoundID(soundURL3, &correctAnswerSound)
    }
    
    func playGameStartSound() {
        AudioServicesPlaySystemSound(gameSound)
    }
    
    func playCorrectAnswerSound() {
        AudioServicesPlaySystemSound(correctAnswerSound)
    }
    
    func playIncorrectAnswerSound() {
        AudioServicesPlaySystemSound(incorrectAnswerSound)
    }
}

<file_sep>//
//  TriviaModel.swift
//  TrueFalseStarter
//
//  Created by <NAME> on 6/26/16.
//  Copyright © 2016 Treehouse. All rights reserved.
//

import GameKit

let questionKey = "Question"
let option1Key = "Option 1"
let option2Key = "Option 2"
let option3Key = "Option 3"
let option4Key = "Option 4"
let answerKey = "Answer"

struct TriviaModel {
    
    var trivia: [[String : String]]
    let unshuffledTrivia: [[String : String]] = [
        QuestionModel.questionModel(question: "This was the only US President to serve more than two consecutive terms.", correctAnswer: "<NAME>", incorrectAnswer1: "<NAME>", incorrectAnswer2: "<NAME>", incorrectAnswer3: "<NAME>"),
        QuestionModel.questionModel(question: "Which of the following countries has the most residents?", correctAnswer: "Nigeria", incorrectAnswer1: "Russia", incorrectAnswer2: "Iran", incorrectAnswer3: "Vietnam"),
        QuestionModel.questionModel(question: "In what year was the United Nations founded?", correctAnswer: "1945", incorrectAnswer1: "1918", incorrectAnswer2: "1919", incorrectAnswer3: "1954"),
        QuestionModel.questionModel(question: "The Titanic departed from the United Kingdom, where was it supposed to arrive?", correctAnswer: "New York City", incorrectAnswer1: "Paris", incorrectAnswer2: "Washington D.C.", incorrectAnswer3: "Boston"),
        QuestionModel.questionModel(question: "What nation produces the most oil?", correctAnswer: "Canada", incorrectAnswer1: "Iran", incorrectAnswer2: "Iraq", incorrectAnswer3: "Brazil"),
        QuestionModel.questionModel(question: "Which country has most recently won consecutive World Cups in Soccer?", correctAnswer: "Italy", incorrectAnswer1: "Brazil", incorrectAnswer2: "Argentina", incorrectAnswer3: "Spain"),
        QuestionModel.questionModel(question: "Which of the following rivers is longest?", correctAnswer: "Yangtze", incorrectAnswer1: "Mississippi", incorrectAnswer2: "Congo", incorrectAnswer3: "Mekong"),
        QuestionModel.questionModel(question: "Which city is oldest?", correctAnswer: "Mexico City", incorrectAnswer1: "Cape Town", incorrectAnswer2: "San Juan", incorrectAnswer3: "Sydney"),
        QuestionModel.questionModel(question: "Which country was the first to allow women to vote in national elections?", correctAnswer: "Poland", incorrectAnswer1: "United States", incorrectAnswer2: "Sweden", incorrectAnswer3: "Senegal"),
        QuestionModel.questionModel(question: "Which of these countries won the most medals in the 2012 Summer Games?", correctAnswer: "Great Britian", incorrectAnswer1: "France", incorrectAnswer2: "Germany", incorrectAnswer3: "Japan")
    ]
    
    init() {
        trivia = GKRandomSource.sharedRandom().arrayByShufflingObjectsInArray(unshuffledTrivia) as! [[String : String]]
    }
    
    mutating func randomTrivia() -> [String : String]? {
        return self.trivia.popLast()
    }
}
<file_sep>//
//  QuestionModel.swift
//  TrueFalseStarter
//
//  Created by <NAME> on 6/30/16.
//  Copyright © 2016 Treehouse. All rights reserved.
//

import GameKit

struct QuestionModel {
    
    static func questionModel(question question: String, correctAnswer: String, incorrectAnswer1: String,
        incorrectAnswer2: String, incorrectAnswer3: String) -> [String : String] {
        
        let unshuffledOptions: [String] = [correctAnswer, incorrectAnswer1, incorrectAnswer2, incorrectAnswer3]
        let options = GKRandomSource.sharedRandom().arrayByShufflingObjectsInArray(unshuffledOptions) as! [String]
        
        return [ questionKey: question, option1Key: options[0], option2Key: options[1], option3Key: options[2], option4Key: options[3], answerKey: correctAnswer ]
    }
}