Skip to content
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

Test #2

Open
wants to merge 13 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
Binary file modified .DS_Store
Binary file not shown.
Binary file not shown.
Binary file modified SetProject4Animate/.DS_Store
Binary file not shown.
42 changes: 42 additions & 0 deletions SetProject4Animate/Concentration/Card.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// Card.swift
// Concentration
//
// Created by Craig Scott on 3/29/18.
// Copyright © 2018 Craig Scott. All rights reserved.
//

import Foundation

//No inheritance in struct, similar to class
//Structs are value types, classes are reference types
//What does that mean?
//Value type, copied
//Classes, pointed to
struct Card: Hashable
{
var hashValue: Int{return identifier}

static func ==(lhs: Card, rhs: Card) -> Bool {
return lhs.identifier == rhs.identifier
}


var isFaceUp = false
var isMatched = false
var isSeen = false;
private var identifier: Int

private static var identifierFactory = 0

static func getUniqueIdentifier() -> Int{

identifierFactory += 1
return identifierFactory
}

init()
{
self.identifier = Card.getUniqueIdentifier()
}
}
126 changes: 126 additions & 0 deletions SetProject4Animate/Concentration/Concentration.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
//
// Concentration.swift
// Concentration
//
// Created by Craig Scott on 3/29/18.
// Copyright © 2018 Craig Scott. All rights reserved.
//☺️😇😎😂😍😛
//👚👕👖👔👗👙
//🐶🐱🐭🐹🐰🦊
//🌕🌖🌗🌘🌑🌒
//🍏🍎🍐🍊🍋🍌
//🚗🚚🚜🏎🚓🚑
import UIKit
import Foundation

//Only classes get free initializer of vars are set

//Appending or doing any sort of assigning
//with structs copies them.
struct Concentration{


private(set) var cards = [Card]()


private(set)var score: Int = 0
// private var themes : [[String]] =
// [["☺️","😇","😎","😂","😍","😛"],
// ["👚","👕","👖","👔","👗","👙"],
// ["🐶","🐱","🐭","🐹","🐰","🦊"],
// ["🌕","🌖","🌗","🌘","🌑","🌒"],
// ["🍏","🍎","🍐","🍊","🍋","🍌"],
// ["🚗","🚚","🚜","🏎","🚓","🚑"]]


/*"🌕🌖🌗🌘🌑🌒",
"🍏🍎🍐🍊🍋🍌",
"🚗🚚🚜🏎🚓🚑"]*/


private(set) var flipCount = 0



private var indexOfOneAndOnlyFaceUpCard: Int?{
get{
return cards.indices.filter{cards[$0].isFaceUp}.oneAndOnly
// var foundIndex: Int? //Intialize optional int to check for face up card
// for index in cards.indices{ // Cycle through cards
// if cards[index].isFaceUp{
// if foundIndex == nil{ //If there is a face up card and foundIndex is empty, set it equal to foundIndex
// foundIndex = index
// } else{
// return nil //If there are no face up cards or there already is a foundIndex
// //TODO: Is foundIndex a local variable that is instantiated each get call, or is it saved in memory?
// }
// }
// }
// return foundIndex
}
set{
for index in cards.indices{
cards[index].isFaceUp = (index == newValue)
}
}
}
mutating func chooseCard(at index: Int)
{
assert(cards.indices.contains(index), "Concentration.chooseCard(at: \(index)): chosen index not in cards") //Makes sure this is true, otherwise crashes
flipCount += 1;
// TODO: This is important
if !cards[index].isMatched{
if let matchIndex = indexOfOneAndOnlyFaceUpCard, matchIndex != index{
//Check if cards match
if cards[matchIndex] == cards[index] {
cards[matchIndex].isMatched = true;
cards[index].isMatched = true;
score += 2
}
cards[index].isFaceUp = true;

}
else{


cards[index].isFaceUp = true;
indexOfOneAndOnlyFaceUpCard = index;
}
}
}

init(numberOfPairsOfCards: Int) {
assert(numberOfPairsOfCards > 0, "Concentration.init: \(numberOfPairsOfCards)): chosen number not")
//Create theme index to randomize which theme to choose

for _ in 1...numberOfPairsOfCards //Add card and randomly insert into cards
{
let card = Card()
cards.insert(card, at: cards.count.arc4Random)
cards.insert(card, at: cards.count.arc4Random)

}

}

}

extension Int{
var arc4Random: Int{
if self > 0{
return Int(arc4random_uniform(UInt32(self)))
}
else if self < 0{
return -Int(arc4random_uniform(UInt32(self)))
}
else{
return 0
}
}
}

extension Collection {
var oneAndOnly: Element?{
return count == 1 ? first : nil
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//
// ConcentrationThemeChooserViewController.swift
// Concentration
//
// Created by Craig Scott on 8/9/18.
// Copyright © 2018 Craig Scott. All rights reserved.
//

import UIKit

class ConcentrationThemeChooserViewController: UIViewController, UISplitViewControllerDelegate {

func splitViewController(_
splitViewController: UISplitViewController,
collapseSecondary secondaryViewController: UIViewController,
onto primaryViewController: UIViewController
) -> Bool {

if let cvc = secondaryViewController as? ConcentrationViewController{
if cvc.theme == nil{
return true
}
}
return false

}

private let themes : [String : String] =
["Emoji" : "☺️😇😎😂😍😛",
"Clothes" : "👚👕👖👔👗👙",
"Animals" : "🐶🐱🐭🐹🐰🦊"]

private var splitViewDetailConcentrationViewController : ConcentrationViewController? {
return splitViewController?.viewControllers.last as? ConcentrationViewController
}

override func awakeFromNib() {
splitViewController?.delegate = self
}

private var lastSeguedToConcentrationViewController : ConcentrationViewController?


@IBAction func changeTheme(_ sender: Any) {
if let cvc = splitViewDetailConcentrationViewController {
if let themeName = (sender as? UIButton)?.currentTitle, let theme = themes[themeName]{
cvc.theme = theme
}
}
else if let cvc = lastSeguedToConcentrationViewController {
if let themeName = (sender as? UIButton)?.currentTitle, let theme = themes[themeName]{


cvc.theme = theme

}
navigationController?.pushViewController(cvc, animated: true)
}
else{

performSegue(withIdentifier: "Choose Theme", sender: sender)

}
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if segue.identifier == "Choose Theme"{
if let themeName = (sender as? UIButton)?.currentTitle, let theme = themes[themeName]{
if let cvc = segue.destination as? ConcentrationViewController{

cvc.theme = theme
lastSeguedToConcentrationViewController = cvc
}
}
}
}




/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/

}
Loading