10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ズンドコキヨシ with PHP

Last updated at Posted at 2016-03-11

5個ずつキヨシる

<?php
$z = 'ズン';
$d = 'ドコ';
$zd = [$z, $d];
while (true) {
    $zundoko = array_map(function() use ($zd) {
        return $zd[array_rand($zd)];
    }, range(0, 4));

    if ([$z, $z, $z, $z, $d] === $zundoko) {
        break;
    }
}
echo 'キ・ヨ・シ!';

1個ずつキヨシる

<?php
list($z, $d) = $zd = ['ズン', 'ドコ'];
$zundoko = range(0, 4);
start:
    array_shift($zundoko);
    $zundoko[] = $zd[array_rand($zd)];

    if ([$z, $z, $z, $z, $d] === $zundoko) {
        echo 'キ・ヨ・シ!';
        exit(0);
    }
goto start;

組み合わせてかつarray_fillとか使ってみた

<?php
list($z, $d) = $zd = ['ズン', 'ドコ'];
$f = function() use ($zd) {
    return $zd[array_rand($zd)];
};
$zundoko = array_map($f, range(0, 4));
while (true) {
    if (array_fill(0, 4, $z) + [4 => $d] === $zundoko) {
        break;
    }
    $zundoko[] = $f();
    array_shift($zundoko);
}
echo 'キ・ヨ・シ!';

再起+クロージャの即時呼び出し (クロージャの即時呼び出ししたかったけど即時呼び出し内で再起できなかった)

<?php
$zd = ['ズン', 'ドコ'];
$expire = array_map(function($i) use ($zd) {
    return $i !== 4 ? $zd[0] : $zd[1];
}, range(0, 4));
($f = function ($zundoko = array()) use ($expire, $zd, &$f) {
    if ($expire === $zundoko) {
        return;
    }
    $zundoko[] = $zd[array_rand($zd)];
    $f(array_slice($zundoko, -5, 5));
})();
echo 'キ・ヨ・シ!';
10
10
5

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?