From 64c74c2e6761feeb591f334b6bee835d5847dfac Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Wed, 23 Apr 2025 21:10:18 -0400 Subject: [PATCH] initial commit --- cube-scrambler.el | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 cube-scrambler.el diff --git a/cube-scrambler.el b/cube-scrambler.el new file mode 100644 index 0000000..b7bbd9c --- /dev/null +++ b/cube-scrambler.el @@ -0,0 +1,41 @@ +(defvar cube-faces "UDLRFB") + +(defun cube-scramble (moves) + "Generates a list of random scramnles for a 3x3 Rubik's cube and displays it. + +MOVES represents the number of moves in the list." + (interactive "p") + (message (mapconcat 'identity (cube-build-scramble moves) " "))) + +(defun cube-build-scramble (moves) + "Generates a list of random moves for a 3x3 Rubik's cube + +MOVES os the number of moves to generate. The algorithm will not +generate a sequence that moves the same face twice in a row." + (let (result last-face) + (while (> moves 0) + (setq last-face (cube-random-face last-face)) + (push (cube-random-turn last-face) result) + (setq moves (1- moves ))) + result)) + +(defun cube-random-face (&optional last-face) + "Generates a random cube face + +If supplied, LAST-FACE will be excluded as a possibility." + (let ((faces (cube-filter cube-faces last-face))) + (elt faces (random (length faces))))) + +(defun cube-random-turn (face) + "Generates a random turn directio for a given face + +FACE is a character representing the face to be turned." + (let ((suffix (pcase (random 3) + (0 "") + (1 "'") + (2 "2")))) + (format "%c%s" face suffix))) + +(defun cube-filter (str char) + "Filters all instances of a character from a string" + (seq-filter (lambda (c) (not (equal c char))) str))