-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
245 lines (217 loc) · 5.07 KB
/
main.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package main
import (
"bufio"
"bytes"
"embed"
"errors"
"flag"
"fmt"
"log"
"net/http"
"strconv"
"strings"
termbox "github.com/nsf/termbox-go"
)
const (
pose int = iota
continuing
quit
win
lose
chPlayer = 'P'
chHunter = 'H'
chGhost = 'G'
chApple = 'o'
chSpace = ' '
chPoison = 'X'
chBoundary = '+'
chObstacle1 = '-'
chObstacle2 = '|'
chObstacle3 = '!'
sceneStart = "files/scene/start.txt"
sceneYouwin = "files/scene/youwin.txt"
sceneYoulose = "files/scene/youlose.txt"
sceneGoodbye = "files/scene/goodbye.txt"
)
//go:embed files
var static embed.FS
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
stages := initStages()
if err := validateFiles(stages); err != nil {
return err
}
level := flag.Int("level", stages[0].level, "Level at the start of the game.")
life := flag.Int("life", 2, "Remaining lives.")
flag.Parse()
stages = splitStages(stages, level)
if err := termbox.Init(); err != nil {
return err
}
if err := termbox.Clear(termbox.ColorWhite, termbox.ColorBlack); err != nil {
return err
}
defer termbox.Close()
if err := switchScene(sceneStart); err != nil {
return err
}
i := 0
game:
for i < len(stages) && *life >= 0 {
p := new(player)
if err := stages[i].init(p, *life); err != nil {
return err
}
standBy(p)
if err := stages[i].start(p); err != nil {
return err
}
switch p.state {
case win:
if err := switchScene(sceneYouwin); err != nil {
return err
}
i++
case lose:
if err := switchScene(sceneYoulose); err != nil {
return err
}
*life--
case quit:
break game
}
}
if err := switchScene(sceneGoodbye); err != nil {
return err
}
return nil
}
func splitStages(stages []stage, level *int) []stage {
for i, stage := range stages {
if *level == stage.level {
return stages[i:]
}
}
return stages
}
func standBy(p *player) {
p.state = pose
for {
ev := termbox.PollEvent()
if ev.Key == termbox.KeyEnter {
p.state = continuing
break
}
if ev.Ch == 'q' {
p.state = quit
break
}
}
}
var (
mimeTypeValidationError = errors.New("MIME Type Validation Error")
fileSizeValidationError = errors.New("File Size Validation Error")
stageMapValidationError = errors.New("Stage Map Validation Error")
)
const (
stageMapMimeType = "text/plain; charset=utf-8"
maxFileSize = 1024
maxStageMapWidth = 50
maxStageMapHeight = 20
)
func validateFiles(stages []stage) error {
for _, s := range stages {
b, err := static.ReadFile(s.mapPath)
if err != nil {
return err
}
if err := validateMimeType(b, s.mapPath); err != nil {
return err
}
if err := validateStage(b, s.mapPath); err != nil {
return err
}
}
return nil
}
func validateMimeType(b []byte, filePath string) error {
mimeType := http.DetectContentType(b)
if mimeType != stageMapMimeType {
err := errors.New(filePath + "; Invalid mime type: " + mimeType + ";")
return fmt.Errorf("%w: %+v", mimeTypeValidationError, err)
}
return nil
}
func validateStage(b []byte, filePath string) error {
lines, err := validateStageSize(b, filePath)
if err != nil {
return err
}
if err := validateStageShape(filePath, lines); err != nil {
return err
}
if err := validateStageBoundary(filePath, lines); err != nil {
return err
}
return nil
}
func validateStageSize(b []byte, filePath string) ([]string, error) {
f := bytes.NewReader(b)
scanner := bufio.NewScanner(f)
lines := []string{}
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if len(lines[0]) > maxStageMapWidth {
err := errors.New(filePath + "; Please keep the stage within 50 columns;")
return nil, fmt.Errorf("%w: %+v", stageMapValidationError, err)
}
if len(lines) > maxStageMapHeight {
err := errors.New(filePath + "; Please keep the stage within 20 lines;")
return nil, fmt.Errorf("%w: %+v", stageMapValidationError, err)
}
return lines, nil
}
func validateStageShape(filePath string, lines []string) error {
width := len(lines[0])
lineNo := 1
errLineNo := []string{}
for _, line := range lines {
if len(line) != width {
errLineNo = append(errLineNo, strconv.Itoa(lineNo))
}
lineNo++
}
if len(errLineNo) > 0 {
err := errors.New(filePath + "; Make the width of the stage map uniform (line " + strings.Join(errLineNo, ",") + ");")
return fmt.Errorf("%w: %+v", stageMapValidationError, err)
}
return nil
}
func validateStageBoundary(filePath string, lines []string) error {
width := len(lines[0])
height := len(lines)
lineNo := 1
errLineNo := []string{}
for _, s := range lines {
if lineNo == 1 || lineNo == height {
if s != strings.Repeat(string(chBoundary), width) {
errLineNo = append(errLineNo, strconv.Itoa(lineNo))
}
} else {
if !strings.HasPrefix(s, string(chBoundary)) || !strings.HasSuffix(s, string(chBoundary)) {
errLineNo = append(errLineNo, strconv.Itoa(lineNo))
}
}
lineNo++
}
if len(errLineNo) > 0 {
err := errors.New(filePath + "; Create a boundary for the stage map with '+' (line " + strings.Join(errLineNo, ",") + ");")
return fmt.Errorf("%w: %+v", stageMapValidationError, err)
}
return nil
}