search
HomeBackend DevelopmentPHP TutorialZhajinhua 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

Idea: Golden Flower (http://www.a8u.net/ )

1" Randomly generate two decks of cards, the structure of each deck is

[php] view plaincopyprint?

  1. array(
  2. ), array( 'Club',
  3. '6'), ), )
  4. 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
  5. For the bobbin, it will be Add 60*100000 to the result. For example, '666': 6060606, 'JJJ': 61111113" Compare the size of the two cards (use the calculated score to compare)
It's that simple! !

The code is as follows (PHP)

[php] view plaincopyprint?

  1. class PlayCards  
  2. {  
  3.     public $suits = array('Spade''Heart''Diamond''Club');  
  4.     public $figures = array('2''3''4''5''6''7''8''9''10''J''Q''K''A');  
  5.     public $cards = array();  
  6.     public function __construct()  
  7.     {  
  8.         $cards = array();  
  9.         foreach($this->suits as $suit){  
  10.             foreach($this->figures as $figure){  
  11.                 $cards[] = array($suit,$figure);  
  12.             }  
  13.         }  
  14.         $this->cards = $cards;  
  15.     }  
  16.     public function getCard()  
  17.     {  
  18.         shuffle($this->cards);  
  19.         //生成3张牌  
  20.         return array(array_pop($this->cards), array_pop($this->cards), array_pop($this->cards));     
  21.   
  22.     }  
  23.     public function compareCards($card1,$card2)  
  24.     {  
  25.         $score1 = $this->ownScore($card1);  
  26.         $score2 = $this->ownScore($card2);  
  27.         if($score1 > $score2return 1;  
  28.         elseif($score1 $score2return -1;  
  29.         return 0;         
  30.     }  
  31.       
  32.       
  33.     private function ownScore($card)  
  34.     {  
  35.         $suit = $figure = array();  
  36.         foreach($card as $v){  
  37.             $suit[] = $v[0];  
  38.             $figure[] = array_search($v[1],$this->figures)+2;  
  39.         }  
  40.         //补齐前导0  
  41.         for($i = 0; $i $i++){  
  42.             $figure[$i] = str_pad($figure[$i],2,'0',STR_PAD_LEFT);  
  43.         }  
  44.         rsort($figure);  
  45.         //对于对子做特殊处理  
  46.         if($figure[1] == $figure[2]){  
  47.             $temp = $figure[0];  
  48.             $figure[0] = $figure[2];  
  49.             $figure[2] = $temp;  
  50.         }
  51.                              $score                                                                                                                                          ​​​​​//Bobbin 60 *100000 [0] == $figure
  52. [2]){
  53.                                                                                       // Golden Flower 30*100000                                                                                                          
  54. (
  55. $suit[0] == $suit[1] && $suit[0] == $suit[2]){                                                                                                                                                
  56.                                                            ] == $figure
  57. [1]+1 &&
  58. $figure
  59. [1] ==
  60. $figure[2]+1 || implode($figure
  61. ) ==
  62. '140302'){                                                                                                                                                                                                                                 if($figure[0] == $figure
  63. [1] &&
  64. $figure[1] != $figure
  65. [2]){
  66.                                                                                                             
  67.   
  68. //test  
  69. $playCard = new PlayCards();  
  70. $card1 = $playCard->getCard();  
  71. $card2 = $playCard->getCard();  
  72. $result = $playCard->compareCards($card1,$card2);  
  73.   
  74. echo 'card1 is ',printCard($card1),'
    '
    ;  
  75. echo 'card2 is ',printCard($card2),'
    '
    ;  
  76. $str = 'card1 equit card2';  
  77. if($result == 1) $str =  'card1 is larger than card2';  
  78. elseif($result == -1) $str = 'card1 is smaller than card2';  
  79. echo $str;  
  80.   
  81.   
  82. function printCard($card)  
  83. {  
  84.     $str = '(';  
  85.     foreach($card as $v){  
  86.         $str .= $v[0].$v[1].',';  
  87.     }  
  88.     return trim($str,',').')';  
  89. }  
<?php class PlayCards
{
	public $suits = array(&#39;Spade&#39;, &#39;Heart&#39;, &#39;Diamond&#39;, &#39;Club&#39;);
	public $figures = array(&#39;2&#39;, &#39;3&#39;, &#39;4&#39;, &#39;5&#39;, &#39;6&#39;, &#39;7&#39;, &#39;8&#39;, &#39;9&#39;, &#39;10&#39;, &#39;J&#39;, &#39;Q&#39;, &#39;K&#39;, &#39;A&#39;);
	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 figures)+2;
		}
		//补齐前导0
		for($i = 0; $i 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教程有兴趣的朋友有所帮助。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How can you check if a PHP session has already started?How can you check if a PHP session has already started?Apr 30, 2025 am 12:20 AM

In PHP, you can use session_status() or session_id() to check whether the session has started. 1) Use the session_status() function. If PHP_SESSION_ACTIVE is returned, the session has been started. 2) Use the session_id() function, if a non-empty string is returned, the session has been started. Both methods can effectively check the session state, and choosing which method to use depends on the PHP version and personal preferences.

Describe a scenario where using sessions is essential in a web application.Describe a scenario where using sessions is essential in a web application.Apr 30, 2025 am 12:16 AM

Sessionsarevitalinwebapplications,especiallyfore-commerceplatforms.Theymaintainuserdataacrossrequests,crucialforshoppingcarts,authentication,andpersonalization.InFlask,sessionscanbeimplementedusingsimplecodetomanageuserloginsanddatapersistence.

How can you manage concurrent session access in PHP?How can you manage concurrent session access in PHP?Apr 30, 2025 am 12:11 AM

Managing concurrent session access in PHP can be done by the following methods: 1. Use the database to store session data, 2. Use Redis or Memcached, 3. Implement a session locking strategy. These methods help ensure data consistency and improve concurrency performance.

What are the limitations of using PHP sessions?What are the limitations of using PHP sessions?Apr 30, 2025 am 12:04 AM

PHPsessionshaveseverallimitations:1)Storageconstraintscanleadtoperformanceissues;2)Securityvulnerabilitieslikesessionfixationattacksexist;3)Scalabilityischallengingduetoserver-specificstorage;4)Sessionexpirationmanagementcanbeproblematic;5)Datapersis

Explain how load balancing affects session management and how to address it.Explain how load balancing affects session management and how to address it.Apr 29, 2025 am 12:42 AM

Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Explain the concept of session locking.Explain the concept of session locking.Apr 29, 2025 am 12:39 AM

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Are there any alternatives to PHP sessions?Are there any alternatives to PHP sessions?Apr 29, 2025 am 12:36 AM

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Define the term 'session hijacking' in the context of PHP.Define the term 'session hijacking' in the context of PHP.Apr 29, 2025 am 12:33 AM

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor