ID:153738
 
Well, I was just pondering on my new card game that I am working on. And I was trying to find a way on how to deal the cards to the players. (There are 52 cards) And I want to make it so that each player doesnt get the same card. So that if player one gets The 6 of Hearts, Player two shouldn't have the 6 of Hearts. I was thinking of calling the rand proc, and each number out of 52 stands for a cards, if that number hasn't been picked by either player, then the card that is given to the user and it adds the card to a list, then when the proc loops through again and picks the same number, it'll lop through it again. But the main problem with this is that the cards for each player may not be even. And I want each player to have exactly 26 cards each. Lol, I know this is a bit much. But, I hope theres a fairly simple way of doing this. :/

Thanks in advanced...

-GDT
It's not too hard. Just make yourself a Deck class which contains a list of 52 numbers, shuffle 'em, then iterate through the deck as you need to retrieve cards.

Deck
{
var
{
Cards[]
stackPointer
}

New()
{
var/i
Cards = new /list()
for(i = 1; i <= 52; i++)
Cards.Add(i)
stackPointer = 1
}

proc
{
Shuffle()
{
var/i
var/j
var/k
for(i = stackPointer; i <= 52; i++)
{
k = rand(stackPointer,52)
j = Cards[k]
Cards[k] = Cards[i]
Cards[i] = j
}
}

GetNextCard()
{
stackPointer++
if(stackPointer <= 52)
return(Cards[stackPointer])
else
return 0 // No cards are left in the deck
}
}
}
A deck of cards is best handled with a list.

However you can take a shortcut to implementing shuffling for most games: Just use pick() to get a card from the list, and remove it each time.

Lummox JR