-
Notifications
You must be signed in to change notification settings - Fork 0
/
dumpgame.go
60 lines (48 loc) · 984 Bytes
/
dumpgame.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"encoding/gob"
"fmt"
"io"
"os"
)
const dumpFilename = "trek.dump"
var dumpTemplate = []interface{}{
&ship, &now, ¶m, &etc, &game, §, &quad, &move, &eventList,
}
func dumpgame(v int) {
f, err := os.OpenFile(dumpFilename, os.O_WRONLY, 0644)
if err != nil {
fmt.Printf("cannot dump: %s\n", err.Error())
return
}
defer f.Close()
enc := gob.NewEncoder(f)
for _, data := range dumpTemplate {
if err := enc.Encode(data); err != nil {
fmt.Printf("cannot encode dump: %s\n", err.Error())
return
}
}
}
func restartgame() bool {
f, err := os.Open(dumpFilename)
if err != nil {
fmt.Printf("cannot restart: %s\n", err.Error())
return false
}
defer f.Close()
if !readdump(f) {
fmt.Printf("cannot restart\n")
return false
}
return true
}
func readdump(f io.Reader) bool {
dec := gob.NewDecoder(f)
for _, data := range dumpTemplate {
if err := dec.Decode(data); err != nil {
return false
}
}
return true
}