我在计划中创建国际象棋游戏,但不知道如何保存和加载游戏状态是我拥有此代码的一部分
(define-struct piece(color nove? type ))
;;tablero lista de listas de fichas
(define-struct estado (board turn fichaSel))
(define bpawn (bitmap "b-peon.png"))
(define brook (bitmap "b-torre.png"))
(define bcaballo (bitmap "b-caballo.png"))
(define bbish (bitmap "b-arfil.png"))
(define bquee (bitmap "b-reina.png"))
(define bking (bitmap "b-rey.png"))
(define wpawn (bitmap "w-peon.png"))
(define wrook (bitmap "w-torre.png"))
(define wcaballo (bitmap "w-caballo.png"))
(define wbish (bitmap "w-arfil.png"))
(define wquee (bitmap "w-reina.png"))
(define wking (bitmap "w-rey.png"))
(define board (bitmap "board.jpg"))
这是一个列表列表
(define board1 (list (list torreb caballob arfilb reinab reyb arfilb caballob torreb)
(list peonb peonb peonb peonb peonb peonb peonb peonb)
(list empty empty empty empty empty empty empty empty)
(list empty empty empty empty empty empty empty empty)
(list empty empty empty empty empty empty empty empty)
(list empty empty empty empty empty empty empty empty)
(list pawnw pawnw pawnw pawnw pawnw pawnw pawnw pawnw)
(list rookw knightw bishopw queenw kingw bishopw knightw rookw)))
我这样做是为了保存游戏状态:
(define (save file)
(write-file (string-append Subcarpeta archivo ".txt")
"game state" ))
但是当你将游戏状态插入“游戏状态”让我保存游戏时
我怎么能这样做?
答案 0 :(得分:0)
在操作系统和其他编程语言中称为文件句柄的Scheme和Racket中的终端都是端口。
当然你有lots of possibilities但是使用lambda语言的初级到中级学生都不支持这一点。使用高级学生,您可以选择以下选项:
#!htdp/asl
(with-output-to-file "file.rc"
(lambda () (write '(your data structure))))
(with-input-from-file "file.rc"
(lambda () (read))) ; ==> (your data structure)
问题在于,如果文件存在,您将收到错误消息。使用#!racket
,您可以添加标记来克服这个问题:
#!racket
(with-output-to-file "file.rc"
(lambda () (write '(your data structure)))
#:mode 'text
#:exists 'truncate)
(with-input-from-file "file.rc"
(lambda () (read))) ; ==> (your data structure)