Last active
April 10, 2016 07:49
-
-
Save kakajika/55244fe81395a62333fea653ed095b46 to your computer and use it in GitHub Desktop.
Swiftの再帰的enumとパターンマッチでズンドコ
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 | |
| enum Zundoko: CustomStringConvertible { | |
| case Start | |
| indirect case Zun(Zundoko) | |
| indirect case Doko(Zundoko) | |
| indirect case Kiyoshi(Zundoko) | |
| var description: String { | |
| switch self { | |
| case Start: return "スタート!" | |
| case Zun(let zd): return "\(zd)\nズン" | |
| case Doko(let zd): return "\(zd)\nドコ" | |
| case Kiyoshi(let zd): return "\(zd)\nキ・ヨ・シ!" | |
| } | |
| } | |
| func zundoko() -> Zundoko { | |
| if case .Doko(.Zun(.Zun(.Zun(.Zun)))) = self { | |
| return .Kiyoshi(self) | |
| } | |
| return (arc4random_uniform(2) > 0) ? Zun(self).zundoko() : Doko(self).zundoko() | |
| } | |
| } | |
| print(Zundoko.Start.zundoko()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment