Reputation: 61
this is my code;
following functions are used to generate a new deck, length of the deck where it will be split and random number of shuffles to be performed.
public function newDeck()
{
$suits = ['S', 'H', 'C', 'D'];
$values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
foreach($suits as $suit)
{
foreach($values as $value)
{
$deck[$suit . $value] = $value;
}
}
return $deck;
}
//this function gives the length where the deck will be sliced
public function cutLength($len)
{
for ($i = 0; $i < 1; ++$i) {
$cutLength[] = rand(1,$len-1);
}
return $cutLength;
}
//this function gives the number of total shuffles
public function shuffleCount($count)
{
for ($i = 0; $i < 1; ++$i) {
$shuffleCount[] = rand(1,$count);
}
return $shuffleCount;
}
and the following code shuffles the card deck. Here I am just retrieving the card deck from a db.
$left = json_decode(Game::where('gamer_id',
$request->id)->latest()->first()->game_deck, TRUE);
//set of single shuffles
$m = $game->shuffleCount(5);
for($j = 0; $j < $m[0]; $j++) {
//count of complete deck
$leftLength = count($left);
//place from where the deck will be seperated
$fullDeckCutLength = $game->cutLength($leftLength);
//right is the lower portion of cards
$right = array_slice($left, $fullDeckCutLength[0], $leftLength - $fullDeckCutLength[0]);
//left is the upper portion of cards
$left = array_slice($left, 0, $fullDeckCutLength[0]);
//empty the full deck cut length array
$fullDeckCutLength = [];
//check the thickness of the cards in the right hand
$rightLength = count($right);
//if less than 10 cards in right hand, then this if will just place them on top of the cards in right hand
if($rightLength < 0.2 * $leftLength) {
$left = array_merge($right, $left);
$right = [];
continue;
} else {
//Single shuffle
$n = $game->shuffleCount(10);
for($i = 0; $i < $n[0]; $i++) {
//count cards in right hand
$rightLength = count($right);
//determine how many cards will be slid from the top of right deck to the top of left deck
$rightDeckCutLength = $game->cutLength($rightLength);
//remove the cards from the top of right hand deck
$rtl = array_slice($right, 0, $rightDeckCutLength[0]);
//place the cards from the top of right deck to the top of left deck
$left = array_merge($rtl, $left);
//empty the right to left array
$rtl = [];
//remaining cards in the right hand deck
$right = array_slice($right, $rightDeckCutLength[0], $rightLength - $rightDeckCutLength[0]);
//empty the right deck cut length array
$rightDeckCutLength = [];
//check the thickness of the cards in the right hand
$rightLength = count($right);
//if the cards in RH are less than 10 then continue the loop
if($rightLength < 0.2 * $leftLength) {
$left = array_merge($right, $left);
$right = [];
$n = [];
break;
} elseif($i == $n[0]) {
$left = array_merge($right, $left);
$right = [];
$n = [];
}
}
}
}
dd($left);
Now this code is shuffling the cards as per intended but the issue I am getting is that the output I am getting doesnot always has 52 cards, most of the times it is 52 cards but sometimes the output is less than 52. Can someone point out the issue, please.
Upvotes: 1
Views: 111