Home > Article > Backend Development > Zhajinhua Game PHP Implementation Code Size Competition
Zha Jinhua Game PHP Implementation Code Size Competition
Programs are inseparable from algorithms. In the previous blog, we have actually discussed the pathfinding algorithm. However, in the example diagram at that time, the optional path was the only one. When we choose an algorithm, we mean to choose the only path. How to choose it?
I still remember when I was in junior high school, I would often hide on the roadside after school in the afternoon and make gold flowers to gamble* money. It seemed that I was addicted to it. Now during the Chinese New Year, we often make gold flowers and gamble* money together, but my luck is not good. We lose every time.
The sun is shining brightly today. I just went out to play during the Qingming Festival, so I didn’t go anywhere today. When I had nothing to do, I thought about how to use a program to compare the sizes of two cards in Golden Flower. Now that I have implemented it, some methods are quite important, so I wrote them down.
Okay, no more nonsense.
I won’t go into the rules for comparing the two decks of cards with Golden Flower. Just indicate when it’s a straight: JQK < A23 < QKA
Idea: Golden Flower (http://www.a8u.net/ )
1" Randomly generate two decks of cards, the structure of each deck is
[php] view plaincopyprint?
array( array('Spade','K'), array('Club','6'), array('Spade','J'), )2” Calculate the score of each deck of cards: each deck of cards has an original size (ie excluding pairs, straights, golden flowers, straight golds, and bobbins), and then each card’s The score is a 2-digit number, with less than 2 digits padded with leading 0s, such as 'A': 14, '10': 10, '2': '02', 'k': 13, '7': 07 Sort the 3 cards according to the number of points (from large to small), and form a 6-digit number. For example, 'A27': 140702, '829': 090802, 'JK8': 131108, '2A10': 141002 Exception, For pairs, put the number of pairs in the first two digits (you will see why this is done later). For example, '779': 070709, '7A7': 070714, 'A33': 030314 The current score is A 6-digit number, set the pair to an original value plus 10*100000, now a 7-digit number. For example, '779': 1070709, '7A7': 1070714, 'A33': 1030314 For smooth For example, add 20*100000 to the result. For example, '345': 2050403, 'QKA': 2141312, '23A': 2140302. For Jinhua, add 30*100000 to the result. For example, 'Spade K, Spade 6. ,Spade J': 3131106 Because the straight gold is actually the sum of the golden flower and the straight child, so the straight gold should be 50*10000. For example, 'Spade 7, Spade 6, Spade 8': 5080706
The code is as follows (PHP)
[php] view plaincopyprint?
<?php class PlayCards { public $suits = array('Spade', 'Heart', 'Diamond', 'Club'); public $figures = array('2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'); public $cards = array(); public function __construct() { $cards = array(); foreach($this->suits as $suit){ foreach($this->figures as $figure){ $cards[] = array($suit,$figure); } } $this->cards = $cards; } public function getCard() { shuffle($this->cards); //生成3张牌 return array(array_pop($this->cards), array_pop($this->cards), array_pop($this->cards)); } public function compareCards($card1,$card2) { $score1 = $this->ownScore($card1); $score2 = $this->ownScore($card2); if($score1 > $score2) return 1; elseif($score1 < $score2) return -1; return 0; } private function ownScore($card) { $suit = $figure = array(); foreach($card as $v){ $suit[] = $v[0]; $figure[] = array_search($v[1],$this->figures)+2; } //补齐前导0 for($i = 0; $i < 3; $i++){ $figure[$i] = str_pad($figure[$i],2,'0',STR_PAD_LEFT); } rsort($figure); //对于对子做特殊处理 if($figure[1] == $figure[2]){ $temp = $figure[0]; $figure[0] = $figure[2]; $figure[2] = $temp; } $score = $figure[0].$figure[1].$figure[2]; //筒子 60*100000 if($figure[0] == $figure[1] && $figure[0] == $figure[2]){ $score += 60*100000; } //金花 30*100000 if($suit[0] == $suit[1] && $suit[0] == $suit[2]){ $score += 30*100000; } //顺子 20*100000 if($figure[0] == $figure[1]+1 && $figure[1] == $figure[2]+1 || implode($figure) =='140302'){ $score += 20*100000; } //对子 10*100000 if($figure[0] == $figure[1] && $figure[1] != $figure[2]){ $score += 10*100000; } return $score; } } //test $playCard = new PlayCards(); $card1 = $playCard->getCard(); $card2 = $playCard->getCard(); $result = $playCard->compareCards($card1,$card2); echo 'card1 is ',printCard($card1),'<br/>'; echo 'card2 is ',printCard($card2),'<br/>'; $str = 'card1 equit card2'; if($result == 1) $str = 'card1 is larger than card2'; elseif($result == -1) $str = 'card1 is smaller than card2'; echo $str; function printCard($card) { $str = '('; foreach($card as $v){ $str .= $v[0].$v[1].','; } return trim($str,',').')'; }
以上就介绍了扎金花游戏 PHP 实现代码之大小比赛,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。