Jump to content

Langton's ant: Difference between revisions

→‎{{header|D}}: Add Common Lisp
(→‎{{header|D}}: Add Common Lisp)
Line 702:
 
 
=={{header|Common Lisp}}==
<lang lisp>(defmacro toggle (gv) `(setf ,gv (not ,gv)))
 
(defun langtons-ant (width height start-x start-y start-dir)
(let ( (grid (make-array (list width height)))
(x start-x)
(y start-y)
(dir start-dir) )
(loop while (and (< -1 x width) (< -1 y height)) do
(if (toggle (aref grid x y))
(setq dir (mod (1+ dir) 4))
(setq dir (mod (1- dir) 4)))
(case dir
(0 (decf y))
(1 (incf x))
(2 (incf y))
(3 (decf x)))
)
(dotimes (y height)
(dotimes (x width)
(princ (if (aref grid x y) "#" ".")))
(princ #\Newline))
)
)
 
(setf *random-state* (make-random-state t))
(langtons-ant 100 100 (+ 45 (random 10)) (+ 45 (random 10)) (random 4))</lang>
=={{header|D}}==
{{incorrect|D|The chirality of the output is reversed, meaning the ant is turning left when the spec says it should turn right, and vice-versa.}}
1,481

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.