parse from file and ignore empty lines
This commit is contained in:
parent
dffd74cb8c
commit
484ef0ed56
2 changed files with 22 additions and 15 deletions
|
@ -18,7 +18,7 @@ W H W
|
|||
W H W
|
||||
W H W -
|
||||
W H W
|
||||
W P HBBBBBBB W
|
||||
W P BHBBBBBBB W
|
||||
W H W
|
||||
W H W
|
||||
W H W -
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
(define-module (game level)
|
||||
#:use-module (ice-9 match)
|
||||
#:use-module (ice-9 pretty-print)
|
||||
#:use-module (ice-9 textual-ports)
|
||||
#:use-module (srfi srfi-1)
|
||||
#:use-module (srfi srfi-9)
|
||||
#:use-module (srfi srfi-64)
|
||||
|
@ -36,12 +37,14 @@
|
|||
(else 'empty)))
|
||||
|
||||
(define (content->lines content)
|
||||
(map
|
||||
(lambda (line)
|
||||
(string-trim-right
|
||||
(car (string-split line #\-))
|
||||
#\space))
|
||||
(string-split content #\newline)))
|
||||
(filter (lambda (line) (not (string-null? line)))
|
||||
(map
|
||||
(lambda (line)
|
||||
(string-trim-right
|
||||
(car (string-split line #\-))
|
||||
#\space))
|
||||
(string-split content #\newline))))
|
||||
|
||||
|
||||
(define (parse-lines lines)
|
||||
(fold append
|
||||
|
@ -52,12 +55,7 @@
|
|||
lines)))
|
||||
|
||||
(define (parse-level content)
|
||||
(let* ((lines (map
|
||||
(lambda (line)
|
||||
(string-trim-right
|
||||
(car (string-split line #\-))
|
||||
#\space))
|
||||
(string-split content #\newline)))
|
||||
(let* ((lines (content->lines content))
|
||||
(width (string-length (car lines)))
|
||||
(height (length lines))
|
||||
(tiles (list->vector
|
||||
|
@ -65,11 +63,15 @@
|
|||
(entities '()))
|
||||
(%make-level width height tiles #f #f entities)))
|
||||
|
||||
(define (parse-level-file filename)
|
||||
(parse-level (call-with-input-file filename get-string-all)))
|
||||
|
||||
(test-begin "level")
|
||||
|
||||
|
||||
(test-assert (level? (parse-level "WWWWW\nWP GW\nWWWWW\n")))
|
||||
(test-assert (level-width (parse-level "WWWWW\nWP GW\nWWWWW\n")) 5)
|
||||
(test-assert (level-height (parse-level "WWWWW\nWP GW\nWWWWW\n")) 3)
|
||||
(test-equal (level-width (parse-level "WWWWW\nWP GW\nWWWWW\n")) 5)
|
||||
(test-equal (level-height (parse-level "WWWWW\nWP GW\nWWWWW\n")) 3)
|
||||
(test-equal (level-tiles (parse-level "W")) #(wall))
|
||||
(test-equal
|
||||
(level-tiles (parse-level "WWWWW\nWP GW\nWWWWW\n"))
|
||||
|
@ -78,6 +80,8 @@
|
|||
(level-tiles (parse-level "--- ignore this line\nWWWWW\nWP GW - must be ignored\nWWWWW\n"))
|
||||
#(wall wall wall wall wall wall player empty goal wall wall wall wall wall wall))
|
||||
|
||||
(test-equal (content->lines "WWWWW\nWP GW\nWWWWW\n") '("WWWWW" "WP GW" "WWWWW"))
|
||||
|
||||
(test-equal (parse-tile #\W) 'wall)
|
||||
(test-equal (parse-tile #\B) 'brick)
|
||||
(test-equal (parse-tile #\H) 'ladder)
|
||||
|
@ -86,4 +90,7 @@
|
|||
(test-equal (parse-tile #\G) 'goal)
|
||||
(test-equal (parse-tile #\space) 'empty)
|
||||
|
||||
(test-equal (level-width (parse-level-file "assets/levels/level-1.map")) 40)
|
||||
(test-equal (level-height (parse-level-file "assets/levels/level-1.map")) 24)
|
||||
|
||||
(test-end "level")
|
||||
|
|
Loading…
Reference in a new issue