Here, have a 5 card poker hand checker I developed today for my casino.
It's already good for start. With it you can already shuffle a deck, deal and check the hand strength.
GL with your project.
class Poker{
var $jacks = false;
var $twopairs = false;
var $threeofakind = false;
var $straight = false;
var $flush = false;
var $fullhouse = false;
var $fourofakind = false;
var $straightflush = false;
var $royalstraightflush = false;
var $held = "00000";
var $comb = 0;
var $card1;
var $card2;
var $card3;
var $card4;
var $card5;
function readHand($c1,$c2,$c3,$c4,$c5,$minCval){
global $deck;
$this->card1 = $deck->getCardByNum($c1);
$this->card2 = $deck->getCardByNum($c2);
$this->card3 = $deck->getCardByNum($c3);
$this->card4 = $deck->getCardByNum($c4);
$this->card5 = $deck->getCardByNum($c5);
$this->held = "00000";
$this->jacks = false;
$this->twopairs = false;
$this->threeofakind = false;
$this->straight = false;
$this->flush = false;
$this->fullhouse = false;
$this->fourofakind = false;
$this->straightflush = false;
$this->royalstraightflush = false;
$this->checkRoyalSF();
$this->checkSF();
$this->checkFourOfAKind();
$this->checkFH();
$this->checkFlush();
$this->checkStr();
$this->check3OfAKind();
$this->check2Pairs();
$this->checkPair($minCval);
}
function faceToNum($face){
switch($face){
case "A": return 1; break;
case "2": return 2; break;
case "3": return 3; break;
case "4": return 4; break;
case "5": return 5; break;
case "6": return 6; break;
case "7": return 7; break;
case "8": return 8; break;
case "9": return 9; break;
case "T": return 10; break;
case "J": return 11; break;
case "Q": return 12; break;
case "K": return 13; break;
}
}
function checkStrPattern($nums){
sort($nums);
$test = array(1,2,3,4,5);
if($nums == $test) return true;
$test = array(2,3,4,5,6);
if($nums == $test) return true;
$test = array(3,4,5,6,7);
if($nums == $test) return true;
$test = array(4,5,6,7,8);
if($nums == $test) return true;
$test = array(5,6,7,8,9);
if($nums == $test) return true;
$test = array(6,7,8,9,10);
if($nums == $test) return true;
$test = array(7,8,9,10,11);
if($nums == $test) return true;
$test = array(8,9,10,11,12);
if($nums == $test) return true;
$test = array(9,10,11,12,13);
if($nums == $test) return true;
$test = array(1,10,11,12,13);
if($nums == $test) return true;
return false;
}
function checkPair($minCval){
if($this->royalstraightflush) return;
if($this->straightflush) return;
if($this->fourofakind) return;
if($this->fullhouse) return;
if($this->flush) return;
if($this->straight) return;
if($this->threeofakind) return;
if($this->twopairs) return;
$validCards = array();
$z = 0;
$pat = array(0,0,0,0,0);
if($this->faceToNum($this->card1['card']) == 1 || $this->faceToNum($this->card1['card']) > $minCval){
$validCards[$z]['card'] = $this->card1['card'];
$validCards[$z]['pos'] = 0;
$z++;
}
if($this->faceToNum($this->card2['card']) == 1 || $this->faceToNum($this->card2['card']) > $minCval){
$validCards[$z]['card'] = $this->card2['card'];
$validCards[$z]['pos'] = 1;
$z++;
}
if($this->faceToNum($this->card3['card']) == 1 || $this->faceToNum($this->card3['card']) > $minCval){
$validCards[$z]['card'] = $this->card3['card'];
$validCards[$z]['pos'] = 2;
$z++;
}
if($this->faceToNum($this->card4['card']) == 1 || $this->faceToNum($this->card4['card']) > $minCval){
$validCards[$z]['card'] = $this->card4['card'];
$validCards[$z]['pos'] = 3;
$z++;
}
if($this->faceToNum($this->card5['card']) == 1 || $this->faceToNum($this->card5['card']) > $minCval){
$validCards[$z]['card'] = $this->card5['card'];
$validCards[$z]['pos'] = 4;
}
if(sizeof($validCards) < 2) return;
for($x = 0; $x <= $z; $x++){
for($y = 0; $y <= $z; $y++){
if($y == $x) continue;
if($validCards[$x]['card'] == $validCards[$y]['card']){
$this->jacks = true;
$pat[$validCards[$x]['pos']] = 1;
$pat[$validCards[$y]['pos']] = 1;
$this->held = implode("",$pat);
$this->comb = 1;
}
}
}
}
function check2Pairs(){
if($this->royalstraightflush) return;
if($this->straightflush) return;
if($this->fourofakind) return;
if($this->fullhouse) return;
if($this->flush) return;
if($this->straight) return;
if($this->threeofakind) return;
//Pat 1: 1+2|3+4
if($this->card1['card'] == $this->card2['card'] && $this->card3['card'] == $this->card4['card']){
$this->twopairs = true;
$this->held = "11110";
$this->comb = 2;
}
//Pat 2: 1+2|3+5
if($this->card1['card'] == $this->card2['card'] && $this->card3['card'] == $this->card5['card']){
$this->twopairs = true;
$this->held = "11101";
$this->comb = 2;
}
//Pat 3: 1+2|4+5
if($this->card1['card'] == $this->card2['card'] && $this->card4['card'] == $this->card5['card']){
$this->twopairs = true;
$this->held = "11011";
$this->comb = 2;
}
//Pat 4: 1+3|2+4
if($this->card1['card'] == $this->card3['card'] && $this->card2['card'] == $this->card4['card']){
$this->twopairs = true;
$this->held = "11110";
$this->comb = 2;
}
//Pat 5: 1+3|2+5
if($this->card1['card'] == $this->card3['card'] && $this->card2['card'] == $this->card5['card']){
$this->twopairs = true;
$this->held = "11101";
$this->comb = 2;
}
//Pat 6: 1+3|4+5
if($this->card1['card'] == $this->card3['card'] && $this->card4['card'] == $this->card5['card']){
$this->twopairs = true;
$this->held = "10111";
$this->comb = 2;
}
//Pat 7: 1+4|2+3
if($this->card1['card'] == $this->card4['card'] && $this->card2['card'] == $this->card3['card']){
$this->twopairs = true;
$this->held = "11110";
$this->comb = 2;
}
//Pat 8: 1+4|2+5
if($this->card1['card'] == $this->card4['card'] && $this->card2['card'] == $this->card5['card']){
$this->twopairs = true;
$this->held = "11011";
$this->comb = 2;
}
//Pat 9: 1+4|3+5
if($this->card1['card'] == $this->card4['card'] && $this->card3['card'] == $this->card5['card']){
$this->twopairs = true;
$this->held = "10111";
$this->comb = 2;
}
//Pat 10: 1+5|2+3
if($this->card1['card'] == $this->card5['card'] && $this->card2['card'] == $this->card3['card']){
$this->twopairs = true;
$this->held = "11101";
$this->comb = 2;
}
//Pat 11: 1+5|2+4
if($this->card1['card'] == $this->card5['card'] && $this->card2['card'] == $this->card4['card']){
$this->twopairs = true;
$this->held = "11011";
$this->comb = 2;
}
//Pat 12: 1+5|3+4
if($this->card1['card'] == $this->card5['card'] && $this->card3['card'] == $this->card4['card']){
$this->twopairs = true;
$this->held = "10111";
$this->comb = 2;
}
//Pat 13: 2+3|4+5
if($this->card2['card'] == $this->card3['card'] && $this->card4['card'] == $this->card5['card']){
$this->twopairs = true;
$this->held = "01111";
$this->comb = 2;
}
//Pat 14: 2+4|3+5
if($this->card2['card'] == $this->card4['card'] && $this->card3['card'] == $this->card5['card']){
$this->twopairs = true;
$this->held = "01111";
$this->comb = 2;
}
//Pat 15: 2+5|3+4
if($this->card2['card'] == $this->card5['card'] && $this->card3['card'] == $this->card4['card']){
$this->twopairs = true;
$this->held = "01111";
$this->comb = 2;
}
}
function check3OfAKind(){
if($this->royalstraightflush) return;
if($this->straightflush) return;
if($this->fourofakind) return;
if($this->fullhouse) return;
if($this->flush) return;
if($this->straight) return;
//Pat 1: 1+2+3
if($this->card1['card'] == $this->card2['card'] && $this->card2['card'] == $this->card3['card']){
$this->threeofakind = true;
$this->held = "11100";
$this->comb = 3;
}
//Pat 2: 1+2+4
if($this->card1['card'] == $this->card2['card'] && $this->card2['card'] == $this->card4['card']){
$this->threeofakind = true;
$this->held = "11010";
$this->comb = 3;
}
//Pat 3: 1+2+5
if($this->card1['card'] == $this->card2['card'] && $this->card2['card'] == $this->card5['card']){
$this->threeofakind = true;
$this->held = "11001";
$this->comb = 3;
}
//Pat 4: 1+3+4
if($this->card1['card'] == $this->card3['card'] && $this->card3['card'] == $this->card4['card']){
$this->threeofakind = true;
$this->held = "10110";
$this->comb = 3;
}
//Pat 5: 1+3+5
if($this->card1['card'] == $this->card3['card'] && $this->card3['card'] == $this->card5['card']){
$this->threeofakind = true;
$this->held = "10101";
$this->comb = 3;
}
//Pat 6: 1+4+5
if($this->card1['card'] == $this->card4['card'] && $this->card4['card'] == $this->card5['card']){
$this->threeofakind = true;
$this->held = "10011";
$this->comb = 3;
}
//Pat 7: 2+3+4
if($this->card2['card'] == $this->card3['card'] && $this->card3['card'] == $this->card4['card']){
$this->threeofakind = true;
$this->held = "01110";
$this->comb = 3;
}
//Pat 8: 2+3+5
if($this->card2['card'] == $this->card3['card'] && $this->card3['card'] == $this->card5['card']){
$this->threeofakind = true;
$this->held = "01101";
$this->comb = 3;
}
//Pat 9: 2+4+5
if($this->card2['card'] == $this->card4['card'] && $this->card4['card'] == $this->card5['card']){
$this->threeofakind = true;
$this->held = "01011";
$this->comb = 3;
}
//Pat 10: 3+4+5
if($this->card3['card'] == $this->card4['card'] && $this->card4['card'] == $this->card5['card']){
$this->threeofakind = true;
$this->held = "00111";
$this->comb = 3;
}
}
function checkStr(){
if($this->royalstraightflush) return;
if($this->straightflush) return;
if($this->fourofakind) return;
if($this->fullhouse) return;
if($this->flush) return;
$num1 = $this->faceToNum($this->card1['card']);
$num2 = $this->faceToNum($this->card2['card']);
$num3 = $this->faceToNum($this->card3['card']);
$num4 = $this->faceToNum($this->card4['card']);
$num5 = $this->faceToNum($this->card5['card']);
$this->straight = $this->checkStrPattern(array($num1,$num2,$num3,$num4,$num5));
if($this->straight){
$this->held = "11111";
$this->comb = 4;
}
}
function checkFlush(){
if($this->royalstraightflush) return;
if($this->straightflush) return;
if($this->fourofakind) return;
if($this->fullhouse) return;
if($this->card1['suit'] == $this->card2['suit'] && $this->card2['suit'] == $this->card3['suit'] && $this->card3['suit'] == $this->card4['suit'] && $this->card4['suit'] == $this->card5['suit']) $this->flush = true;
if($this->flush){
$this->held = "11111";
$this->comb = 5;
}
}
function checkFH(){
if($this->royalstraightflush) return;
if($this->straightflush) return;
if($this->fourofakind) return;
//Pat1: 1+2+3|4+5
if($this->card1['card'] == $this->card2['card'] && $this->card2['card'] == $this->card3['card'] && $this->card4['card'] == $this->card5['card']) $this->fullhouse = true;
//Pat2: 1+2+4|3+5
if($this->card1['card'] == $this->card2['card'] && $this->card2['card'] == $this->card4['card'] && $this->card3['card'] == $this->card5['card']) $this->fullhouse = true;
//Pat3: 1+3+4|2+5
if($this->card1['card'] == $this->card3['card'] && $this->card3['card'] == $this->card4['card'] && $this->card2['card'] == $this->card5['card']) $this->fullhouse = true;
//Pat4: 1+4+5|3+2
if($this->card1['card'] == $this->card4['card'] && $this->card4['card'] == $this->card5['card'] && $this->card2['card'] == $this->card3['card']) $this->fullhouse = true;
//Pat5: 2+3+4|1+5
if($this->card2['card'] == $this->card3['card'] && $this->card3['card'] == $this->card4['card'] && $this->card1['card'] == $this->card5['card']) $this->fullhouse = true;
//Pat6: 2+3+5|1+4
if($this->card2['card'] == $this->card3['card'] && $this->card3['card'] == $this->card5['card'] && $this->card1['card'] == $this->card4['card']) $this->fullhouse = true;
//Pat7: 2+4+5|1+3
if($this->card2['card'] == $this->card4['card'] && $this->card4['card'] == $this->card5['card'] && $this->card1['card'] == $this->card3['card']) $this->fullhouse = true;
//Pat8: 3+4+5|1+2
if($this->card3['card'] == $this->card4['card'] && $this->card4['card'] == $this->card5['card'] && $this->card1['card'] == $this->card2['card']) $this->fullhouse = true;
//Pat9: 1+4+5|2+3
if($this->card1['card'] == $this->card4['card'] && $this->card4['card'] == $this->card5['card'] && $this->card2['card'] == $this->card3['card']) $this->fullhouse = true;
//Pat10: 1+3+5|2+4
if($this->card1['card'] == $this->card3['card'] && $this->card3['card'] == $this->card5['card'] && $this->card2['card'] == $this->card4['card']) $this->fullhouse = true;
//Pat11: 1+2+5|3+4
if($this->card1['card'] == $this->card2['card'] && $this->card2['card'] == $this->card5['card'] && $this->card3['card'] == $this->card4['card']) $this->fullhouse = true;
if($this->fullhouse){
$this->held = "11111";
$this->comb = 6;
}
}
function checkFourOfAKind(){
if($this->royalstraightflush) return;
if($this->straightflush) return;
//Pat1: 1+2+3+4
if($this->card1['card'] == $this->card2['card'] && $this->card2['card'] == $this->card3['card'] && $this->card3['card'] == $this->card4['card']) {
$this->fourofakind = true;
$this->comb = 7;
$this->held = "11110";
}
//Pat2: 1+2+3+5
if($this->card1['card'] == $this->card2['card'] && $this->card2['card'] == $this->card3['card'] && $this->card3['card'] == $this->card5['card']) {
$this->fourofakind = true;
$this->comb = 7;
$this->held = "11101";
}
//Pat3: 1+2+4+5
if($this->card1['card'] == $this->card2['card'] && $this->card2['card'] == $this->card4['card'] && $this->card4['card'] == $this->card5['card']) {
$this->fourofakind = true;
$this->comb = 7;
$this->held = "11011";
}
//Pat4: 1+3+4+5
if($this->card1['card'] == $this->card3['card'] && $this->card3['card'] == $this->card4['card'] && $this->card4['card'] == $this->card5['card']) {
$this->fourofakind = true;
$this->comb = 7;
$this->held = "10111";
}
//Pat5: 2+3+4+5
if($this->card2['card'] == $this->card3['card'] && $this->card3['card'] == $this->card4['card'] && $this->card4['card'] == $this->card5['card']) {
$this->fourofakind = true;
$this->comb = 7;
$this->held = "01111";
}
}
function checkSF(){
if($this->royalstraightflush) return;
if($this->card1['suit'] == $this->card2['suit'] && $this->card2['suit'] == $this->card3['suit'] && $this->card3['suit'] == $this->card4['suit'] && $this->card4['suit'] == $this->card5['suit']){
$num1 = $this->faceToNum($this->card1['card']);
$num2 = $this->faceToNum($this->card2['card']);
$num3 = $this->faceToNum($this->card3['card']);
$num4 = $this->faceToNum($this->card4['card']);
$num5 = $this->faceToNum($this->card5['card']);
$this->straightflush = $this->checkStrPattern(array($num1,$num2,$num3,$num4,$num5));
if($this->straightflush){
$this->held = "11111";
$this->comb = 8;
}
}
}
function checkRoyalSF(){
if($this->card1['suit'] == $this->card2['suit'] && $this->card2['suit'] == $this->card3['suit'] && $this->card3['suit'] == $this->card4['suit'] && $this->card4['suit'] == $this->card5['suit']){
$tfound = false;
$jfound = false;
$qfound = false;
$kfound = false;
$afound = false;
if($this->card1['card'] == "T" || $this->card2['card'] == "T" || $this->card3['card'] == "T" || $this->card4['card'] == "T" || $this->card5['card'] == "T") $tfound = true;
if($this->card1['card'] == "J" || $this->card2['card'] == "J" || $this->card3['card'] == "J" || $this->card4['card'] == "J" || $this->card5['card'] == "J") $jfound = true;
if($this->card1['card'] == "Q" || $this->card2['card'] == "Q" || $this->card3['card'] == "Q" || $this->card4['card'] == "Q" || $this->card5['card'] == "Q") $qfound = true;
if($this->card1['card'] == "K" || $this->card2['card'] == "K" || $this->card3['card'] == "K" || $this->card4['card'] == "K" || $this->card5['card'] == "K") $kfound = true;
if($this->card1['card'] == "A" || $this->card2['card'] == "A" || $this->card3['card'] == "A" || $this->card4['card'] == "A" || $this->card5['card'] == "A") $afound = true;
if($tfound == true && $jfound == true && $qfound == true && $kfound == true && $afound == true) {
$this->royalstraightflush = true;
$this->held = "11111";
$this->comb = 9;
}
}
}
} ?>
It calls the class deck; this one:
class Deck{
var $pointer = 0;
var $deck = array();
function Deck(){
}
function shuffleCards(){
mt_srand(microtime() * 10000000 + rand());
$cards = array();
for($l = 0; $l < 52; $l++){
$cards[] = $l;
}
shuffle($cards);
$this->deck = $cards;
}
function getFirstHand(){
$c1 = $this->deck[$this->pointer];
$this->pointer++;
$c2 = $this->deck[$this->pointer];
$this->pointer++;
$c3 = $this->deck[$this->pointer];
$this->pointer++;
$c4 = $this->deck[$this->pointer];
$this->pointer++;
$c5 = $this->deck[$this->pointer];
$this->pointer++;
return array($c1,$c2,$c3,$c4,$c5);
}
function getCard(){
if($this->pointer >= 52){
$this->shuffleCards();
$this->pointer = 0;
}
$c = $this->deck[$this->pointer];
$this->pointer++;
return $c;
}
function getCardByNum($num){
switch($num){
case 0: return array("suit" => "h", "card" => "A"); break;
case 1: return array("suit" => "h", "card" => "2"); break;
case 2: return array("suit" => "h", "card" => "3"); break;
case 3: return array("suit" => "h", "card" => "4"); break;
case 4: return array("suit" => "h", "card" => "5"); break;
case 5: return array("suit" => "h", "card" => "6"); break;
case 6: return array("suit" => "h", "card" => "7"); break;
case 7: return array("suit" => "h", "card" => "8"); break;
case 8: return array("suit" => "h", "card" => "9"); break;
case 9: return array("suit" => "h", "card" => "T"); break;
case 10: return array("suit" => "h", "card" => "J"); break;
case 11: return array("suit" => "h", "card" => "Q"); break;
case 12: return array("suit" => "h", "card" => "K"); break;
case 13: return array("suit" => "d", "card" => "A"); break;
case 14: return array("suit" => "d", "card" => "2"); break;
case 15: return array("suit" => "d", "card" => "3"); break;
case 16: return array("suit" => "d", "card" => "4"); break;
case 17: return array("suit" => "d", "card" => "5"); break;
case 18: return array("suit" => "d", "card" => "6"); break;
case 19: return array("suit" => "d", "card" => "7"); break;
case 20: return array("suit" => "d", "card" => "8"); break;
case 21: return array("suit" => "d", "card" => "9"); break;
case 22: return array("suit" => "d", "card" => "T"); break;
case 23: return array("suit" => "d", "card" => "J"); break;
case 24: return array("suit" => "d", "card" => "Q"); break;
case 25: return array("suit" => "d", "card" => "K"); break;
case 26: return array("suit" => "c", "card" => "A"); break;
case 27: return array("suit" => "c", "card" => "2"); break;
case 28: return array("suit" => "c", "card" => "3"); break;
case 29: return array("suit" => "c", "card" => "4"); break;
case 30: return array("suit" => "c", "card" => "5"); break;
case 31: return array("suit" => "c", "card" => "6"); break;
case 32: return array("suit" => "c", "card" => "7"); break;
case 33: return array("suit" => "c", "card" => "8"); break;
case 34: return array("suit" => "c", "card" => "9"); break;
case 35: return array("suit" => "c", "card" => "T"); break;
case 36: return array("suit" => "c", "card" => "J"); break;
case 37: return array("suit" => "c", "card" => "Q"); break;
case 38: return array("suit" => "c", "card" => "K"); break;
case 39: return array("suit" => "s", "card" => "A"); break;
case 40: return array("suit" => "s", "card" => "2"); break;
case 41: return array("suit" => "s", "card" => "3"); break;
case 42: return array("suit" => "s", "card" => "4"); break;
case 43: return array("suit" => "s", "card" => "5"); break;
case 44: return array("suit" => "s", "card" => "6"); break;
case 45: return array("suit" => "s", "card" => "7"); break;
case 46: return array("suit" => "s", "card" => "8"); break;
case 47: return array("suit" => "s", "card" => "9"); break;
case 48: return array("suit" => "s", "card" => "T"); break;
case 49: return array("suit" => "s", "card" => "J"); break;
case 50: return array("suit" => "s", "card" => "Q"); break;
case 51: return array("suit" => "s", "card" => "K"); break;
}
}
} ?>