Last active
March 31, 2016 05:52
-
-
Save hmhmsh/f5f12eb77ecd38770f25deb41eaae3e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func() { | |
let recursion = RecursionClosure(itemArray: ["ずん", "どこ"], decisionStr: "ずんずんずんずんどこ", maxCount: 5) | |
let result = recursion.recursionZunDoko() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import UIKit | |
class Recursion: NSObject { | |
internal var zundoko = [String]() | |
internal var item: [String]! | |
internal var decision: String! | |
internal var count: Int! | |
internal var resultNumber = 0 | |
override init() { | |
super.init() | |
} | |
convenience init(itemArray: [String], decisionStr: String, maxCount: Int) { | |
self.init() | |
item = itemArray | |
result = resultStr | |
decision = decisionStr | |
count = maxCount | |
} | |
func recursionZunDoko() -> Bool { | |
zundoko.append(getZundoko()) | |
resultNumber += 1 | |
if checkZundoko() { | |
return true | |
} else { | |
return recursionZunDoko() | |
} | |
} | |
internal func getZundoko() -> String { | |
let random = arc4random_uniform(UInt32(item.count)) | |
return item[Int(random)] | |
} | |
internal func checkZundoko() -> Bool { | |
if zundoko.count > count { | |
zundoko.removeFirst() | |
} | |
let str = zundoko.reduce("", combine: +) | |
if str == decision { | |
return true | |
} else { | |
return false | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment