This article is an example of how PHP realizes the game's size. Share to everyone for your reference. The specific analysis is as follows:
Program is inseparable from the algorithm, discussed in the previous search algorithm. However, in the example diagram then, the optional path is unique. We pick an algorithm, which means to choose the only path, how to choose?
Also remember when the middle school often in the afternoon to hide in the roadside Jinhua bar to gamble * Money, seemingly also addicted to, now the Chinese New Year often together with gold betting * money, but luck is not good, every time is to lose AH.
Today, the sun is bright, because of the Qingming Festival to go out to play, so today did not go where. Idle just think how to use the program to achieve the size of the two cards in Golden Flower, now it is realized, some methods are quite important, so write down.
Okay, no more nonsense.
Jinhua Bar The comparison rules of two pairs of cards do not say, note that is the time of the son: Jqk < A23 < QKA
Train of thought: Jinhua Bar
1. Randomly generated two cards, each card structure is
Copy Code code as follows:
Array
Array (' spade ', ' K '),
Array (' Club ', ' 6 '),
Array (' spade ', ' J '),
)
Copy Code code as follows:
Array
Array (' spade ', ' K '),
Array (' Club ', ' 6 '),
Array (' spade ', ' J '),
)
2. Calculate the score of each deck: each deck has a raw size (that is, excluding pairs, Shun Zi, golden, shun gold, the size of the package), and then
The score for each card is a 2-digit, less than 2-digit complement leading 0, such as ' A ': ', ', ' Ten ': ' 2 ': ' ', ' K ': 13, ' 7 ': 07
Sort 3 cards by number of points (from large to small) and make up a 6-digit size. For example ' A27 ': 140702, ' 829 ': 090802, ' JK8 ': 131108, ' 2a10 ': 141002
The exception to this is the number of digits to be placed in the top two for the pair (see why). For example ' 779 ': 070709, ' 7a7 ': 070714, ' A33 ': 030314
Now the score is a 6-digit number, set the pair to a raw value plus the value of 10*100000, now a 7-digit number. For example ' 779 ': 1070709, ' 7a7 ': 1070714, ' A33 ': 1030314
For Junko, add the result to the 20*100000. For example ' 345 ': 2050403, ' QKA ': 2141312, ' 23A ': 2140302
For the golden flower, the result is added 30*100000. For example ' spade k,spade 6,spade J ': 3131106
Because Shun Jin when in fact is the golden Flower and the son's and, so shun gold should be 50*10000. For example ' Spade 7,spade 6,spade 8 ': 5080706
For the cheese, add the result to the 60*100000. For example ' 666 ': 6060606, ' jjj ': 6111111
3. Compare the size of the two tiles (compared with the calculated points)
It's so simple!!
The code is as follows (PHP)
Copy Code code as follows:
<?php
Class Playcards
{
Public $suits = Array (' Spade ', ' Heart ', ' Diamond ', ' Club ');
Public $figures = Array (' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' Ten ', ' 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 3 Cards
Return Array (Array_pop ($this->cards), Array_pop ($this->cards), Array_pop ($this->cards));
}
Public Function Comparecards ($card 1, $card 2)
{
$score 1 = $this->ownscore ($card 1);
$score 2 = $this->ownscore ($card 2);
if ($score 1 > $score 2) return 1;
ElseIf ($score 1 < $score 2) 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;
}
Padded leading 0
for ($i = 0; $i < 3; $i + +) {
$figure [$i] = Str_pad ($figure [$i],2, ' 0 ', str_pad_left);
}
Rsort ($figure);
Special handling for pairs
if ($figure [1] = = $figure [2]) {
$temp = $figure [0];
$figure [0] = $figure [2];
$figure [2] = $temp;
}
$score = $figure [0]. $figure [1]. $figure [2];
Cheese 60*100000
if ($figure [0] = = $figure [1] && $figure [0] = = $figure [2]) {
$score + + 60*100000;
}
Golden Flower 30*100000
if ($suit [0] = = $suit [1] && $suit [0] = = $suit [2]) {
$score + + 30*100000;
}
Shun Zi 20*100000
if ($figure [0] = = $figure [1]+1 && $figure [1] = = $figure [2]+1 | | implode ($figure) = = ' 140302 ') {
$score + + 20*100000;
}
Pair 10*100000
if ($figure [0] = = $figure [1] && $figure [1]!= $figure [2]) {
$score + + 10*100000;
}
return $score;
}
}
Test
$playCard = new Playcards ();
$card 1 = $playCard->getcard ();
$card 2 = $playCard->getcard ();
$result = $playCard->comparecards ($card 1, $card 2);
Echo ' Card1 is ', Printcard ($card 1), ' <br/> ';
Echo ' Card2 is ', Printcard ($card 2), ' <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, ', '). ') ';
}
Copy Code code as follows:
<?php
Class Playcards
{
Public $suits = Array (' Spade ', ' Heart ', ' Diamond ', ' Club ');
Public $figures = Array (' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' Ten ', ' 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 3 Cards
Return Array (Array_pop ($this->cards), Array_pop ($this->cards), Array_pop ($this->cards));
}
Public Function Comparecards ($card 1, $card 2)
{
$score 1 = $this->ownscore ($card 1);
$score 2 = $this->ownscore ($card 2);
if ($score 1 > $score 2) return 1;
ElseIf ($score 1 < $score 2) 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;
}
Padded leading 0
for ($i = 0; $i < 3; $i + +) {
$figure [$i] = Str_pad ($figure [$i],2, ' 0 ', str_pad_left);
}
Rsort ($figure);
Special handling for pairs
if ($figure [1] = = $figure [2]) {
$temp = $figure [0];
$figure [0] = $figure [2];
$figure [2] = $temp;
}
$score = $figure [0]. $figure [1]. $figure [2];
Cheese 60*100000
if ($figure [0] = = $figure [1] && $figure [0] = = $figure [2]) {
$score + + 60*100000;
}
Golden Flower 30*100000
if ($suit [0] = = $suit [1] && $suit [0] = = $suit [2]) {
$score + + 30*100000;
}
Shun Zi 20*100000
if ($figure [0] = = $figure [1]+1 && $figure [1] = = $figure [2]+1 | | implode ($figure) = = ' 140302 ') {
$score + + 20*100000;
}
Pair 10*100000
if ($figure [0] = = $figure [1] && $figure [1]!= $figure [2]) {
$score + + 10*100000;
}
return $score;
}
}
Test
$playCard = new Playcards ();
$card 1 = $playCard->getcard ();
$card 2 = $playCard->getcard ();
$result = $playCard->comparecards ($card 1, $card 2);
Echo ' Card1 is ', Printcard ($card 1), ' <br/> ';
Echo ' Card2 is ', Printcard ($card 2), ' <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, ', '). ') ';
}
I hope this article will help you with your PHP program design.