justinh Posted February 1, 2009 Share Posted February 1, 2009 Okay im trying to write a program that creates a random number (1,52) and then determines what the card is. for instance 1 = 2 of spades 2 = 2 of clubs 3 = 2 of diamonds 4 = 2 of hearts I know that if the cards even, its either clubs or hearts. odd, spades or diamonds. Is there way of determining if an even is club or heart? odd, spade or diamond? Quote Link to comment https://forums.phpfreaks.com/topic/143402-deck-of-cards/ Share on other sites More sharing options...
premiso Posted February 1, 2009 Share Posted February 1, 2009 if (($card %2) == 0) { echo "Card is even"; }else { echo "Card is odd"; } Should tell you whether it is odd or even. The % is the modulus operator, if you want to read up on that. Quote Link to comment https://forums.phpfreaks.com/topic/143402-deck-of-cards/#findComment-752191 Share on other sites More sharing options...
justinh Posted February 1, 2009 Author Share Posted February 1, 2009 Sorry must have worded by question wrong, I know how to determine if the number is even or odd. Maybe it will help if I give an example: Let's say rand(1,52) outputs 12 <?php $card = rand(1,52); if ( ( $card % 2 ) == 0 ) { //card is either Clubs, or Hearts } else { //card is either Spades or Diamonds } ?> This would only tell us that 12 is a club OR a heart.. I'm wondering after I figure this out, How would I go about finding out if 12 is a club OR if 12 is a heart. Quote Link to comment https://forums.phpfreaks.com/topic/143402-deck-of-cards/#findComment-752198 Share on other sites More sharing options...
Lucky_PHP_MAN Posted February 1, 2009 Share Posted February 1, 2009 I'm assuming that your (1,52) is a complete sets of card. I would usually do a pre-defined sets of cards in an array. So, let's say 1-13 = Spades 14-26 = Hearts 27-39 = Clubs 40-52 = Diamonds Then check the number of the cards against these array of cards. It's just an opinion. Hope this help Regards, LPM Quote Link to comment https://forums.phpfreaks.com/topic/143402-deck-of-cards/#findComment-752200 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.