LoginSignup
6
6

More than 5 years have passed since last update.

今更Swiftでズンドコキヨシ。Protocol Extension編。

Last updated at Posted at 2016-03-30

まずはコード

protocol 節系 {
    init()
    func 鳴らす()
}

extension 節系 {
    func 鳴らす() {
        print(self)
    }
}

protocol フィニッシュ系: 節系 { }

struct ズン: 節系 { }
struct ドコ: 節系 { }
struct : フィニッシュ系 { }

let 節集: [節系] = [ズン(), ドコ()]
var ズンカウント = 0
let フィニッシュズンカウント = 4

while true {
    let  = 節集[random() % 節集.count]
    .鳴らす()

    if  is ズン {
        ズンカウント += 1
    } else if  is ドコ
        && ズンカウント == フィニッシュズンカウント {
        break
    } else {
        ズンカウント = 0
    }
}

().鳴らす()

ポイント

  • ズンドコキ・ヨ・シ!を文字列としてハードコーディングしていません。(ホントか??!!
  • Protocol Extensionで鳴らすを実装しています。
  • キ・ヨ・シ!の最後の!がアンラップに見える。

出力

...
ズン()
ドコ()
ズン()
ズン()
ズン()
ズン()
ズン()
ドコ()
ドコ()
ズン()
ドコ()
ズン()
ズン()
ズン()
ズン()
ドコ()
キ・ヨ・シ!()

まとめ

ほんとごめんなさい

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6