Compare commits

...

2 Commits

Author SHA1 Message Date
Nico c62d8a1653 Merge branch 'main' of tildegit.org:nihilazo/dotsandboxes 2022-04-07 17:53:56 +01:00
Nico 0a8c5f301d add file loading function 2022-04-07 17:47:46 +01:00
2 changed files with 23 additions and 6 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)
@ -238,7 +238,6 @@
; GameState path !
; saves a game into the .dbn file format that I just made up.
; don't worry, it's sexpy.
; TODO write loader
(define (save-game! s filename)
(let ([g (GameState-grid s)]
[f (open-output-file filename)])
@ -248,6 +247,24 @@
(grid ,(grid-width g) ,(grid-height g))
(score ,(first (GameState-scores s)) ,(second (GameState-scores s)))
(lines
,(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)))
,((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)))
; path -> GameState
; loads a game from a .dbn file into a GameState structure
(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)
))))