-
Notifications
You must be signed in to change notification settings - Fork 11
/
levels_db.go
74 lines (67 loc) · 1.33 KB
/
levels_db.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"bufio"
"io"
"os"
"strings"
)
const game_data_file = "sokoban_levels.txt"
type ldb struct {
maxLevel int
data [][]string
}
// convert strings to 2-d matrix with bytes
func (db *ldb) getLevel(l int) [][]Cell {
board := make([][]Cell, len(db.data[l-1]))
for i, str := range db.data[l-1] {
board[i] = make([]Cell, len(str))
for j := 0; j < len(str); j++ {
var c Cell
switch str[j] {
case WALL:
c = Cell{base: WALL, obj: WALL}
case FLOOR:
c = Cell{base: FLOOR, obj: FLOOR}
case GUY:
c = Cell{base: FLOOR, obj: GUY}
case SLOT:
c = Cell{base: SLOT, obj: SLOT}
case BOX:
c = Cell{base: FLOOR, obj: BOX}
case BOXIN:
c = Cell{base: SLOT, obj: BOX}
}
board[i][j] = c
}
}
return board
}
// Read game database from .txt
func (db *ldb) loadAll() {
f, err := os.Open(game_data_file)
if err != nil {
panic(err)
}
rd := bufio.NewReader(f)
var l = 0
var matrix = make([]string, 0)
for {
line, err := rd.ReadString('\n')
if err != nil {
if err == io.EOF {
db.data = append(db.data, matrix)
db.maxLevel = len(db.data)
return
}
panic(err)
}
line = strings.TrimRight(line, "\t\n\f\r")
if len(line) == 0 {
db.data = append(db.data, matrix)
l = l + 1
matrix = make([]string, 0)
} else {
matrix = append(matrix, line)
}
}
}