Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
card shuffler
#1
here's a good one that's not too hard but typically has a lot of capacity for improvement:

write a function that shuffles a 52 card deck of cards, as efficiently as possible.
ignatures suck
Reply
#2
Define "efficiently"

For how many players?

I mean, if there are 4 players, and you sort the cards by color, in rising order, 2-Ace..

Player 1 gets: 2
Player 2 gets: 3
etc

Pretty random..

Change to 5 players, and each player gets even more random cards.



So, should the actuall deck be randomized, or should the cards the player get be randomized?



And.. if theres an algorithm for it, it's no longer efficient, as people would know the order.

And if you use a totally random sequence, then well.. no challenge.
Reply
#3
it doesn't matter how many players will get dealt cards. Only that the deck gets randomized.
And, your method of "player 1 gets 2, player 2 gets 3" etc would not qualify as even remotely random.
Use of RND or rand() is fine.

I'm interested in actual coded implementations or very detailed descriptions of particular algorithms, not just broad general ideas.

The method I used was basically to simulate the actual act of physically shuffling a deck - it splits the deck into 2 halfs, then randomly decides if it should add a card from the left half or right half to the temporary pile, then repeats this process for a total of 7 iterations.
Also, the "cut" is made slightly uneven on purpose - anywhere from 22-30 to 30-22 for each half

cheers
- Eric
ignatures suck
Reply
#4
I don't have a QB example handy...however....my current favorite solution to this problem is as follows:

make a 2 dimensional array...with index[1st == card, 2nd == randomshuffelnumber]

Initialize the deck with appropriate cards

fill the randomshufflenumber with random numbers

sort the deck based on randomshufflenumber

deal the cards in order....they're shuffled


if you want to see a c++ app that usess this solution, ask...it may be a week for a reply...i'm out of town durring the week these days....


mango
Reply
#5
Not tested!!!

Code:
dim cards[51] as integer

dim i as integer, j as integer

for i = 0 to 51
    cards[i] = i
next i

for i = 0 to 51
     j = int(rnd * 52)  ' change to 51 if this errors out.
     swap cards(i), cards(j)
next i


for i = 0 to 51
     print cards(i)
next i
y smiley is 24 bit.
[Image: anya2.jpg]

Genso's Junkyard:
http://rel.betterwebber.com/
Reply
#6
here ya go... i got sick of doing "serious" things =P

Code:
Dim cards (51) As Long

Randomize Timer

For fillcards = 0 To 51

  Do
    tryagain = 0
  
    newrandomnum = Int(Rnd * 52) + 1

    Do

      For checksofar = 0 To fillcards
        If cards(checksofar) \ 256 = newrandomnum Then tryagain = -1:  Exit Do

      Next

    Loop While 0 = 1

  Loop While tryagain

  cards(fillcards) = (newrandomnum * 256) + fillcards

Next


For showcards = 0 To 51

  keyin$ = ""

  Do

    For findcard = 0 To 51
      If cards(findcard) \ 256 = showcards  + 1 Then valuedisplay = cards(findcard) Mod 256: Exit Do

    Next

  Loop While 0 = 1

  Select Case valuedisplay \ 13

    Case 0
      suit$ = "spades"

    Case 1
      suit$ = "hearts"
  
    Case 2
      suit$ = "diamonds"

    Case 3
      suit$ = "clubs"

  End Select


  Select Case valuedisplay Mod 13
  
    Case 0
      cardval$ = "Ace"

    Case 10
      cardval$ = "Jack"
  
    Case 11
      cardval$ = "Queen"

    Case 12
      cardval$ = "King"

    Case Else
      cardval$ = Str$((valuedisplay Mod 13) + 1)

  End Select

  ? "Card"; showcards; " = " + cardval$ + " of " + suit$

  Do While keyin$ = "": keyin$ = Inkey$: Loop
  
  If (keyin$ = Chr$(27)) Or (keyin$ = Chr$(255) + "X") Then End

Next

edit: looking at rel's mine seems grossly overcomplicated. meh...

most of it is just presentation though i guess :p
Reply
#7
I dont see the challenge.

I mean, like already posted, it's just a matter of randomizing an array..



And with my player1 etc example, what I meant to get to is:
A deck might be random, but if you have enough players, the random effect is lost.

example:
Deck: 1, 7, 6, 2, 9, Ace, 3

Player 1 gets: 1, 2, 3

While the deck is random, the player doesent get very random cards..


Which leads to the next issue: What is random.
Reply
#8
Quote:Which leads to the next issue: What is random.

erm?

i hope youre joking ;P
Reply
#9
Quote:Deck: 1, 7, 6, 2, 9, Ace, 3

Player 1 gets: 1, 2, 3

Deck is random?
Player gets random?

Should the player output be random, or the deck shuffle..

And like I said, it's nothing more than randomizing an array.. It's no challenge.. alteast not that I can see..
Reply
#10
all that must be shuffled is the deck.

In the hypothetical situation where the deck is randomized but 1 player still gets seemingly unrandom cards (e.g., 1,2,3, etc), this still counts as random, because indeed, there is always a probability that a randomized deck will yield an ordered hand. The fallacy in your argument is you appear to believe that if a player coincidently gets an ordered hand that it is therefore not considered random.

And you say it is a simple algorithm of randomizing an array - well, it's good that it's simple, so lets see your implementation.

peace
- e
ignatures suck
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)