69 lines
2.2 KiB
EmacsLisp
69 lines
2.2 KiB
EmacsLisp
;;; cube-scrambler.el --- Rubik's cube scrambler
|
|
|
|
;; Copyright (C) Jonathan Lamothe
|
|
|
|
;; Author: Jonathan Lamothe <jonathan@jlamothe.net>
|
|
;; Package-Version: 0.0
|
|
|
|
;; 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
|
|
;; <https://www.gnu.org/licenses/>.
|
|
|
|
;;; Code:
|
|
|
|
(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))
|
|
|
|
;;; cube-scrambler.el ends here
|