;;; cube-scrambler.el --- Rubik's cube scrambler ;; Copyright (C) Jonathan Lamothe ;; Author: Jonathan Lamothe ;; Package-Version: 0.1.1 ;; This file is not part of GNU Emacs. ;; This program is free software: you can redistribute it and/or ;; modify it under the terms of the GNU Affero General Public License ;; as published by the Free Software Foundation, either version 3 of ;; the License, or (at your option) any later version. ;; This program is distributed in the hope that it will be useful, but ;; WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ;; Affero General Public License for more details. ;; You should have received a copy of the GNU Affero General Public ;; License along with this program. If not, see ;; . ;;; Code: (defvar cube-faces "UDLRFB") (defun cube-scramble (moves) "Generate a list of random moves for a 3x3 Rubik's cube and display it MOVES represents the number of moves in the list." (interactive "p") (message (mapconcat 'identity (cube-build-scramble moves) " "))) (defun cube-build-scramble (moves) "Generate a list of random moves for a 3x3 Rubik's cube MOVES is 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) "Generate 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) "Generate a random turn direction 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 (sequence value) "Filter all instances of a value from a sequence" (seq-filter (lambda (c) (not (equal c value))) sequence)) ;;; cube-scrambler.el ends here