-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
543 lines (487 loc) · 12.1 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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
// SPDX-License-Identifier: Apache-2.0
// Copyright 2020,2021 Marcus Soll
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// sl_ow is a solution for the InformatiCup2021 by Marcus Soll
// It works by randomly simulating games and choosing the best action.
// It is based on a heavily modified server.
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"math/rand"
"net/http"
"net/url"
"os"
"runtime"
"runtime/pprof"
"sort"
"strings"
"time"
"github.com/gorilla/websocket"
)
// GameData holds the metadata of a round.
type GameData struct {
Alive bool
Collect map[string]struct {
Run int
Won int
Survived int
SurvivdedOpponent int
Round int
SurvivedList []uint16
LongestOpponent int
}
LongestWin int
LongestWinAction string
Longest int
LongestAction string
Action string
Reason string
Game *Game
Round int
Jumps int
Runtime time.Duration
}
func main() {
rand.Seed(time.Now().UnixNano())
endpoint := flag.String("api", "wss://msoll.de/spe_ed", "API Endpoint")
key := flag.String("key", "KEY", "API key")
quiet := flag.Bool("quiet", false, "Only print result")
maxDurationString := flag.String("max", "", "Max computation time in seconds. 0 or empty string disables max time. Must be parseable as time.Duration")
profile := flag.String("profile", "", "Profile program to file")
print := flag.String("print", "", "Prints output into file")
showui := flag.Bool("ui", false, "Enables cmd ui")
dump := flag.String("dump", "", "Dumps game data as gob to file")
printWin := flag.String("printwin", "", "Prints outcome of the game as a simple \"Win/Loss\" into file")
flag.Parse()
// Replace flags
{
env := os.Getenv("URL")
if env != "" {
fmt.Println("Using URL from env:", env)
*endpoint = env
}
env = os.Getenv("KEY")
if env != "" {
fmt.Println("Using KEY from env:", env)
*key = env
}
}
// Max Duration
var maxDuration time.Duration
if *maxDurationString != "" {
var err error
maxDuration, err = time.ParseDuration(*maxDurationString)
if err != nil {
panic(err)
}
if maxDuration != 0 && maxDuration < 500*time.Millisecond {
panic(fmt.Errorf("max must be at least 500ms (is %s) or else simulations can not run", maxDuration.String()))
}
}
var UI UI
if *quiet {
UI = quietUI{}
} else if *showui {
UI = new(terminalUI)
} else {
UI = cmdUI{}
}
defer func() {
err := recover()
if err != nil {
// Clearly close UI
if UI != nil {
UI.Finish(false, -1, -1)
UI.Wait()
}
fmt.Fprintln(os.Stderr, err)
}
}()
if *profile != "" {
f, err := os.Create(*profile)
if err != nil {
log.Panicln(err)
}
defer f.Close()
err = pprof.StartCPUProfile(f)
if err != nil {
log.Panicln(err)
}
defer pprof.StopCPUProfile()
}
if *print != "" {
UI = &teeUI{File: *print, UI: UI}
}
if *dump != "" {
UI = &dumpUI{File: *dump, UI: UI}
}
if *printWin != "" {
UI = &printWinUI{File: *printWin, UI: UI}
}
url := fmt.Sprintf("%s?key=%s", *endpoint, url.QueryEscape(*key))
conn, _, err := websocket.DefaultDialer.Dial(url, http.Header{})
if err != nil {
log.Panicln(err)
}
err = UI.Initialise()
if err != nil {
panic(err)
}
var mastergame *Game
round := 0
lastAlive := 0
numberWorker := runtime.NumCPU()
jumpsObserved := 0
var start time.Time
for {
round++
_, b, err := conn.ReadMessage()
if err != nil {
log.Panicln(err)
}
if start.IsZero() {
start = time.Now()
}
mastergame = new(Game)
err = json.Unmarshal(b, mastergame)
if err != nil {
log.Panicln(err)
}
// Add round since that is not transmitted
for k := range mastergame.Players {
mastergame.Players[k].stepCounter = round - 1
}
if mastergame.Running == false || !mastergame.Players[mastergame.You].Active {
UI.NewData(GameData{
Alive: mastergame.Players[mastergame.You].Active,
Collect: make(map[string]struct {
Run int
Won int
Survived int
SurvivdedOpponent int
Round int
SurvivedList []uint16
LongestOpponent int
}),
LongestWin: 0,
LongestWinAction: "",
Longest: 0,
LongestAction: "",
Action: "nothing",
Reason: "",
Round: round,
Game: mastergame,
Jumps: jumpsObserved,
Runtime: time.Now().Sub(start),
})
}
if mastergame.Running == false {
break
}
mastergame.PopulateInternalCellsFlat()
UI.NewRound(mastergame.PublicCopy(), round)
if !mastergame.Players[mastergame.You].Active {
continue
}
lastAlive = round
deadline, err := time.Parse(time.RFC3339, mastergame.Deadline)
if err != nil {
log.Panicln(err)
}
if maxDuration > 0 {
test := time.Now().Add(maxDuration)
if test.Before(deadline) {
deadline = test
}
}
ctxWorker, ctxWorkerCancel := context.WithDeadline(context.Background(), deadline.Add(-500*time.Millisecond))
ctxMain, ctxMainCancel := context.WithDeadline(context.Background(), deadline.Add(-250*time.Millisecond))
results := make(chan struct {
action string
win bool
survived int
survivdedOpponent int
round int
}, numberWorker)
data := GameData{
Alive: true,
Collect: make(map[string]struct {
Run int
Won int
Survived int
SurvivdedOpponent int
Round int
SurvivedList []uint16
LongestOpponent int
}),
LongestWin: 0,
LongestWinAction: "",
Longest: 0,
LongestAction: "",
Action: "nothing",
Reason: "",
Round: round,
Game: mastergame,
}
for i := 0; i < numberWorker; i++ {
go func() {
for {
select {
case <-ctxWorker.Done():
return
default:
g := mastergame.PublicCopy()
test := []string{ActionTurnLeft, ActionTurnRight, ActionSlower, ActionFaster, ActionNOOP}[rand.Intn(5)]
g.SimulateGame(test, results)
}
}
}()
}
collectorWorker:
for {
select {
case r := <-results:
d := data.Collect[r.action]
d.Run++
if r.win {
d.Won++
if r.survived > data.LongestWin {
data.LongestWin = r.survived
data.LongestWinAction = r.action
}
}
if r.survived > data.Longest {
data.Longest = r.survived
data.LongestAction = r.action
}
if r.survivdedOpponent > d.LongestOpponent {
d.LongestOpponent = r.survivdedOpponent
}
d.Survived += r.survived
d.SurvivdedOpponent += r.survivdedOpponent
d.Round += r.round
d.SurvivedList = append(d.SurvivedList, uint16(r.survived))
data.Collect[r.action] = d
case <-ctxMain.Done():
break collectorWorker
}
}
ctxWorkerCancel()
ctxMainCancel()
// Sort survivedList
for k := range data.Collect {
d := data.Collect[k]
sort.Slice(d.SurvivedList, func(i, j int) bool { return i < j })
data.Collect[k] = d
}
best := 0.0
if data.Action == "nothing" {
for k := range data.Collect {
d := data.Collect[k]
if d.Run == 0 {
continue
}
winchance := float64(d.Won) / float64(d.Run)
if winchance > 0.85 && winchance > best {
data.Reason = "win > 85%"
data.Action = k
best = winchance
}
}
}
best = 0.0
if data.Action == "nothing" {
for k := range data.Collect {
d := data.Collect[k]
if d.Run == 0 {
continue
}
averageLength := float64(d.Survived) / float64(d.Run)
if averageLength > best && float64(d.Won)/float64(d.Run) > 0.1 {
best = averageLength
data.Action = k
data.Reason = "average length"
}
}
}
if data.Action == "nothing" {
// In case no win path is found
if data.LongestAction != "" {
data.Action = data.LongestAction
data.Reason = "longest path"
}
}
if data.Action == "nothing" {
data.Action = ActionNOOP
data.Reason = "fallback"
}
answer, err := json.Marshal(Action{data.Action})
if err != nil {
log.Panicln(err)
}
err = conn.WriteMessage(websocket.TextMessage, answer)
if err != nil {
log.Panicln(err)
}
if isJump(mastergame.PublicCopy(), data.Action) {
jumpsObserved++
}
data.Jumps = jumpsObserved
data.Runtime = time.Now().Sub(start)
UI.NewData(data)
}
UI.Finish(mastergame.Players[mastergame.You].Active, lastAlive, round)
UI.Wait()
}
func (g Game) String() string {
return g.PrintGame(false)
}
// PrintGame returns a string representation of the game cells.
func (g Game) PrintGame(colour bool) string {
var s strings.Builder
for y := 0; y < g.Height; y++ {
for x := 0; x < g.Width; x++ {
v := int(g.Cells[y][x])
r := rune(48 + v)
switch {
case v == 0:
r = '·'
case v > 0:
if v == g.You {
r = '●'
}
p := g.Players[v]
if p.X == x && p.Y == y {
switch p.Direction {
case DirectionUp:
r = '⮝'
if v == g.You {
r = '⮉'
}
case DirectionRight:
r = '⮞'
if v == g.You {
r = '⮊'
}
case DirectionDown:
r = '⮟'
if v == g.You {
r = '⮋'
}
case DirectionLeft:
r = '⮜'
if v == g.You {
r = '⮈'
}
}
}
case v == -1:
r = '×'
}
if colour && g.Cells[y][x] != -1 {
s.WriteString(colours[g.Cells[y][x]])
}
s.WriteRune(r)
if colour {
s.WriteString(colourReset)
}
}
if y < g.Height-1 {
s.WriteRune('\n')
}
}
return s.String()
}
func isJump(g *Game, action string) bool {
// Not a jump round? Too slow?
if (g.Players[g.You].stepCounter+1)%HolesEachStep != 0 {
return false
}
// Process action
switch action {
case ActionTurnLeft:
switch g.Players[g.You].Direction {
case DirectionLeft:
g.Players[g.You].Direction = DirectionDown
case DirectionRight:
g.Players[g.You].Direction = DirectionUp
case DirectionUp:
g.Players[g.You].Direction = DirectionLeft
case DirectionDown:
g.Players[g.You].Direction = DirectionRight
}
case ActionTurnRight:
switch g.Players[g.You].Direction {
case DirectionLeft:
g.Players[g.You].Direction = DirectionUp
case DirectionRight:
g.Players[g.You].Direction = DirectionDown
case DirectionUp:
g.Players[g.You].Direction = DirectionRight
case DirectionDown:
g.Players[g.You].Direction = DirectionLeft
}
case ActionFaster:
g.Players[g.You].Speed++
if g.Players[g.You].Speed > MaxSpeed {
return false
}
case ActionSlower:
g.Players[g.You].Speed--
if g.Players[g.You].Speed < 1 {
return false
}
case ActionNOOP:
// Do nothing
default:
log.Println("isJump:", "unknown action", action)
return false
}
if g.Players[g.You].Speed < HoleSpeed {
return false
}
// Check Jump
jumpFound := false
var dostep func(x, y int) (int, int)
switch g.Players[g.You].Direction {
case DirectionUp:
dostep = func(x, y int) (int, int) { return x, y - 1 }
case DirectionDown:
dostep = func(x, y int) (int, int) { return x, y + 1 }
case DirectionLeft:
dostep = func(x, y int) (int, int) { return x - 1, y }
case DirectionRight:
dostep = func(x, y int) (int, int) { return x + 1, y }
}
for s := 0; s < g.Players[g.You].Speed; s++ {
g.Players[g.You].X, g.Players[g.You].Y = dostep(g.Players[g.You].X, g.Players[g.You].Y)
if g.Players[g.You].X < 0 || g.Players[g.You].X >= g.Width || g.Players[g.You].Y < 0 || g.Players[g.You].Y >= g.Height {
return false
}
if s != 0 && s != g.Players[g.You].Speed-1 {
if g.Cells[g.Players[g.You].Y][g.Players[g.You].X] != 0 {
jumpFound = true
}
continue
}
if g.Cells[g.Players[g.You].Y][g.Players[g.You].X] != 0 {
return false
}
}
return jumpFound
}