计划作业二十一点洗牌

发布于 2024-08-27 08:12:55 字数 1781 浏览 5 评论 0原文

所以我需要做一个二十一点模拟器游戏,但似乎无法弄清楚洗牌有什么问题。它应该从牌堆中随机取出一张牌,然后将其放在牌堆的顶部。最后将其从其余部分中删除。所以:

(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给妤﹃绝世温柔 2024-09-03 08:12:55

有一种更好的方法来洗牌。
按照你的方法,有可能有一组卡片保持有序。

最好循环浏览这副牌,并为每张牌将其交换到随机位置。您可能想为此使用向量。作为奖励,它会快得多:)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文