<?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);
}
- Output for 8.0.0 - 8.0.3, 8.1.23 - 8.1.33, 8.2.10 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
- [✊ vs ✊] あいこ
[✊ vs ✌] あなたの勝ち
[✊ vs 🖐] あなたの負け
[✌ vs ✊] あなたの負け
[✌ vs ✌] あいこ
[✌ vs 🖐] あなたの勝ち
[🖐 vs ✊] あなたの勝ち
[🖐 vs ✌] あなたの負け
[🖐 vs 🖐] あいこ
- Output for 7.4.0 - 7.4.16
- Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) in /in/MFqsQ on line 24
Process exited with code 255. - Output for 7.3.0 - 7.3.27
- Parse error: syntax error, unexpected '$my_hand' (T_VARIABLE), expecting ')' in /in/MFqsQ on line 19
Process exited with code 255.
preferences:
82.71 ms | 408 KiB | 5 Q