Reputation: 31
Here is the code I'm using to give the user 13 cards and the cards change every time the page refreshes. What I'm trying to do is give every user 13 cards that are different than the other users where there are 4 users in every room and should be 52 cards.
echo $_SESSION["user"];
echo "<br><br> Your cards :<br>";
function pc_array_shuffle($array) {
$i = count($array);
while(--$i) {
$j = mt_rand(0, $i);
if ($i != $j) {
// swap elements
$tmp = $array[$j];
$array[$j] = $array[$i];
$array[$i] = $tmp;
}
}
return $array;
}
$suits = array('denari', 'kuba', 'sanak', 'shjra');
$cards = array( 2, 3, 'J',4, 5, 6,'Q', 7, 8,'K', 9, 10,'A');
$deck = pc_array_shuffle(range(1,13));
while (($draw = array_pop($deck)) != NULL) {
print "<br>" . $cards[$draw / 4] . ' of ' . $suits[$draw % 4] . "<br>\n";
}
Upvotes: 3
Views: 1019
Reputation: 195
You mean to deal cards from the same deck to 4 different players connected from different computers/browsers ? If so, that won't help you, 'cause that code will deal cards from a new deck for each player, the deck is not shared by the connected users, you'll need to build a database with some tables like 'play_tables', 'players', 'decks' and 'cards', in such a way that decks has cards, play_tables has a deck and has a max of 4 players, and players have cards, so you build a deck with the default 52 cards for a table and shuffle it, then the users should connect to the table and let them choose to "begin dealing" and then you deal the cards of the shared deck to the connected players of the play_table. You need to keep all this in a database so all users are accessing the same information, if not, you are building and dealing cards of a deck built for each player, there is no relation between them that way.
Upvotes: 1
Reputation: 3867
Simmilar like @kainaw said, but with code:
$suits = array('denari', 'kuba', 'sanak', 'shjra');
$cards = array( 2, 3, 'J',4, 5, 6,'Q', 7, 8,'K', 9, 10,'A');
$deck = array();
foreach ($suits as $suit) {
foreach ($cards as $card) {
$deck[]=$card. " of " . $suit;
}
}
shuffle($deck);
//deal out the cards from deck
`
Upvotes: 0