ポーカーの役判定
お題にしようと思っていたのに間違えてしまいました。今から変更可能でしょうか?
(説明) 当初間違ってトピックに投稿していたので、このようなコメントを付けていたのですが、 このコメントに気づいた管理人さんにお題に移していただきました。 (最初の2つだけ投稿日時が早いのはそのためです)
Posted feedbacks - PHP
以下の様にコールすると役を表示します。
echo Poker('SQSJSASKST');
パラメータチェックを怠っています。
echo Poker('SQSJSASKST');
パラメータチェックを怠っています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | <?php
function Poker($c)
{
CSort($c);
if (IsFlush($c)) {
if (IsStraight($c)) {
if ($c[1] == 'A') {
return 'Royal flush';
}
return 'Straight flush';
}
return 'Flush';
} else {
if (IsStraight($c)) {
return 'Straight';
}
$wk = array();
$wk[$c[1]]++;
$wk[$c[3]]++;
$wk[$c[5]]++;
$wk[$c[7]]++;
$wk[$c[9]]++;
arsort($wk);
switch (array_shift($wk).array_shift($wk)) {
case '41': return 'Four of a kind';
case '32': return 'Full house';
case '31': return 'Three of a kind';
case '22'; return 'Two pair';
case '21'; return 'One pair';
}
}
return 'No pair';
}
function IsFlush($c)
{
if ($c[0] == $c[2] && $c[0] == $c[4] && $c[0] == $c[6] && $c[0] == $c[8]) {
return TRUE;
}
return FALSE;
}
function IsStraight($c)
{
$rank = 'A23456789TJQK';
$num = $c[1].$c[3].$c[5].$c[7].$c[9];
if (strpos($rank, $num) !== FALSE || $num == 'ATJQK') {
return TRUE;
}
return FALSE;
}
function CSort(&$c)
{
$rep = array('1'=>'A', '10'=>'T', '11'=>'J', '12'=>'Q', '13'=>'K');
$wk = array($c[0].$c[1] => $c[1], $c[2].$c[3] => $c[3],
$c[4].$c[5] => $c[5], $c[6].$c[7] => $c[7],
$c[8].$c[9] => $c[9]);
$wk = str_ireplace($rep, array_flip($rep), $wk);
asort($wk);
$c = '';
foreach ($wk as $key => $value) {
$c .= $key;
}
}
?>
|
PHPの関数はいっぱいある。
array_count_values は、こんなのあるかな?と思ってマニュアル探したらやっぱりあった。
array_count_values は、こんなのあるかな?と思ってマニュアル探したらやっぱりあった。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | #!/usr/bin/php
<?php
$suits = array();
$ranks = array();
for ($i = 0; $i < 10; $i += 2) {
$suits[] = $argv[1][$i];
$ranks[] = strtr($argv[1][$i+1], "TJQKA", "ABCDE");
}
sort($suits);
sort($ranks);
$suitCnt = array_count_values($suits);
asort($suitCnt);
$rankCnt = array_count_values($ranks);
arsort($rankCnt);
$straight = false;
$flush = false;
$suitHist = array_values($suitCnt);
if ($suitHist[0] == 5) {
$flush = true;
}
$rankHist = array_values($rankCnt);
if (strstr("23456789ABCDE,2345E", implode('',$ranks))) {
$straight = true;
}
$hand = '';
if ($straight && $flush) {
if (implode('', $ranks) == 'ABCDE') {
$hand = "Royal straight flush";
} else {
$hand = "Straight flush";
}
} else if ($rankHist[0] == 4) {
$hand = "Four of a kind";
} else if ($rankHist[0] == 3 && $rankHist[1] == 2) {
$hand = "Full house";
}
if ($hand == '') {
if ($flush) {
$hand = "Flush";
} else if ($straight) {
$hand = "Straight";
} else if ($rankHist[0] == 3) {
$hand = "Three of a kind";
} else if ($rankHist[0] == 2 && $rankHist[1] == 2) {
$hand = "Two pairs";
} else if ($rankHist[0] == 2) {
$hand = "One pair";
} else {
$hand = "No Pair";
}
}
echo $hand . "\n";
?>
|





xsd
#4978()
Rating6/10=0.60
引数に手札を与えると、ポーカーの役を表示するプログラムを作ってください。
条件:
実行例:
see: ポーカー - Wikipedia
1 reply [ reply ]