Jump to content

deck of cards


justinh

Recommended Posts

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?

 

 

Link to comment
https://forums.phpfreaks.com/topic/143402-deck-of-cards/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/143402-deck-of-cards/#findComment-752198
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/143402-deck-of-cards/#findComment-752200
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.