SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,053
    Mentioned
    66 Post(s)
    Tagged
    0 Thread(s)

    Fun Challenge - smallest block of code you can devise to build a deck of cards.

    Just for fun, seeing if anyone can make a block that defines a standard 52 card deck faster than this.

    Code php:
    function createDeck() {
      $deck = array();
      for ($i = 0, $i < 52; $i++) {
        switch(floor($i / 13)) {
          case 0: $suit = 'S'; break;
          case 1: $suit = 'H'; break;
          case 2: $suit = 'D'; break;
          case 3: $suit = 'C'; break;
        }
     
        switch($i % 13) {
          case 0: $pip = 'A'; break;
          case 12: $pip = 'K'; break;
          case 11: $pip = 'Q'; break;
          case 10: $pip = 'J'; break;
          default: $pip = $i + 1; break;
        }
        $deck[$i] = $pip.$suit;
      }
     
      return $deck;
    }

    The challenge is - while keeping it legible, find a solution in fewer lines.

    DISCLAIMER - Code above untested.

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <?php
    $deck = array();
    foreach(array('S', 'H', 'D', 'C') as $suit)
    {
        $pips = array_merge(
            array('A'),
            range(2, 10),
            array('J', 'Q', 'K')
        );
        
        foreach($pips as $pip){
            $deck[] = $pip . $suit;
        }
    }
    Last edited by AnthonySterling; Feb 1, 2012 at 16:39. Reason: Missed 10, thanks Lyndah. :)
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    702
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    function createDeck()
    {
        
    $suits = array('S','H','D','C');
        
    $pips  = array('A','K','Q','J','10','9','8','7','6','5','4','3','2');
        
    $deck  = array();
        foreach(
    $suits as $suit
        {
            foreach(
    $pips as $pip)
            {
                
    $deck[] = $suit $pip;
            }
        }
        return 
    $deck;


  4. #4
    Non-Member
    Join Date
    Jan 2012
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Should
    PHP Code:
    range(29), 
    be

    PHP Code:
    range(210), 
    I don't see where the 10 of any suit is defined.

  5. #5
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    702
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
        return array
        (
            
    'SA','SK','SQ','SJ','S10','S9','S8','S7','S6','S5','S4','S3','S2',
            
    'HA','HK','HQ','HJ','H10','H9','H8','H7','H6','H5','H4','H3','H2',
            
    'DA','DK','DQ','DJ','D10','D9','D8','D7','D6','D5','D4','D3','D2',
            
    'CA','CK','CQ','CJ','C10','C9','C8','C7','C6','C5','C4','C3','C2',
        ); 

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ahundiak View Post
    PHP Code:
        return array
        (
            
    'SA','SK','SQ','SJ','S10','S9','S8','S7','S6','S5','S4','S3','S2',
            
    'HA','HK','HQ','HJ','H10','H9','H8','H7','H6','H5','H4','H3','H2',
            
    'DA','DK','DQ','DJ','D10','D9','D8','D7','D6','D5','D4','D3','D2',
            
    'CA','CK','CQ','CJ','C10','C9','C8','C7','C6','C5','C4','C3','C2',
        ); 
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    Non-Member
    Join Date
    Jan 2012
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think ahundiak wins

  8. #8
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <?php
    $deck = array();
    foreach(array('S', 'H', 'D', 'C') as $suit)
    {
        array_map(
            function($pip) use ($suit, &$deck) {
                $deck[] = $suit . $pip;
            },
            array_merge(array('A', 'J', 'Q', 'K'), range(2, 10))
        );
    }
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  9. #9
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Code PHP:
    function cardDeck() {
    	$deck = '';
    	foreach( array('C', 'D', 'H', 'S') as $suit ) {
    		foreach( array_merge(array('A', 'J', 'Q', 'K'),range( 2, 10 )) as $card ) {
    			$deck[] = $suit . '_' . $card;
    		}
    	}
    	return $deck;
    }
     
    $cardDeck = cardDeck();
    shuffle($cardDeck);
    print_r($cardDeck);

  10. #10
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    702
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    $tpl '#A,#K,#Q,#J,#10,#9,#8,#7,#6,#5,#4,#3,#2,';
    return 
    explode(',',
        
    str_replace('#','C',$tpl) . 
        
    str_replace('#','D',$tpl) . 
        
    str_replace('#','H',$tpl) . 
        
    str_replace('#','S',$tpl)); 

  11. #11
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,053
    Mentioned
    66 Post(s)
    Tagged
    0 Thread(s)
    Heh heh heh -- I'll take the second entry now. That is clever.

  12. #12
    Non-Member
    Join Date
    Jan 2012
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I like entry 3 better

    The next step now would be to create a stack of multiple decks and shuffle the whole stack.


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
  •