2

Quick background on myself. This is my first time coding in PHP. I have a computer information systems degree, learned C++, VB, Cobol and Java in college (about 15 years ago), but have not really used it since. Some stuff is coming back to me as I learn this.

I'm attempting to simulate the opening of randomized packs of cards for a trading card game. The end result would be printing out 4 pages. Each page will list 12 card numbers, and all of the information on the card.

Here's what I was planning on doing:

Step One: Generate 180 random numbers from different ranges. Each number represents a card in the game.

  • range 1 = 35-74 (116 numbers)
  • range 2 = 75-106 (46 numbers)
  • range 3 = 107-134 (16 numbers, no duplicates)
  • range 4 = 135-142 (2 numbers, no duplicates)

Step Two: Take the 180 numbers and break them down into 90 pairs.

Step Three: From the 90 pairs, break them down to 4 sets of 6 pairs.

Step Four: From the 4 sets of 6 pairs, list the card information for each number and make 4 printable pages, 1 for each set.

I've gotten as far as creating the 180 random numbers. I'm still working on getting the unique numbers. I've tried creating arrays for the numbers I need, but none of them work. Here's the last working code I have, which will generate the 180 numbers I need, however, range 3 and 4 need to be fixed to not allow duplicates.

The way i currently have this coded, it just displays the numbers on the screen. Should I be storing them in an array? Am I just completely tackling the wrong way?

<?php

// generate 116 common cards
echo "Commons: " . '<br />';
for ($commonfeed = 0; $commonfeed < 116; $commonfeed++) {
echo mt_rand(35, 74). '<br />';
}

// generate 46 uncommon cards
echo "Uncommons: " . '<br />';
for ($uncommonfeed = 0; $uncommonfeed < 46; $uncommonfeed++) {
echo mt_rand(75, 106). '<br />';
}

// generate 16 rare cards
echo "Rares: " . '<br />';
for ($rarefeed = 0; $rarefeed < 16; $rarefeed++) {
echo mt_rand(107, 134). '<br />';
}

// generate 2 super rare cards
echo "Super Rares: " . '<br />';
for ($superrarefeed = 0; $superrarefeed < 2; $superrarefeed++) {
echo mt_rand(135, 142). '<br />';
}

?>
  • 1
    No really sure I what your algorithm is supposed to do, especially what the breaking down into sets of pairs is supposed to achieve – but if you want the random numbers out of your initial ranges to be unique, then I’d suggest you start with the `range` function, `shuffle` the resulting array, and pick the first x elements out of that. – CBroe Apr 13 '16 at 19:50
  • how do you break 90 pairs (180 cards) to 4 sets of 6 pairs? 6 pairs = 12 cards, multiply that by 4 sets and you get 48 cards. – hummingBird Apr 13 '16 at 19:51
  • Sorry, I should have clarified. The code above is just step 1. It creates the 180 cards that are in the box of cards. Step 2 separates them into packs of 2. Step 3 simulates the drafting of the packs. At the end, yes, 48 cards will be selected. All 180 cards will not be used. This is supposed to simulate a 2 player draft. Eventually I'd like to expand it to 4 or 6. – DigitalFirefly Apr 14 '16 at 12:29

2 Answers2

1

Since you are learning to code (again), I will answer using a few pointers instead of just generating some working code.

To start off with the last question: yes, i would be storing everything in an array. This way you can split "processing" code from "output" code.

Are you tackling this the wrong way? Hard to say, depends on all game mechanics and such. But to start easy: yes, it is a good start.

Using array_unique you can make an array unique, which you can use while generating the rare and superrare cards.

Onto the game mechanics: Are you sure you always want to give someone 16 rare cards and 2 super rare cards? What you could do, is create the total "deck of cards" up front, and then select the number of cards you would like:

$number_of_cards = 5;
$deck = [1,1,1,1,2,2,2,2,3,3,4,4,100,101,102];
shuffle($deck); // shuffle the cards
$selected = array_slice($deck, 0, $number_of_cards); // select the amount of cards

You can even just use this using strings instead of integers.

Ronald Swets
  • 1,669
  • 10
  • 16
  • Thanks for the response. I'll try rewriting it with arrays. As for the mechanics of the game. The card counts (116, 46, 16, and 2) are per box. There's 180 cards in the box, 2 per pack. The draft I'm trying to simulate would select 6 packs per person, two times. So you won't be getting 16 rare and 2 super rare every time since only 48 of the 180 cards will be drafted (24 per player). Here's the link to the game format I'm trying to simulate. http://wizkids.com/dicemasters/rainbow-draft/ – DigitalFirefly Apr 13 '16 at 19:51
1

Here's a solution you might try:

$cards = array();
// get cards per range

for($i = 0; $i < 116; $i++) {
    // range 1:
    $cards[] = mt_rand(35, 74); 
    // for the fun, let's also do range 2:
    if($i < 46) {
        $cards[] = mt_rand(75, 106);
    }
}

// range 3: (and range 4)
$rare = array();
$superrare = array();
for ($i = 107; $i <= 134; $i++) {
    $rare[] = $i;
    // range 4:
    if ($i <= 114) {
        $superrare[] = $i + 28;
    }
}
shuffle($rare);
shuffle($superrare); // not the best choice of randomness (check: http://stackoverflow.com/questions/5694319/how-random-is-phps-shuffle-function)

$cards = array_merge($cards, array_slice($rare, 0, 16));
$cards = array_merge($cards, array_slice($superrare, 0, 2));

// shuffle everything one more time since cards have been added randomly 
// to the deck
shuffle($cards);

// now when we have everything in $cards - 180 of random cards, we can
//    pack them
$pack1 = array_slice($cards, 0, 90);
$pack2 = array_slice($cards, 90, 90);

// always picking first n cards because they are all shuffled
$player1Drafted = array_slice($pack1, 0, 48);
$player2Drafted = array_slice($pack2, 0, 48);

// print it all
print_r(array('Player1' => $player1Drafted, 
              'Player2' => $player2Drafted));

In the end, I'm not entirely sure i guessed the drafting process OK, but it seemed to me that randomization was the biggest issue and that I solved it OK. Again, if you think that shuffle is not good enough, it can be done differently, but that's another story ;)

hummingBird
  • 2,495
  • 3
  • 23
  • 43
  • Thank you, this helps a lot. I was running it a few times to test it, but when the 48 cards are selected, I'm not seeing anything above 116 (no rare or super rare). Here's what the end of the array looks like 71 [159] => 50 [160] => 71 [161] => 67 [162] => Array ( [0] => 127 [1] => 129 [2] => 115 [3] => 131 [4] => 124 [5] => 107 [6] => 132 [7] => 119 [8] => 120 [9] => 116 [10] => 110 [11] => 126 [12] => 114 [13] => 111 [14] => 130 [15] => 108 ) [163] => Array ( [0] => 136 [1] => 137 ) ) The rare and super rare are there, but it doesn't seem like they're going into the packs. – DigitalFirefly Apr 14 '16 at 19:56
  • you're right - i should've shuffled all the cards one more time after everything has been added. check it now. – hummingBird Apr 15 '16 at 09:45
  • Ok, rares and SRs are showing up now. However, looking at the contents of the arrays I'm seeing the following. $cards only contains 164 entries, not 180. Also $rare and $superrare are always lumped together in $cards, and $packs. So instead of 1 rare showing up in a pack, all the rares show up in the pack. – DigitalFirefly Apr 15 '16 at 12:55
  • Yeah, it seems like it's not combining the arrays into $cards correctly. When I look at the output, it shows a bunch of numbers, then it will say Array, and list all of the rares or super rares. – DigitalFirefly Apr 15 '16 at 14:16
  • resolved :). when i was adding rares and superrares to the array, i should've added it using `array_merge`. it should be fine now. – hummingBird Apr 15 '16 at 14:17
  • well,it seems like you're not handling the arrays correctly. i guess this is matter of a new question - it would look messy if you added it to your question now. – hummingBird Apr 15 '16 at 16:25
  • Thanks, trying something now. If it doesn't work I'll repost a new question. – DigitalFirefly Apr 15 '16 at 16:27