<?php
namespace Janken;
use Closure;
const WIN_LOSE_TABLE = [
'グー' => ['チョキ' => 1, 'パー' => -1],
'チョキ' => ['パー' => 1, 'グー' => -1],
'パー' => ['グー' => 1, 'チョキ' => -1],
];
const EMOJI_TABLE = [
'グー' => '✊',
'チョキ' => '✌',
'パー' => '🖐',
];
$printer = fn(string $my_hand, string $your_hand, int $result): string =>
sprintf("[%s vs %s] %s",
EMOJI_TABLE[$my_hand],
EMOJI_TABLE[$your_hand],
match ($result) {
-1 => 'あなたの負け',
0 => 'あいこ',
1 => 'あなたの勝ち',
}
);
foreach (['グー', 'チョキ', 'パー'] as $my_hand) {
foreach (['グー', 'チョキ', 'パー'] as $your_hand) {
echo battle($my_hand, $your_hand, $printer), PHP_EOL;
}
}
function battle(string $my_hand, string $your_hand, Closure $printer): string
{
return $printer($my_hand, $your_hand, WIN_LOSE_TABLE[$my_hand][$your_hand] ?? 0);
}
preferences:
16.31 ms | 402 KiB | 5 Q