Tie for Golden size comparison algorithm development tutorial chess platform in php

PHP, bar Jinhua how to implement than the size

In the chess game, whether it is realistic or line, bar Jinhua is undoubtedly one of the most popular board games, I grew fond of bar Jinhua, by chance behind in the IT industry, he did not talk much, directly into the topic it.

Tie for Golden Rule compare two cards do not say, indicate what is straight when: JQK <A23 <QKA

The following is a personal piece of advice, for your reference! (Where there are not welcome to teach chess platform development: www.yasewl.com)

Ideas: Tie for Golden

1. two randomly generated cards, each deck structure

Copy the code code is as follows:
Array (
Array ( 'Spade', 'K'),
Array ( 'Club', '. 6'),
Array ( 'Spade', 'J'),
)

Copy the code code is as follows:
Array (
Array ( 'Spade', 'K'),
Array ( 'Club', '. 6'),
Array ( 'Spade', 'J'),
)
2. The value calculated for each of the cards : each of the cards has a original size (ie excluding pairs, straight, Golden, shun gold, the size of the cheese), then

The value of each card is a 2 digit up to 2 preamble bits 0, for example, 'A': 14, '10 ': 10,' 2 ':' 02 ',' k ': 13,' 7 ' : 07

The three points sorted according to size card (descending), make up a six-figure. For example 'A27': 140702, '829': 090802, 'JK8': 131108, '2A10': 141002

Exception, for the number of bits you want to pair the pair on the first two (see later how to do so). E.g. '779': 070709, '7A7': 070714, 'A33': 030314

Now the score is a 6-digit, will be a sub-set of the original value plus the value of 10 * 100000, now a 7-digit number. E.g. '779': 1070709, '7A7': 1070714, 'A33': 1030314

For straight, plus 20 * 100 000 results .. E.g. '345': 2050403, 'QKA': 2141312, '23A': 2140302

For Golden, the result plus 30 * 100,000. For example 'Spade K, Spade 6, Spade J': 3131106

Because Shunjin exactly is the Golden and straight and so along the gold should be 50 * 10000. For example 'Spade 7, Spade 6, Spade 8': 5080706

For cheese, the result plus 60 * 100,000. E.g. '666': 6060606, 'JJJ': 6111111

3. Comparison of the size of the two cards (compared with the calculated value)

It's that simple! !

Code below (PHP)

复制代码 代码如下:
<?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
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,',').')';
}

 
Source: https: //www.cnblogs.com/bobpop/p/7238633.html

PHP, bar Jinhua how to implement than the size

In the chess game, whether it is realistic or line, bar Jinhua is undoubtedly one of the most popular board games, I grew fond of bar Jinhua, by chance behind in the IT industry, he did not talk much, directly into the topic it.

Tie for Golden Rule compare two cards do not say, indicate what is straight when: JQK <A23 <QKA

The following is a personal piece of advice, for your reference! (Where there are not welcome to teach chess platform development: www.yasewl.com)

Ideas: Tie for Golden

1. two randomly generated cards, each deck structure

Copy the code code is as follows:
Array (
Array ( 'Spade', 'K'),
Array ( 'Club', '. 6'),
Array ( 'Spade', 'J'),
)

Copy the code code is as follows:
Array (
Array ( 'Spade', 'K'),
Array ( 'Club', '. 6'),
Array ( 'Spade', 'J'),
)
2. The value calculated for each of the cards : each of the cards has a original size (ie excluding pairs, straight, Golden, shun gold, the size of the cheese), then

The value of each card is a 2 digit up to 2 preamble bits 0, for example, 'A': 14, '10 ': 10,' 2 ':' 02 ',' k ': 13,' 7 ' : 07

The three points sorted according to size card (descending), make up a six-figure. For example 'A27': 140702, '829': 090802, 'JK8': 131108, '2A10': 141002

Exception, for the number of bits you want to pair the pair on the first two (see later how to do so). E.g. '779': 070709, '7A7': 070714, 'A33': 030314

Now the score is a 6-digit, will be a sub-set of the original value plus the value of 10 * 100000, now a 7-digit number. E.g. '779': 1070709, '7A7': 1070714, 'A33': 1030314

For straight, plus 20 * 100 000 results .. E.g. '345': 2050403, 'QKA': 2141312, '23A': 2140302

For Golden, the result plus 30 * 100,000. For example 'Spade K, Spade 6, Spade J': 3131106

Because Shunjin exactly is the Golden and straight and so along the gold should be 50 * 10000. For example 'Spade 7, Spade 6, Spade 8': 5080706

For cheese, the result plus 60 * 100,000. E.g. '666': 6060606, 'JJJ': 6111111

3. Comparison of the size of the two cards (compared with the calculated value)

It's that simple! !

Code below (PHP)

复制代码 代码如下:
<?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
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,',').')';
}

Guess you like

Origin www.cnblogs.com/gao88/p/11371602.html