parse from file and ignore empty lines

This commit is contained in:
Peter Tillemans 2024-05-19 19:00:40 +02:00
parent dffd74cb8c
commit 484ef0ed56
2 changed files with 22 additions and 15 deletions

View file

@ -18,7 +18,7 @@ W H W
W H W W H W
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 W H W
W H W - W H W -

View file

@ -1,6 +1,7 @@
(define-module (game level) (define-module (game level)
#:use-module (ice-9 match) #:use-module (ice-9 match)
#:use-module (ice-9 pretty-print) #:use-module (ice-9 pretty-print)
#:use-module (ice-9 textual-ports)
#:use-module (srfi srfi-1) #:use-module (srfi srfi-1)
#:use-module (srfi srfi-9) #:use-module (srfi srfi-9)
#:use-module (srfi srfi-64) #:use-module (srfi srfi-64)
@ -36,12 +37,14 @@
(else 'empty))) (else 'empty)))
(define (content->lines content) (define (content->lines content)
(map (filter (lambda (line) (not (string-null? line)))
(lambda (line) (map
(string-trim-right (lambda (line)
(car (string-split line #\-)) (string-trim-right
#\space)) (car (string-split line #\-))
(string-split content #\newline))) #\space))
(string-split content #\newline))))
(define (parse-lines lines) (define (parse-lines lines)
(fold append (fold append
@ -52,12 +55,7 @@
lines))) lines)))
(define (parse-level content) (define (parse-level content)
(let* ((lines (map (let* ((lines (content->lines content))
(lambda (line)
(string-trim-right
(car (string-split line #\-))
#\space))
(string-split content #\newline)))
(width (string-length (car lines))) (width (string-length (car lines)))
(height (length lines)) (height (length lines))
(tiles (list->vector (tiles (list->vector
@ -65,11 +63,15 @@
(entities '())) (entities '()))
(%make-level width height tiles #f #f 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-begin "level")
(test-assert (level? (parse-level "WWWWW\nWP GW\nWWWWW\n"))) (test-assert (level? (parse-level "WWWWW\nWP GW\nWWWWW\n")))
(test-assert (level-width (parse-level "WWWWW\nWP GW\nWWWWW\n")) 5) (test-equal (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-height (parse-level "WWWWW\nWP GW\nWWWWW\n")) 3)
(test-equal (level-tiles (parse-level "W")) #(wall)) (test-equal (level-tiles (parse-level "W")) #(wall))
(test-equal (test-equal
(level-tiles (parse-level "WWWWW\nWP GW\nWWWWW\n")) (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")) (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)) #(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 #\W) 'wall)
(test-equal (parse-tile #\B) 'brick) (test-equal (parse-tile #\B) 'brick)
(test-equal (parse-tile #\H) 'ladder) (test-equal (parse-tile #\H) 'ladder)
@ -86,4 +90,7 @@
(test-equal (parse-tile #\G) 'goal) (test-equal (parse-tile #\G) 'goal)
(test-equal (parse-tile #\space) 'empty) (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") (test-end "level")