Log in

View Full Version : Resolved Wrong Sort



marain
03-26-2014, 11:42 AM
Hi Folks,

The following code is developing and echoing a poker hand. The code is sorting the hand by suit. Is there a straightforward way to sort the hand, instead, by rank of card?


$suits = array(
"♠",
"<span style='color : #ff4040; '>&hearts;</span>",
"<span style='color : #ff4040; '>&diams;</span>",
"&clubs;"
);

$ranks = array (
"Ace",
"Deuce",
"Trey",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten",
"Jack",
"Queen",
"king"
);

$card = 0; // loop index: We'll draw five cards
$cardsInSuit = 13; // number of cards for each suit, ace low
$deck = range (0, 51); // establishes array of numbers from 0 to 51
shuffle ($deck); // and this shuffles the "deck"
$raw_hand = array_slice ($deck, 0, 5); // extract first five cards
sort ($raw_hand); // sort

echo "<div align='right'><font size='-1'>Here is your " . $handStatus . "poker hand. Please use it
wisely:<br />";

while ($card < 5) { // will iterate for $card = 0, 1, 2, 3, 4
$lemma = $raw_hand[$card]; // select "card" from raw hand, call it $lemma
$rank = $lemma % $cardsInSuit; // place it in range of zero to twelve
$pips = $ranks[$rank]; // find spelled out rank of card, from ranks array
$lemma = floor($lemma / $cardsInSuit); // determine quartile (52 divided by 13 = 4)
$suit = $suits[$lemma]; // select suit corresponding to quartile
echo "$pips $suit &nbsp;&nbsp;&nbsp;"; // echo processed card to screen
$card ++; // bump card count
}

The finished product is at http://www.marainlaw.com/page.php?here=quotations

marain
03-26-2014, 01:35 PM
On further reflection, this is a logic problem and not a PHP problem. I'll work on it further myself. (But suggestions from the crowd would still be welcomed.)

A.

Further thoughts (if y'all don't mind my progressive thinking): Suit = f(quartile). Rank = f(modulus). Therefore I must first incorporate modulus into the five cards that constitute the hand, sort by modulus, then after the sort, strip out the modulus, to prepare what will be sent to the screen. While accomplishing this will take a bit of thought, I envision nothing particularly complicated. But wonder whether a more elegant solution exists.

A.