This repository has been archived by the owner on Jul 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.go
340 lines (280 loc) · 7.05 KB
/
map.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package main
import (
"errors"
"fmt"
"log"
"math/rand"
"strconv"
"strings"
"github.com/google/logger"
)
type block int
const (
blockClear = 0 // _
blockWall = iota // X
blockSnake = iota // *
blockSnakeHead = iota // H
blockFood = iota // ^
)
const (
blockClearChar = '_'
blockWallChar = 'X'
blockSnakeChar = '*'
blockSnakeHeadChar = 'H'
blockFoodChar = '^'
)
/*
GameMap is a small struct for maps passed as a "2d string"
*/
type GameMap struct {
// The "simple map"
SizeX int
SizeY int
Content [][]block
// Some lists if you would rather use them for the parsing
Heads []Head
Walls []Wall // This contains also the rest of the snake blocks
Foods []Food
}
func (gm *GameMap) getTile(x int, y int) (block, error) {
if x < 0 || x >= gm.SizeX {
return blockClear, errors.New("X out of bounds")
}
if y < 0 || y >= gm.SizeY {
return blockClear, errors.New("Y out of bounds")
}
return gm.Content[y][x], nil
}
func (gm *GameMap) setTileLine(startX int, startY int, deltaX int, deltaY int, value block, iterations int) {
x := startX
y := startY
for iter := 0; iter < iterations; iter++ {
err := gm.setTile(x, y, value)
if err != nil {
logger.Info("Hit outside bound, stopping line")
return
}
x += deltaX
y += deltaY
}
}
func (gm *GameMap) setTile(x int, y int, value block) error {
if x < 0 || x >= gm.SizeX {
return errors.New("X out of bounds")
}
if y < 0 || y >= gm.SizeY {
return errors.New("Y out of bounds")
}
switch value {
case blockWall:
gm.Walls = append(gm.Walls, Wall{x, y})
case blockSnakeHead:
gm.Heads = append(gm.Heads, Head{x, y})
case blockFood:
gm.Foods = append(gm.Foods, Food{x, y})
}
gm.Content[y][x] = value
return nil
}
func (b block) MarshalText() (text []byte, err error) {
val, err := b.toRune()
if err != nil {
return []byte(""), err
}
return []byte(strconv.QuoteRuneToASCII(val)), nil
}
// Returns an rune from an block. return space and an error if not possible
func (b *block) toRune() (rune, error) {
switch *b {
case blockClear:
return blockClearChar, nil
case blockWall:
return blockWallChar, nil
case blockSnake:
return blockSnakeChar, nil
case blockSnakeHead:
return blockSnakeHeadChar, nil
case blockFood:
return blockFoodChar, nil
}
return rune(-1), errors.New("unable to convert block to rune")
}
// Creates a block from a rune. return an error and -1 if not possible
func toBlock(r rune) (block, error) {
switch r {
case blockClearChar:
return blockClear, nil
case blockWallChar:
return blockWall, nil
case blockSnakeChar:
return blockSnake, nil
case blockSnakeHeadChar:
return blockSnakeHead, nil
case blockFoodChar:
return blockFood, nil
}
return -1, errors.New("unable to convert rune to block")
}
func (gm *GameMap) findCoordInList(c *coord, list []coord) (int, error) {
for index, t := range list {
if t.X == c.X && t.Y == c.Y {
return index, nil
}
}
return -1, errors.New("could not find coord in list")
}
func (gm *GameMap) getAllEmpty(blockType block) ([]int, []int, error) {
listX := make([]int, 0)
listY := make([]int, 0)
for indexY := range gm.Content {
yLine := gm.Content[indexY]
for indexX, xBlock := range yLine {
if xBlock != blockType {
continue
}
listX = append(listX, indexX)
listY = append(listY, indexY)
}
}
if len(listX) == 0 {
return nil, nil, errors.New("no empty tiles left")
}
return listX, listY, nil
}
//
// Finds an empty spot which can be used for placing down objects
// The fair bool value exists for trying to place the objects some part away from the snakes head
// Note that fair does not do anything yet!
// Returns the x,y coordinates for a empty spot
func (gm *GameMap) findEmptySpot() (int, int, error) {
listX, listY, err := gm.getAllEmpty(blockClear)
if err != nil {
return -1, -1, err
}
if len(listX) != len(listY) {
return -1, -1, errors.New("invalid length on the two lists")
}
element := rand.Intn(len(listX))
return listX[element], listY[element], nil
}
func baseGameMap(sizeX int, sizeY int, walls int) GameMap {
logger.Infof("Generating map with size: %v,%v", sizeX, sizeY)
blankMap := fmt.Sprintf("%v,%v\n", sizeX, sizeY)
for i := 1; i < sizeY; i++ {
blankMap = blankMap + strings.Repeat("_", sizeX) + "\n"
}
gm := mapFromString(blankMap)
if walls == 0 {
return gm
}
// Top
gm.setTileLine(0, 0, 1, 0, blockWall, sizeX)
// Bottom
gm.setTileLine(0, sizeY-1, 1, 0, blockWall, sizeX)
// Left
gm.setTileLine(0, 0, 0, 1, blockWall, sizeY)
// Right
gm.setTileLine(sizeX-1, 0, 0, 1, blockWall, sizeY)
return gm
}
func baseGameMapSize(numberPlayers int) int {
return numberPlayers*2 + 10
}
/* mapFromString takes in an map in the form of x,y and then y lines with x length denoting the map.
The map is denoted with the chars:
_ : May walk on
X : Wall. Blocked
* : Fuel
^ : Bullet
Note that it is also implicit that out of bound are walls.
Example map:
---Star---
6,6
X____X
__XX__
_XXXX_
______
XX_XX_
XX____
---End---
*/
func mapFromString(mapInput string) GameMap {
logger.Info("Parsing map")
defer logger.Info("Map parsed")
lines := strings.Split(mapInput, "\n")
size := strings.Split(lines[0], ",")
sizeX, err := strconv.Atoi(size[0])
if err != nil {
log.Fatal("Got invalid map for the X")
}
sizeY, err := strconv.Atoi(size[1])
if err != nil {
log.Fatal("Got invalid map for the Y")
}
if sizeY != len(lines)-1 {
log.Fatalf("Mismatch between size Y of the map and the number given. SizeY: %v len(lines): %v", sizeY, len(lines))
}
if len(lines[1]) != sizeX {
log.Fatal("Mismatch between size of X and the size of the first line of the map")
}
gm := GameMap{
Content: nil,
}
content := make([][]block, sizeY)
for index, line := range lines[1:] {
contentLine := make([]block, sizeX)
for index, char := range line {
switch char {
case blockClearChar:
contentLine[index] = blockClear
case blockWallChar:
contentLine[index] = blockWall
case blockSnakeChar:
contentLine[index] = blockSnake
case blockSnakeHead:
contentLine[index] = blockSnakeHead
case blockFoodChar:
contentLine[index] = blockFood
default:
log.Panicf("Found invalid char: '%c'", char)
}
}
content[index] = contentLine
}
gm.Content = content
gm.SizeX = sizeX
gm.SizeY = sizeY
return gm
}
func (gm *GameMap) removeFood(p *Player) {
x := p.next.X
y := p.next.Y
index := 0
for _, t := range gm.Foods {
if t.X == x && t.Y == y {
break
}
index++
}
gm.Foods[len(gm.Foods)-1], gm.Foods[index] = gm.Foods[index], gm.Foods[len(gm.Foods)-1]
gm.Foods = gm.Foods[:len(gm.Foods)-1]
err := gm.setTile(x, y, blockClear)
if err != nil {
logger.Info("Was unable to remove food tile")
}
}
func (gm *GameMap) spreadFood(targetAmount int) {
food := len(gm.Foods)
for food < targetAmount {
x, y, err := gm.findEmptySpot()
logger.Infof("Food pos: %v %v", x, y)
if err != nil {
panic("Could not init round, not enough space to start new round")
}
food += 1
err = gm.setTile(x, y, blockFood)
if err != nil {
panic("Could not init round, not enough space to start new round")
}
}
}