所以我需要做一个二十一点模拟器游戏,但似乎无法弄清楚洗牌有什么问题。它应该从牌堆中随机取出一张牌,然后将其放在牌堆的顶部。最后将其从其余部分中删除。所以 :
(ace)(2)(3)(4)(5)...(k)
如果随机牌是 5
(5)(ace)(2)(3)(4)(5)...( k)
然后它删除第二个 5
(5)(ace)(2)(3)(4)(6)...(k)
这是代码:
(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)))))))
(