[eluser]Sally D[/eluser]
I was board today so I wrote this code to try to make a poker card game I use this library to check and array of cards array('c1','h13','s1','d1') would produce a three_of a kind if that array was passed to it's constructor
I use this library along with another library that controls user events along with a controller to feed all this to a view file
it's not perfected yet but I can see all the pieces and I know I can make them fit
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/* welcome to my card algorithm
* This program evaulates a 5 card poker hand
* c=clubs, d=diamonds, h=hearts, s=spades, 1=ace
*
*/
// my poker hand
class Eval_hand {
var $hand;
var $suits;
var $ranks;
var $counted_up_ranks;
// x is the poker hand which is sorted
function Eval_hand($x){
$CI =& get_instance();
if(count($x) == 5) {$this->hand=$x;}else{$this->hand=null;}
$this->suits=array('c'=>array(), 'd'=>array(), 'h'=>array(), 's'=>array());
$this->ranks=array();
foreach($this->hand as $key=>$value) {
if(strlen($value) == 3) {
$suit=substr($value, -3,1);
$rank=(int)substr($value, -2);
$this->ranks[] = $rank;
$this->suits[$suit][] = $rank;
} else if(strlen($value) == 2){
$suit=substr($value, -2, 1) ;
$rank=substr($value,-1);
$this->ranks[] = $rank;
$this->suits[$suit][]=$rank;
}
}
$this->counted_up_ranks=array_count_values($this->ranks);
}
function test(){
global $CI;
print_r($this->hand);
echo "<br>";
print_r($this->counted_up_ranks);
}
function royal_flush()
{
if(in_array('1', $this->ranks, true))
{
$aces_hi=$this->ranks;
sort($aces_hi);
$aces_hi[0] = 14;
sort($aces_hi);
if( ($aces_hi[0]+1) == ($aces_hi[1])
&& ($aces_hi[1]+1) == ($aces_hi[2])
&& ($aces_hi[2]+1) == ($aces_hi[3])
&& ($aces_hi[3]+1) == ($aces_hi[4]))
{
if( (count($this->suits['h'])==5)
|| (count($this->suits['c']) == 5)
|| (count($this->suits['s']) == 5)
|| (count($this->suits['d']) == 5))
{
return 1;
}
}
}
else
{
return 0;
}
}
function straight_flush(){
// code to test for a straight flush
if( ($this->ranks[0]+1) == ($this->ranks[1])
&& ($this->ranks[1]+1) == ($this->ranks[2])
&& ($this->ranks[2]+1) == ($this->ranks[3])
&& ($this->ranks[3]+1) == ($this->ranks[4]))
{
if( (count($this->suits['h'])==5) || (count($this->suits['c']) == 5)
|| (count($this->suits['s']) == 5)
|| (count($this->suits['d']) == 5))
{
return 1;
}
}
else
{
return 0;
}
}
function four_of_a_kind(){
if( (count($this->counted_up_ranks) == 2)
&& (in_array('4',$this->counted_up_ranks)))
{
return 1;
}
else
{
return 0;
}
}
function full_house(){
// code to test for a full house
if((count($this->counted_up_ranks) == 2)
&& (in_array('3',$this->counted_up_ranks))
&& (in_array('2',$this->counted_up_ranks)))
{
return 1;
}
else
{
return 0;
}
}
function flush(){
// code to test for a flush
if( (count($this->suits['h'])==5)
|| (count($this->suits['c']) == 5)
|| (count($this->suits['s']) == 5)
|| (count($this->suits['d']) == 5))
{
return 1;
}
else
{
return 0;
}
}
function straight() {
if( ($this->ranks[0]+1) == ($this->ranks[1])
&& ($this->ranks[1]+1) == ($this->ranks[2])
&& ($this->ranks[2]+1) == ($this->ranks[3])
&& ($this->ranks[3]+1) == ($this->ranks[4]))
{
// return 5 for just a straight
return 1;
}
else
{
return 0;
}
}
function three_of_a_kind() {
// code to
if( (count($this->counted_up_ranks) == 3)
&& (in_array('3',$this->counted_up_ranks)))
{
return 1;
}
else
{
return 0;
}
}
function two_pair(){
if( (count($this->counted_up_ranks) == 3)
&& (in_array('2',$this->counted_up_ranks)))
{
return 1;
}
else
{
return 0;
}
}
function pair(){
if( (count($this->counted_up_ranks) == 4)
&& (in_array('2',$this->counted_up_ranks)))
{
return 1;
}
else
{
return 0;
}
}
function high_card(){
return "not yet implemented";
}
}
?>