42 lines
1.3 KiB
EmacsLisp
42 lines
1.3 KiB
EmacsLisp
(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))
|