Skip to content

Instantly share code, notes, and snippets.

@crertel
Created May 21, 2019 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crertel/91b538980f5c3003cf22e53cb430fa14 to your computer and use it in GitHub Desktop.
Save crertel/91b538980f5c3003cf22e53cb430fa14 to your computer and use it in GitHub Desktop.
Adventures in emacs-lisp doing a mastermind game
(require 'dash)
(setq kColors '( :red :green :blue :cyan :orange :yellow))
(defun make-random-color
(x)
"Picks a random color."
( nth (random ( length kColors) ) kColors ))
(defun make-sequence
(n)
"Creates a color sequence of length n"
(
mapcar 'make-random-color (number-sequence 1 n 1)
)
)
(defun score-whites
(key guess)
"Scores misplaced correct colors."
(
mapcar (lambda (l) (member l key))
guess
)
)
(defun score-game
(key guess)
"Scores a guess using an answer color key."
(
mapcar (lambda(l)(let
((is_whites (car l)) (key (cadr l)) (guess (caddr l)))
(
cond
( (equal key guess) :black)
( (not is_whites) :none)
( t :white)
)
)
)
(-zip (score-whites key guess) key guess)
)
)
;; test all good
(
let ((guess '(:red :red :red :red))
(key '(:red :red :red :red))
)
(
score-game guess key
)
)
;; test all bad
(
let ((guess '(:blue :blue :blue :blue))
(key '(:red :red :red :red))
)
(
score-game guess key
)
)
;; right colors wrong order
(
let ((guess '(:cyan :blue :green :red))
(key '(:red :green :blue :cyan))
)
(
score-game guess key
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment