Here is the deal, working on a card game using php mysql, I have a very simple way of shuffling the deck and I use array_chunk() to divide the deck into hands, the number of cards in each hand is variable depending of the numbers of players, there is also an extra hand called the widow. I'm still a long way to get the game going but my problem at the moment is to be able to insert the hands in the database after the initial shuffling, any advice would be greatly appreciated.

Here is my code so far:

<?php
$deck = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52");

shuffle($deck);



$players = "5";

$hands = (int)(52/($players + 1)); //Number of players + 1 for the widow         
$widow = (int)52%($players + 1);   //one full hand + the remainder of the cards  
$cards_per_hand = $hands = (int)(52/($players + 1));

echo "Player: $players";
echo "<br>Hand: $hands";
echo "<br>Widow: $widow";

//DEBUG
/*
echo "<pre>";
//print_r(array_chunk($deck, $hands));
print_r(array_chunk($deck, $hands, true));
echo "</pre>";
*/

$game = array_chunk($deck, $hands);


            for ($deck= 0; $deck < ($players + 2); $deck++)
            {
               echo "<li><b>Player $deck</b>";
               echo "<ul>";

                for ($hands = 0; $hands < $cards_per_hand; $hands++)
                {       
                   //echo "<img src=\"http://www.cemond.com/RUMMOLI/Cards_1_52/".$game[$deck][$hands].".png\">";
                     echo "".$game[$deck][$hands].", ";
                }

               echo "</ul>";
               echo "</li>";
            }

?>

An here is a snapshot of what I can print on the screen.

Player 0

46, 12, 32, 42, 48, 13, 37, 20, 

Player 1

4, 16, 40, 24, 22, 45, 15, 27, 

Player 2

14, 50, 17, 49, 33, 31, 30, 18, 

Player 3

26, 35, 51, 21, 29, 10, 7, 52, 

Player 4

47, 5, 1, 8, 41, 25, 44, 23, 

Player 5

38, 6, 39, 11, 43, 3, 19, 36, 

Player 6

2, 28, 34, 9, , , , , 

Recommended Answers

All 3 Replies

Well... you have strings... you can store those strings and associate them to a player.
You have a comma delimited list of card numbers. You can have a table that has the columns: id, playerID, gameID, cardNo and insert them individually, or use a stored proceedure that takes the comma delim. string, and inserts them.

Your options are plentiful.. think about why you are putting this in the database, and what purpose it will serve, and that should help you come up with a schema for data tracking. You know the rules of your game, and you know the plays that will be made. Knowing this, you are the only one that can come up with a schema and method for storing, updating, deleting, and creating data as you need it.

Sorry bud, your question is too vague to give you any real answer :-/

"your question is too vague", yup, but for some reason putting it out there helps me to see it more clearly, your comment about the purpose it will serve hit the spot, I have a tendacy to lose sight, overwhelmed with the big picture instead of breaking it in small step.

Just running in the wood to find a tree, any tree. :)

By the way: you can use $deck = range(1, 52); to get the numbers array, each entry will be an integer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.