cg-core: make cg-shuffle optionally seeded and reproducible
cg-shuffle gains an optional SEED and a `cg-shuffle-seed' dynamic var. With a seed the order is deterministic and identical on every platform, so a deal can be replayed, shared between players, or pinned in a test; the index comes from secure-hash (spec-defined output everywhere, 7 hex digits stay a fixnum even on 32-bit Emacs), so no bignum/overflow behaviour can vary it. With no seed the shuffle is the byte-for-byte old system-random path -- normal play is unchanged. Test cgt-shuffle-seeded pins determinism, permutation, override and a cross-platform golden prefix; fails on the prior one-arg cg-shuffle. Suite 159/159.
This commit is contained in:
parent
1a59003b13
commit
6379127e17
8 changed files with 180 additions and 15 deletions
|
|
@ -262,6 +262,26 @@ known :type built from plain data; anything else never reaches
|
|||
(setcdr (cdr cyc) cyc)
|
||||
(should-not (cg-net--valid-p (list :type 'move :move cyc) '(hello move)))))
|
||||
|
||||
;;;; Seeded shuffle
|
||||
|
||||
(ert-deftest cgt-shuffle-seeded ()
|
||||
"A seeded shuffle is deterministic, portable, and a true permutation;
|
||||
the unseeded shuffle keeps its system-random behaviour. Opening #4."
|
||||
(let ((deck (number-sequence 0 51)))
|
||||
;; same seed -> identical order (FAILS on HEAD: cg-shuffle takes 1 arg)
|
||||
(should (equal (cg-shuffle deck 12345) (cg-shuffle deck 12345)))
|
||||
;; still a permutation of the input, nothing dropped or duplicated
|
||||
(should (equal (sort (copy-sequence (cg-shuffle deck 12345)) #'<) deck))
|
||||
;; different seeds -> different order (almost surely)
|
||||
(should-not (equal (cg-shuffle deck 1) (cg-shuffle deck 2)))
|
||||
;; the dynamic variable seeds it too, and an explicit seed overrides it
|
||||
(should (equal (let ((cg-shuffle-seed "deal-A")) (cg-shuffle deck))
|
||||
(let ((cg-shuffle-seed "deal-A")) (cg-shuffle deck))))
|
||||
(should (equal (cg-shuffle deck 7)
|
||||
(let ((cg-shuffle-seed "ignored")) (cg-shuffle deck 7))))
|
||||
;; portability pin: this exact prefix must hold on every platform
|
||||
(should (equal (seq-take (cg-shuffle deck "GOLDEN") 6) '(15 20 35 24 31 23)))))
|
||||
|
||||
;;;; Gaps
|
||||
|
||||
(ert-deftest cgt-gaps-deal ()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue