程序主入口
include "Table.class.php";
//显示函数
function pre($str){
echo ''
;
print_r($str);
echo '
';
}
//创建扑克牌
$c=new Cards();
//初始化玩家数量
$table=new Table(2,$c->Cards);
扑克牌类 Cards.class.php
class Cards{
public $Cards=array();
function __construct()
{
$this->Cards=$this->createCards();
shuffle($this->Cards);
}
private function createCards()
{
$cards=array();
$suit=array("H","S","D","C");
$p=array("T","J","Q","K","A");
for($i=0;$i$suit);$i++){
for($j=2;$j<=9;$j++){
$cards[]=$suit[$i].$j;
}
for($k=0;$k$p);$k++){
$cards[]=$suit[$i].$p[$k];
}
}
return $cards;
}
}
玩家类 Player.class.php
class Player{
public $score=0;
public $CardInHand=array();
function __construct($cards)
{
$this->getCardInHand($cards);
}
private function getCardInHand($cards){
$randkey=array_rand($cards,2);
$this->CardInHand[]=$cards[$randkey[0]];
$this->CardInHand[]=$cards[$randkey[1]];
return $this->CardInHand;
}
}
牌桌类(核心算法类)Table.class.php
include "Cards.class.php";
include "Player.class.php";
class Table{
public $players=array();
public $tablecards=array();
function __construct($num,$cards)
{
for($i=0;$i<$num;$i++){
$this->players[$i]=new Player($cards);
$cards=array_diff($cards,$this->players[$i]->CardInHand);
//echo count($cards).'
';
$k=$i+1;
echo "玩家".$k."的手牌为
";
$this->showCard($this->players[$i]->CardInHand);
echo "
==========
";
}
array_shift($cards);
for($i=0;$i<3;$i++) {
$this->tablecards[] = $cards[$i];
}
$cards=array_diff($cards,$this->tablecards);
for($i=0;$i<2;$i++){
array_shift($cards);
$this->tablecards[] = $cards[0];
}
echo "
====公牌为====
";
$this->showCard($this->tablecards);
for($i=0;$i$this->players);$i++){
$k=$i+1;
echo "
----玩家{$k}的结果为----
";
$this->result($this->players[$i]);
echo "
-----------------------
";
}
}
private function showCard($cards){
foreach ($cards as $ca) {
$path.= "
";
}
echo $path;
}
private function pre($str,$comment){
echo "=={$comment}===
"
;
print_r($str);
echo '
====ps:以上算是德州扑克的算法程序的90%内容,还有10%的内容是同花顺的判断,以及玩家分数的计算和胜负的判断,但是这些内容已经很简单,已经没有算法上的挑战性,因此《用PHP实现一个关于德州扑克算法的程序》就告一段落了。