Skip to content

Instantly share code, notes, and snippets.

@hmhmsh
Last active March 31, 2016 05:52
Show Gist options
  • Save hmhmsh/f5f12eb77ecd38770f25deb41eaae3e8 to your computer and use it in GitHub Desktop.
Save hmhmsh/f5f12eb77ecd38770f25deb41eaae3e8 to your computer and use it in GitHub Desktop.
func() {
let recursion = RecursionClosure(itemArray: ["ずん", "どこ"], decisionStr: "ずんずんずんずんどこ", maxCount: 5)
let result = recursion.recursionZunDoko()
}
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