LoginSignup
2
0

More than 3 years have passed since last update.

[PHP]FizzBuzzゲームをifとforを使わないで作った結果www

Posted at

1.rangeで配列生成してarray_filterで抽出してからarray_fill_keysで文字を埋めてarray_replaceで合体してarray_mapで要素を出力

fizz_buzz.php
<?php
$array = range(0, 100);

$fizz_array = array_fill_keys(array_filter($array, function($value) {
    return $value % 3 === 0;
}), 'Fizz');

$buzz_array = array_fill_keys(array_filter($array, function($value) {
    return $value % 5 === 0;
}), 'Buzz');

$fizz_buzz_array = array_fill_keys(array_filter($array, function($value) {
    return $value % 3 === 0 && $value % 5 === 0;
}), 'FizzBuzz');


$result = array_replace($array, $fizz_array, $buzz_array, $fizz_buzz_array);
unset($result[0]);

array_map($result, function($value){
    echo $value . '<br>';
});

ちなみにarray_filterの代わりに

range(0, 100, 3); // rangenの第3引数を指定すると要素の増加数をしていできる。

でも可。

2.クラスを使う

fizzbuzz.php
<?php
class FizzBuzz {
    const FIZZ = 3;
    const BUZZ = 5;
    public static $counter = 0;
    private $number;
    public function __construct () {
        self::$counter++;
        $this->number = self::$counter;
    }

    public function build() {
        return self::getFizzBuzz() ? 'FizzBuzz' :
            self::getFizz() ?  'Fizz':
                self::getBuzz() ? 'Buzz' : $this->number;
    }

    private function getFizzBuzz() {
        return $this->number % self::FIZZ === 0 && $this->number % self::BUZZ === 0;
    }

    private function getFizz() {
        return $this->number % self::FIZZ === 0;
    }

    private function getBuzz() {
        return $this->number % self::BUZZ === 0;
    }
}

$array = array_fill(1, 100, 'Inori Minase');

array_walk($array, function(&$value){
    $fizz_buzz_incetance = new FizzBuzz();
    $value = $fizz_buzz_incetance->build();
});

$obj = new ArrayObject($array);
$it = $obj->getIterator();

while ($it->valid()){
    echo $it->current() . "</br>";
    $it->next();
}

FizzBuzzCounterクラスは、インスタンスが生成されるたびにカウンターを1づつ増やしていくよ。

まとめ

困ったら公式ドキュメントを読もう!

おまけ

chain of responsibility

<?php
class FizzBuzzClient {
    public static $counter = 0;
    private $number;
    public function __construct () {
        self::$counter++;
        $this->number = self::$counter;
    }

    public function getNumber() {
        return $this->number;
    }
}

abstract class Handler {
    private $next;

    public function setNext(Handler $next) {
        $this->next = $next;
    }

    public function request (FizzBuzzClient $client) {
        $this->resolve($client) ? $this->done() : $this->request($client);
    }

    abstract protected function resolve(FizzBuzzClient $client);

    abstract protected function done (FizzBuzzClient $client);

}

class NoHandler extends Handler {
    protected function resolve (FizzBuzzClient $client) {
        return false;
    }

    protected function done (FizzBuzzClient $client) {
        return false;
    }
}

class FizzBuzzHandler extends Handler {
    protected function resolve (FizzBuzzClient $client) {
        return $client->getNumber % 3 === 0 && $client->getNumber % 5;
    }

    protected function done (FizzBuzzClient $client) {
        return 'FizzBuzz';
    }
}

class FizzHandler extends Handler {
    protected function resolve (FizzBuzzClient $client) {
        return $client->getNumber % 3 === 0;
    }

    protected function done (FizzBuzzClient $client) {
        return 'Fizz';
    }
}

class BuzzHandler extends Handler {
    protected function resolve (FizzBuzzClient $client) {
        return $client->getNumber % 3 === 0 && $client->getNumber % 5;
    }

    protected function done (FizzBuzzClient $client) {
        return 'Buzz';
    }
}

class LastHandler extends Handler {
    protected function resolve (FizzBuzzClient $client) {
        return true;
    }

    protected function done (FizzBuzzClient $client) {
        return $client->getNumber;
    }
}

$array = array_fill(1, 100, 'Inori Minase');

$no_handler = new NoHandler();
$fizz_buzz = new FizzBuzzHandler();
$fizz = new FizzHandler();
$buzz = new BuzzHandler();
$last = new LastHandler();

$no_handler->setNext($fizz_buzz)->setNext($fizz)->setNext($buzz)->setNext($last);

foreach ($array as $key => $value) {
$fizz_buzz_client = new FizzBuzzClient();
    $no_Handler->request($fizz_buzz_client);
}

ここまで書いたけどメモリ足りなくて断念した。

おまけ2

「何って...俺はただif文を使っただけだが...?」キョトン

<?php
for ($i = 1; $i <= 100; $i++) {
    if ($i % 3 == 0 && $i % 5 == 0) {
        echo 'FizzBuzz</br>';
    } elseif ($i % 3 == 0) {
        echo 'Fizz</br>';
    } elseif ($i % 5 == 0) {
        echo 'Buzz</br>';
    } else {
        echo $i .'</br>';
    }
}
2
0
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
2
0