views:

183

answers:

1

So I need to do a game of blackjack simulator, butt can't seem to figure out whats wrong with the shuffle it's suppose to take a card randomly from the pack the put it on top of the pack. The delete it from the rest. so :

(ace)(2)(3)(4)(5)...(k)
if random card is let say 5
(5)(ace)(2)(3)(4)(5)...(k)
then it deletes the 2nd 5
(5)(ace)(2)(3)(4)(6)...(k)

here is the code:

    (define deck '((A . C) (2 . C) (3 . C) (4 . C) (5 . C) (6 . C) (7 . C) (8 . C) (9 . C) (10 . C) (V . C) (Q . C) (K . C)))

;auxilliary function for shuffle let you randomly select a card.
(define shuffAux
  (lambda (t)
    (define cardR
  (lambda (t)  (list-ref t (random 13))))
    (cardR t)))

;auxilliary function used to remove the card after the car to prevent
you from removing the randomly selected from the car(begining of the deck).
(define (removeDupC card deck)
      (delete card (cdr deck))
      )

(define shuffle2ndtry
  (lambda (deck seed)
    (define do-shuffle
      (lambda (deck seed)
        (if (> seed 0)(
        (cons (shuffAux deck) deck)
        (removeDupC (car deck)  deck)
        (- 1 seed))
        (write deck)   
        )
      )
      )
    (do-shuffle deck seed)))

(define (shuffle deck seed)
  (define cards (cons (shuffAux deck) deck))
  (write cards)
  (case (> seed 0)
   [(#t)
        (removeDupC (car cards) (cdr cards)) 
        (shuffle cards (- seed 1))]
   [(#f) (write cards)]))

(define random
 (let ((seed 0) (a 3141592653)
  (c 2718281829) (m (expt 2 35)))
  (lambda (limit)
   (cond 
   ((and (integer? limit))
    (set! seed (modulo (+ (* seed a) c) m))
    (quotient (* seed limit) m))
   (else
   (/ (* limit (random 34359738368))
   34359738368))))))


;function in which  you can delete an element from the list.
(define delete
  (lambda (item list)
    (cond
     ((equal? item (car list)) (cdr list))
     (else (cons (car list) (delete item (cdr list)))))))



(
A: 

There is a better way to shuffle a deck like that. With your method it's possible that there is a group of cards that stay in order.

It's better to loop through the deck and for each card swap it with a random position. You would want to use a vector for this. As a bonus it will be much faster :)

Brian Makin