Scripts

THPokerCheck

Ook maar eens een bijdrage van mij. Dit is een class die een array van 7 kaarten doorloopt (2 hand en 5 op tafel) en weergeeft wat je hand inhoud. Dit was mijn eerste versie, maar heb ik alweer afgedankt omdat ik een nieuwe heb geschreven die ook rekening houd met de volledige waarde en de bijkaarten. Ik hoop hiermee iemand misschien opweg te helpen. Het werkt alsvolgt: maak een array met kaarten waarbij de key het type is en de value de waarde instantieer het object en gooi de kaarten erin en een print_r voor het resultaat:

thpokercheck
THPokerCheck.php
[code]
<?
/*
*	Poker Engine Card Evaluator v0.1a
*	Produced by: Michel Ypma
*/

class THPokerCheck {
	
	private $cards = array();
	
	private $types = array('d','h','s','c');
	
	private $foundTypes = array();
	
	private $countCards = array();
	
	public $rules = array(
		'Royal Flush' => false,
		'Straight Flush' => false,
		'Four of a kind' => false,
		'Full House' => false,
		'Flush' => false,
		'Straight' => false,
		'Three of a kind' => false,
		'Two pair' => false,
		'One pair' => false,
		'High card' => false
	);
	
	public function evaluate($cards) {
		$this->cards = $cards;
		$this->countCards = $this->countAll();
		$this->start();
	}
	
	private function start() {
		$this->kinds();
		$this->isStraight();
		$this->isRoyalFlush();
	}
	
	private function isRoyalFlush() {
		foreach($this->foundTypes as $a) {
			$cache = $this->countCards[$a];
			$start = current($cache);
			if ($start != 14) return false;
			if ($this->isStraightFlush(1)) {
				$this->rules['Royal Flush'] = true;
			}
		}
	}
	
	private function isStraightFlush($bool = false) {
		foreach($this->foundTypes as $a) {
			$cache = $this->countCards[$a];
			$start = current($cache);
			$total = count($cache);
			$retries = $total - 5;
			$counter = 1;
			if ($this->isFlush()) {
				for ($i=0; $i < 4; $i++) {
					next($cache);
					$start--;
					$counter++;
					if (current($cache) != $start) {
						if ($retries > 0 && $bool == false) {
							$retries--;
							$i--;
							$counter = 1;
						} else {
							return false;
						}
					}
					// edit aan de hand van de ontdekking van cervetti
					if ($counter == 5) {
						$this->rules['Straight Flush'] = true;
						return true;
					}
				}
			} else {
				return false;
			}
			return false;
		}
	}
	
	private function isStraight() {
		foreach($this->countCards as &$value) {
			foreach($value as $nKey => &$nValue) {
				$cache[] = $nValue;
			}
		}
		arsort($cache);
		
		$total = count($cache);
		$retries = $total - 5;
		$counter = $start = current($cache);
		
		foreach($cache as $a => $b) {
			if ($cache[$a] != $counter) {
				if ($retries > 0) {
					$retries--;
					$counter = $start = $cache[$a];
				} else {
					return false;
				}
			}
			if (($start - 5) == $counter ) {
				break;
			}
			$counter--;
		}
		$this->rules['Straight'] = true;
		return true;
	}
	
	private function isFlush() {
		foreach($this->foundTypes as $a) {
			$cache = $this->countCards[$a];
			$total = count($cache);
			if ($total >= 5) {
				$this->rules['Flush'] = true;
				return true;
			}
		}
		return false;
		
	}
	
	private function kinds() {
		foreach($this->countCards as $value) {
			foreach($value as $nKey => $nValue) {
				$cache[] = $nValue;
			}
		}
		$cache = array_count_values($cache);
		foreach($cache as $key => $value) {
			$pair += ($cache[$key] == 2) ? 1 : 0 ;
			$threekind += ($cache[$key] >= 3) ? 1 : 0 ;
			$fourkind += ($cache[$key] >= 4) ? 1 : 0 ;
		}
		$this->rules['One pair'] = ($pair == 1) ? true : false;
		$this->rules['Two pair'] = ($pair == 2) ? true : false;
		$this->rules['Three of a kind'] = ($threekind == 1) ? true : false;
		$this->rules['Four of a kind'] = ($fourkind == 1) ? true : false;
		$this->rules['Full House'] = ($threekind == 1 && $pair == 1) ? true : false;
		return true;
	}
	
	private function countAll() {
		foreach($this->cards as $knum => $hand) {
			foreach($hand as $type => $value) {
				switch($type) {
					case 'd':
					$this->countCards['d'][] = $value;
					break;
					case 'h':
					$this->countCards['h'][] = $value;
					break;
					case 's':
					$this->countCards['s'][] = $value;
					break;
					case 'c':
					$this->countCards['c'][] = $value;
					break;
				}
				$this->foundTypes[] = $type;
			}
		}
		$this->foundTypes = array_unique($this->foundTypes);
		
		for($i = 0; $i<=count($this->foundTypes);$i++) {
			($this->countCards[$this->foundTypes[$i]]) ? arsort($this->countCards[$this->foundTypes[$i]]) : '';
		}
		
		return $this->countCards;
	}
	
	private function getHighestCards($num = 1) {
		$cache = $this->getValues();
		arsort($cache);
		$cache = array_chunk($cache,5);
	}
	
	private function getValues() {
	$i = 0;
		foreach($this->countCards as $value) {
			foreach($value as $nKey => $nValue) {
				$cache[$i]['value'] = $nValue;
				$cache[$i]['type'] = $nKey;
				$i++;
			}
		}
		return $cache;
	}
	
}
?>
[/code]

Reacties

0
Nog geen reacties.