LoginSignup
7
9

More than 5 years have passed since last update.

ズンドコキヨシ with C#

Last updated at Posted at 2016-03-13

流行っているようなので。ズンドコキヨシ

すでにやられていますが、Rxの教材として面白そう。

先人
C# と Reactive Extensions でズンドコキヨシ

コード

private static readonly string Zun = "ズン";
private static readonly string Doco = "ドコ";
private readonly string[] Pattern = new[] { Zun, Zun, Zun, Zun, Doco };
private bool KiyoshiSinging = true;

public void ZunDoco()
{
    var rand = new Random();
    var dis = Observable.Create<string>(observer =>
        {
            while (KiyoshiSinging)
            {
                var nextWord = rand.Next() % 2 == 0 ? Zun : Doco;
                Console.Write(nextWord);
                observer.OnNext(nextWord);
            }
            observer.OnCompleted();
            return () => { };
        })
        .Buffer(5, 1)
        .Subscribe<IList<string>>(next =>
        {
            if (next.Count < 5)
            {
                return;
            }
            if (next.SequenceEqual(Pattern))
            {
                Console.WriteLine("キ・ヨ・シ!");
                this.KiyoshiSinging = false;
            }
        }, () => Console.WriteLine("終了"));
}

実行結果

ドコドコズンズンドコドコズンドコズンドコズンズンズンドコドコドコドコズンドコドコズンズンドコズンズンズンズンドコキ・ヨ・シ!
終了

7
9
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
7
9