Results 1 to 2 of 2

Thread: Wrong Sort

  1. #1
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    286
    Thanks
    95
    Thanked 3 Times in 3 Posts

    Default Wrong Sort

    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?

    Code:
    $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
    Last edited by marain; 03-28-2014 at 12:36 AM.

  2. #2
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    286
    Thanks
    95
    Thanked 3 Times in 3 Posts

    Default

    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.
    Last edited by marain; 03-26-2014 at 06:28 PM. Reason: To reflect additional thinking on the subject.

Similar Threads

  1. Math wrong? Array wrong?
    By bluewalrus in forum PHP
    Replies: 0
    Last Post: 10-17-2009, 02:32 PM
  2. Resolved Sort?
    By Nile in forum JavaScript
    Replies: 1
    Last Post: 12-24-2008, 03:20 PM
  3. Looking for customizable CMS, sort of....
    By Strangeplant in forum PHP
    Replies: 1
    Last Post: 11-07-2008, 01:31 PM
  4. please help me to sort this out
    By itsvps in forum MySQL and other databases
    Replies: 0
    Last Post: 01-20-2007, 06:53 AM
  5. Sort by...?
    By Elevation in forum HTML
    Replies: 2
    Last Post: 02-26-2006, 10:30 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •