PHP: how to implement the big and small competitions of zaginhua games

Source: Internet
Author: User
This article mainly introduces the PHP method for implementing the large and small competitions of Zhabei game. The example analyzes the implementation principle and related algorithm skills of Zhabei game, which has some reference value, for more information about PHP, see the following example. Share it with you for your reference. The specific analysis is as follows:

Programs cannot do without Algorithms. we have discussed pathfinding algorithms before. However, in the example at that time, the optional path is unique. We select an algorithm. that is to say, we need to select this unique path. how can we choose it?

I still remember that when I was in junior high school, I often spent money by hiding on the roadside when I was out of school in the afternoon. * It seems that I am still addicted. now I often bet on money together during the Chinese New Year, but I am not lucky, every time I lose.

Today, the sun is shining. I went out for a visit during the Tomb Sweeping Day, so I didn't go anywhere today. I thought about how to use a program to compare the sizes of the two cards in Jinhua. now I have implemented it. some methods are quite important, so I will write it down.

Okay, no nonsense.

The comparison rules of the two deck cards of zinjinhua will not be mentioned, indicating that JQK <A23 <QKA

Idea: zaginhua

1. two cards are randomly generated, with each deck structured

The code is as follows:

Array (
Array ('spad', 'k '),
Array ('Club ', '6 '),
Array ('spade ', 'J '),
)


The code is as follows:

Array (
Array ('spad', 'k '),
Array ('Club ', '6 '),
Array ('spade ', 'J '),
)

2. calculate the score of each sub-card: each sub-card has an original size (that is, the size of sub-card, sub-card, Jin Hua, Shun Jin, and sub-card), and then

Each card has A two-digit score with less than two leading zeros, for example, 'A': 14, '10': 10, '2': '02 ', 'K': 13, '7': 07

Sort the three cards by the number of points (from big to small) to a six-digit number. For example, 'a27': 140702, '2018010': 829, 'jk8': 090802, '2a10': 131108

With the exception, the number of digits of the child must be placed in the first two digits (the reason for this is later ). For example, '200': 779, '7a7 ': 070709, 'a33': 070714

The current score is a 6-digit number. The child is set to an original value plus a value of 10*100000. now it is a 7-digit number. For example, '200': 779, '7a7 ': 1070709, 'a33': 1070714

Add 20*100000 to the result .. For example, '200': 345, 'qka ': 2050403, '23a': 2141312

For Jinhua, add 30*100000 to the result. For example, 'spade K, Spade 6, Spade j': 3131106

Because Shun Jin is actually the sum of Jin Hua and Heshun Zi, So Shun Jin should be 50*10000. For example, 'spade 7, Spade 6, Spade 8': 5080706

Add 60*100000 to the result of the package. For example, '200': 666, 'jjj ': 6060606

3. compare the two cards (use the calculated score to compare them)

That's easy !!

The code is as follows (PHP)

The code is as follows:

<? 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 );
// Generate three cards
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;
}
// Complete the leading 0
For ($ I = 0; $ I <3; $ I ++ ){
$ Figure [$ I] = str_pad ($ figure [$ I], 2, '0', STR_PAD_LEFT );
}
Rsort ($ figure );
// Perform special processing on the child
If ($ figure [1] ==$ figure [2]) {
$ Temp = $ figure [0];
$ Figure [0] = $ figure [2];
$ Figure [2] = $ temp;
}
$ Score = $ figure [0]. $ figure [1]. $ figure [2];
// 60x100000
If ($ figure [0] === figure [1] & $ figure [0] ===$ figure [2]) {
$ Score + = 60*100000;
}
// Calendula 30*100000
If ($ suit [0] ==$ suit [1] & $ suit [0] ===$ suit [2]) {
$ Score + = 30*100000;
}
// Shunzi 20x100000
If ($ figure [0] = $ figure [1] + 1 & $ figure [1] = $ figure [2] + 1 | implode ($ figure) = '000000 '){
$ Score + = 20*100000;
}
// 10*100000 pairs
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 ),'
';
Echo 'card2 is ', printCard ($ card2 ),'
';
$ 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 ,',').')';
}


The code is as follows:

<? 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 );
// Generate three cards
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;
}
// Complete the leading 0
For ($ I = 0; $ I <3; $ I ++ ){
$ Figure [$ I] = str_pad ($ figure [$ I], 2, '0', STR_PAD_LEFT );
}
Rsort ($ figure );
// Perform special processing on the child
If ($ figure [1] ==$ figure [2]) {
$ Temp = $ figure [0];
$ Figure [0] = $ figure [2];
$ Figure [2] = $ temp;
}
$ Score = $ figure [0]. $ figure [1]. $ figure [2];
// 60x100000
If ($ figure [0] === figure [1] & $ figure [0] ===$ figure [2]) {
$ Score + = 60*100000;
}
// Calendula 30*100000
If ($ suit [0] ==$ suit [1] & $ suit [0] ===$ suit [2]) {
$ Score + = 30*100000;
}
// Shunzi 20x100000
If ($ figure [0] = $ figure [1] + 1 & $ figure [1] = $ figure [2] + 1 | implode ($ figure) = '000000 '){
$ Score + = 20*100000;
}
// 10*100000 pairs
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 ),'
';
Echo 'card2 is ', printCard ($ card2 ),'
';
$ 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 ,',').')';
}

I hope this article will help you with php programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.