Skip to content

Finished Project Requirements. #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 106 additions & 75 deletions HeadsUpper/HeadsUpper.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

217 changes: 217 additions & 0 deletions HeadsUpper/HeadsUpper/AnswerVIewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
//
// AnswerVIewController.swift
// HeadsUpper
//
// Created by Jovanny Espinal on 2/21/16.
// Copyright © 2016 Jovanny Espinal. All rights reserved.
//

import Foundation
import UIKit

class AnswerViewController: UIViewController
{
@IBOutlet weak var answerLabel: UILabel!

@IBOutlet weak var timerLabel: UILabel!
var timer: NSTimer?
var count = 3
var answers: [String] = []
var didWin = false


override func viewDidLoad()
{
super.viewDidLoad()

updateLabels()

addSwipeGestures()
}

override func viewDidAppear(animated: Bool)
{
super.viewDidAppear(animated)

startTimer()
}

}


// MARK: Game State Functionalities
extension AnswerViewController
{
func nextQuestion()
{
if !didWin {
answers.removeFirst()

checkForWin()
}

updateAnswerLabel()
}

func checkForWin()
{
didWin = answers.isEmpty
}

func checkForStatusOfPlayer()
{
if count == 0 {
gameOver()
} else if didWin {
timer!.invalidate()

updateLabels()

displayAlert()
} else {
updateLabels()
}
}
}

// MARK: Timer Functionalities
extension AnswerViewController
{
func startTimer()
{
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "countDown", userInfo: nil, repeats: true)

NSRunLoop.currentRunLoop().addTimer(timer!, forMode: NSRunLoopCommonModes)
}

func countDown()
{
count -= 1
checkForStatusOfPlayer()
}
}

// MARK: Gesture Functionalities
extension AnswerViewController
{
func addSwipeGestures()
{
let swipeRight = UISwipeGestureRecognizer(target: self, action: "correct")
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
self.view.addGestureRecognizer(swipeRight)

let swipeLeft = UISwipeGestureRecognizer(target: self, action: "skip")
swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
self.view.addGestureRecognizer(swipeLeft)
}

func correct()
{
nextQuestion()

changeBackgroundColor(UIColor.greenColor())
}

func skip()
{
nextQuestion()

changeBackgroundColor(UIColor.redColor())
}
}

// MARK: UI Update Functionalities
extension AnswerViewController
{
func updateLabels()
{
updateAnswerLabel()
updateTimerLabel()
}

func updateTimerLabel()
{
if count == 0 || didWin {
timerLabel.text = nil
} else if !didWin {
timerLabel.text = "\(count)"
}

}

func updateAnswerLabel()
{
if didWin {
answerLabel.text = "WINNER WINNER CHICKEN DINNER!"
} else if count == 0 {
answerLabel.text = "WOMP WOMP! TRY AGAIN!"
} else {
answerLabel.text = answers.first
}

}

func changeBackgroundColor(toColor: UIColor)
{
if didWin {
self.view.backgroundColor = UIColor.greenColor()
} else {
UIView.animateWithDuration(0.55, animations: {
self.view.backgroundColor = toColor
self.view.backgroundColor = UIColor.whiteColor()
})
}

}

func gameOver()
{
self.view.backgroundColor = UIColor.redColor()

timer!.invalidate()

updateLabels()

removeGestureRecognizers()

displayAlert()
}

func removeGestureRecognizers()
{
if self.view.gestureRecognizers != nil {
for gesture in self.view.gestureRecognizers! {
if let recognizer = gesture as? UISwipeGestureRecognizer {
self.view.removeGestureRecognizer(recognizer)
}
}
}
}

func displayAlert()
{
let alertController = UIAlertController(title: "Game Over", message: returnAlertMessage(), preferredStyle: .Alert)

let defaultAction = UIAlertAction(title: "OK", style: .Default) { _ in
self.dismissViewControllerAnimated(true, completion: nil)
}

alertController.addAction(defaultAction)

presentViewController(alertController, animated: true, completion: nil)
}

func returnAlertMessage() -> String
{
var alertMessage = ""

if didWin {
alertMessage = "Nailed it! Try a different topic!"
} else if count == 0 {
alertMessage = "Bummer! You lost. Try again!"
}

return alertMessage
}

}
17 changes: 0 additions & 17 deletions HeadsUpper/HeadsUpper/AppDelegate.h

This file was deleted.

45 changes: 0 additions & 45 deletions HeadsUpper/HeadsUpper/AppDelegate.m

This file was deleted.

45 changes: 45 additions & 0 deletions HeadsUpper/HeadsUpper/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// AppDelegate.swift
// HeadsUpper
//
// Created by Jovanny Espinal on 2/21/16.
// Copyright © 2016 Jovanny Espinal. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
return true
}

func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,36 @@
"idiom" : "iphone",
"size" : "60x60",
"scale" : "3x"
},
{
"idiom" : "ipad",
"size" : "29x29",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "29x29",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "40x40",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "40x40",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "76x76",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "76x76",
"scale" : "2x"
}
],
"info" : {
Expand Down
Loading