// 手札
$cards = [
[‘suit’=>’heart’, ‘number’=>7],
[‘suit’=>’club’, ‘number’=>7],
[‘suit’=>’diamond’, ‘number’=>4],
[‘suit’=>’heart’, ‘number’=>7],
[‘suit’=>’spade’, ‘number’=>11],
];
$suits_order = [“spade”, “heart”, “diamond”, “club”];
usort($cards, ‘compare_cards’);
//不正チェック関数
function judge($cards) {
// この関数内に処理を記述
$uniqueCards = array_unique($cards, SORT_REGULAR);
if (count($cards) != count($uniqueCards)) {
return “手札は不正”;
}
// 役判定
// 結果を返す
}
// カードの並び替え
//数字五個の範囲に連番で並んでいるかどうかを確認するための専用の関数が存在する←PHP
//絵柄もそろえた方が良い(ハート ハート スペード スペード クローバー)
//役判定で使う為 [[]]
function compare_cards($a, $b){
global $suits_order;
if ($a[‘number’] == $b[‘number’]){
if ($a[‘suit’] == $b[‘suit’]){
return 0; // 数字とスートが同じ場合は等しい
} else {
return array_search($a[‘suit’], $suits_order) > array_search($b[‘suit’], $suits_order) ? 1 : -1;
}
} else {
return $a[‘number’] > $b[‘number’] ? 1 : -1;
}
}
//役判定関数
function check_cards($cards){
if( $cards[0][‘number’] === 1 &&
$cards[1][‘number’] === 10 &&
$cards[2][‘number’] === 11 &&
$cards[3][‘number’] === 12 &&
$cards[4][‘number’] === 13 &&
$cards[0][‘suit’] === $cards[1][‘suit’] &&
$cards[1][‘suit’] === $cards[2][‘suit’] &&
$cards[2][‘suit’] === $cards[3][‘suit’] &&
$cards[3][‘suit’] === $cards[4][‘suit’] ){
echo “ロイヤルストレートフラッシュです。”;
}elseif($cards[0][‘number’] + 1 === $cards[1][‘number’] &&
$cards[1][‘number’] + 1 === $cards[2][‘number’] &&
$cards[2][‘number’] + 1 === $cards[3][‘number’] &&
$cards[3][‘number’] + 1 === $cards[4][‘number’] &&
$cards[0][‘suit’] === $cards[1][‘suit’] &&
$cards[1][‘suit’] === $cards[2][‘suit’] &&
$cards[2][‘suit’] === $cards[3][‘suit’] &&
$cards[3][‘suit’] === $cards[4][‘suit’]){
echo “ストレートフラッシュです。”;
}elseif($cards[0][‘number’] === $cards[1][‘number’] &&
$cards[1][‘number’] === $cards[2][‘number’] &&
$cards[2][‘number’] === $cards[3][‘number’] ||
$cards[1][‘number’] === $cards[2][‘number’] &&
$cards[2][‘number’] === $cards[3][‘number’] &&
$cards[3][‘number’] === $cards[4][‘number’] ){
echo “フォーカードです。”;
}elseif($cards[0][‘number’] === $cards[1][‘number’] &&
$cards[1][‘number’] === $cards[2][‘number’] &&
$cards[3][‘number’] === $cards[4][‘number’] ||
$cards[0][‘number’] === $cards[1][‘number’] &&
$cards[2][‘number’] === $cards[3][‘number’] &&
$cards[3][‘number’] === $cards[4][‘number’] ){
echo “フルハウスです。”;
}elseif($cards[0][‘suit’] === $cards[1][‘suit’] &&
$cards[1][‘suit’] === $cards[2][‘suit’] &&
$cards[2][‘suit’] === $cards[3][‘suit’] &&
$cards[3][‘suit’] === $cards[4][‘suit’] ){
echo “フラッシュです。”;
}elseif($cards[0][‘number’] + 1 === $cards[1][‘number’] &&
$cards[1][‘number’] + 1 === $cards[2][‘number’] &&
$cards[2][‘number’] + 1 === $cards[3][‘number’] &&
$cards[3][‘number’] + 1 === $cards[4][‘number’] ){
echo “ストレートです。”;
}elseif($cards[0][‘number’] === $cards[1][‘number’] &&
$cards[1][‘number’] === $cards[2][‘number’] ||
$cards[1][‘number’] === $cards[2][‘number’] &&
$cards[2][‘number’] === $cards[3][‘number’] ||
$cards[2][‘number’] === $cards[3][‘number’] &&
$cards[3][‘number’] === $cards[4][‘number’] ){
echo “スリーカードです。”;
}elseif($cards[0][‘number’] === $cards[1][‘number’] &&
$cards[2][‘number’] === $cards[3][‘number’] ||
$cards[1][‘number’] === $cards[2][‘number’] &&
$cards[3][‘number’] === $cards[4][‘number’] ){
echo “ツーペアです。”;
}elseif( $cards[0][‘number’] === $cards[1][‘number’] ||
$cards[1][‘number’] === $cards[2][‘number’] ||
$cards[2][‘number’] === $cards[3][‘number’] ||
$cards[3][‘number’] === $cards[4][‘number’]){
echo “ワンペアです。”;
}else{
echo “ハイカードです。”;
}
}
?>
<!DOCTYPE html>
<html lang=”ja”>
<head>
<meta charset=”utf-8″>
<title>ポーカー役判定</title>
</head>
<body>
<section>
<p>手札は<?php foreach($cards as $card): ?><?=$card[‘suit’].$card[‘number’] ?><?php endforeach; ?></p>
<p><?=check_cards($cards) ?></p>
</section>
</body>
</html>