LoginSignup
8
7

More than 5 years have passed since last update.

ズンドコジェネレータで、ズンドコキヨシ with PHP

Last updated at Posted at 2016-03-12

ズンドコキヨシまとめ

ズンドコジェネレータで、普通に実装。

<?php
const ZUN = 'ズン';
const DOKO = 'ドコ';

$generator = function () {
    $zundoko = [ZUN, DOKO];
    for (;;) {
        yield $zundoko[mt_rand(0, 1)];
    }
};

$actual = [];
$expected = [ZUN, ZUN, ZUN, ZUN, DOKO];
foreach ($generator() as $v) {
    echo $v;

    $actual[] = $v;
    if (count($actual) > 5) {
        array_shift($actual);
    }

    if ($actual === $expected) {
        break;
    }
}

echo 'キ・ヨ・シ!';

実行結果

1__tmux.png

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