Reputation: 99
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.
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 />';
}
?>
Upvotes: 2
Views: 899
Reputation: 2555
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 ;)
Upvotes: 1
Reputation: 1667
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.
Upvotes: 1