Now loading… please wait.
As a primer in lisp I had did a few of the Advent of Code problem last year. They all required reading from an input. Much like load my level as part of step for in Building a Roguelike.
Code is pretty simple..
Reading from a file.
The code is simple and similar to every other language. This code will also split on each line lines.
I'm using the as the second parameter in a mapcar
, so I can iterate over each character and add walls as needed.
#+NAME Read from a file into
(defun sre/read-lines (filepath) "Return each line of file at FILEPATH and remove empty lines." (with-temp-buffer (insert-file-contents filepath) (remove-if 'null (split-string (buffer-string) "\n" t))))
This turned out nice. The map file is an actual visible map.
#+NAME Map example
##### #.#.# #...# #####
Would be nice to work out how to get artist-mode to draw different characters.
For the configuration/state stuff i'll just put straight up lists. There is a eval
in emacs lisp that can just read it directly in.
#+NAME Parse lisp data/code from a file
(defun rl/load-state (filepath) "Return the contents of FILEPATH as a lisp object." (let ((content (with-temp-buffer (insert-file-contents filepath) (buffer-string)))) (read content)))
Reading to a file.
Writing and object looks simple enough. Just use the write-region with a format and path name.
#+NAME Write lisp object to file
(write-region (format "%s" state) nil filepath)