计划作业二十一点洗牌
所以我需要做一个二十一点模拟器游戏,但似乎无法弄清楚洗牌有什么问题。它应该从牌堆中随机取出一张牌,然后将其放在牌堆的顶部。最后将其从其余部分中删除。所以:
(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)))))))
(
So I need to do a game of blackjack simulator, but can't seem to figure out what's wrong with the shuffle. It's supposed to take a card randomly from the deck, then put it on top of the pack. And finally 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)))))))
(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一种更好的方法来洗牌。
按照你的方法,有可能有一组卡片保持有序。
最好循环浏览这副牌,并为每张牌将其交换到随机位置。您可能想为此使用向量。作为奖励,它会快得多:)
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 :)