add file loading function

This commit is contained in:
Nico 2022-04-07 17:47:46 +01:00
parent d9d7a6ab49
commit 0a8c5f301d
2 changed files with 22 additions and 3 deletions

View File

@ -68,5 +68,5 @@
[label (format "Final Score: R: ~a, B: ~a" (first (GameState-scores game)) (last (GameState-scores game)))]))
(send frame show #t))
(define g (play-game (GameState (grid '() 6 6) 0 '(0 0) (list random-player random-player))))
(define g (load-game! "/tmp/test.dbn"))
(analysis-gui g)

View File

@ -17,7 +17,7 @@
; represents the state of a game in play
; grid is the grid
; player is the current player
; player is the current player (ignored when the game is over)
; scores is a list of numbers for the score of each player.
; players is a list of two players who are playing in the game
(struct GameState (grid player scores players) #:transparent)
@ -255,4 +255,23 @@
(lines
(unquote (for/list ([l (grid-lines g)])
(list (point-x (line-from l)) (point-y (line-from l)) (point-x (line-to l)) (point-y (line-to l)) (line-player l))))))) f)
(close-output-port f)))
(close-output-port f)))
; path -> GameState
; loads a game from a .dbn file into a GameState structure
; TODO lines, scores, etc
(define (load-game! p)
(let* ([f (open-input-file p)]
[data (read f)])
(close-input-port f)
(GameState
(grid
(for/list ([i (last (assoc 'lines data))])
(line (point (first i) (second i)) (point (third i) (fourth i)) (fifth i)))
(second (assoc 'grid data)) (last (assoc 'grid data)))
0 ; assumed, the game is over
(list (second (assoc 'score data)) (last (assoc 'score data)))
`(
,(player (last (assoc 'player0 data)) 0)
,(player (last (assoc 'player1 data)) 0)
))))