Author Topic: 52-card deck code, including probability and card removal???  (Read 566 times)

I'm trying to simulate a 52 card poker deck for my Blackjack server. What's the best or most efficient way to go about this? I want to create a deck with all the proper suits and cards dictated by probability but realistic probability, so that no more than 4 of the same card value can be picked, and picking a card will remove that card from the deck, thus lowering the active probability of drawing it again, while slightly boosting the odds for picking a different card.

Should I use script objects? Variables? Arrays? I know to use getRandom(0,12) to specify the face value, and probably getRandom(0,4) to specify suit, but I do still want to realistically represent card loss and drawing as accurate as possible.

Share solutions/ideas.

just have an array of cards? you can access global vars like $Card[%suit][%value] where %suit and %value are some string or number

you could go the whole mile and create a shuffle system so you only need to drw cards from the top, using a formatted string to save the order

ill post a more complete idea later when im on my computer
swollows setup is basically that but implemented more smartly (why did i forget scriptobjects exist  lmao)
« Last Edit: January 15, 2016, 03:08:14 AM by Conan »

Code: [Select]
package cardDeck
{
function cardDeck::getCardName(%this,%card)
{
%num = getWord(%card,0);
%suite = getWord(%card,1);
return (%this.cardSpecial[%num] $= "" ? %num : %this.cardSpecial[%num]) @ " of " @ %this.cardType[%suite];
}
function cardDeck::iniDeck(%this)
{
%this.cardType[0] = "Spades";
%this.cardType[1] = "Diamonds";
%this.cardType[2] = "Clubs";
%this.cardType[3] = "Hearts";
%this.cardSpecial[1] = "A";
%this.cardSpecial[11] = "Jack";
%this.cardSpecial[12] = "Queen";
%this.cardSpecial[13] = "King";
%this.cardCnt = 0;
%this.cardPos = 0;
%this.deckSize = 52;
for(%i=0;%this.cardType[%i]!$="";%i++)
{
for(%a=1;%a<=13;%a++)
{
%this.card[%this.cardCnt] = %a SPC %this.cardType[%i];
%this.cardCnt++;
}
}
}
function cardDeck::shuffleDeck(%this)
{
for(%i=%this.deckSize-1;%i>=0;%i--)
{
%rand = getRandom(0,%i);
%tempCard = %this.card[%i];
%this.card[%i] = %this.card[%rand];
%this.card[%rand] = %tempCard;
}
}
function cardDeck::pickTopCard(%this)
{
if(%this.cardPos >= %this.deckSize)
return "Deck Empty";
%card = %this.card[%this.cardPos];
%this.cardPos++;
return %card;
}
};
activatePackage(cardDeck);
if(isObject($Swol_CardDeckObj))
$Swol_CardDeckObj.delete();
$Swol_CardDeckObj = new scriptObject(cardDeck);

some code I had lying around, should get you started

This looks good enough. Thanks swollow, i'll build off of this