add file loading function
This commit is contained in:
parent
d9d7a6ab49
commit
0a8c5f301d
2
gui.rkt
2
gui.rkt
|
@ -68,5 +68,5 @@
|
||||||
[label (format "Final Score: R: ~a, B: ~a" (first (GameState-scores game)) (last (GameState-scores game)))]))
|
[label (format "Final Score: R: ~a, B: ~a" (first (GameState-scores game)) (last (GameState-scores game)))]))
|
||||||
(send frame show #t))
|
(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)
|
(analysis-gui g)
|
23
main.rkt
23
main.rkt
|
@ -17,7 +17,7 @@
|
||||||
|
|
||||||
; represents the state of a game in play
|
; represents the state of a game in play
|
||||||
; grid is the grid
|
; 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.
|
; 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
|
; players is a list of two players who are playing in the game
|
||||||
(struct GameState (grid player scores players) #:transparent)
|
(struct GameState (grid player scores players) #:transparent)
|
||||||
|
@ -255,4 +255,23 @@
|
||||||
(lines
|
(lines
|
||||||
(unquote (for/list ([l (grid-lines g)])
|
(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)
|
(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)
|
||||||
|
))))
|
||||||
|
|
Loading…
Reference in New Issue