-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.go
122 lines (116 loc) · 4.01 KB
/
parser.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
package main
import (
"fmt"
"os"
"strconv"
"strings"
)
// ParseSettings takes a Settings object to modify,
// and a command tuple of the format "settings timebank 10000"
func ParseSettings(settings *Settings, command []string) {
switch command[1] {
case "timebank":
time, err := strconv.Atoi(command[2])
if err != nil {
fmt.Fprintln(os.Stderr, errorStr+"Unable to convert command argument to int. Error:", err)
}
(*settings).timebank = time
case "time_per_move":
time, err := strconv.Atoi(command[2])
if err != nil {
fmt.Fprintln(os.Stderr, errorStr+"Unable to convert command argument to int. Error:", err)
}
(*settings).timePerMove = time
case "player_names":
names := strings.Split(command[2], ",")
if len(names) != 2 {
fmt.Fprintln(os.Stderr, errorStr+"player_names was unable to parse into []string of length 2. Detail: names=", names)
}
(*settings).playerNames = names
case "your_bot":
(*settings).yourBot = command[2]
case "your_botid":
ID, err := strconv.Atoi(command[2])
if err != nil {
fmt.Fprintln(os.Stderr, errorStr+"Unable to parse bot id. Error: ", err, " Detail: ", command)
}
(*settings).yourBotID = ID
case "field_width":
width, err := strconv.Atoi(command[2])
if err != nil {
fmt.Fprintln(os.Stderr, errorStr+"Unable to parse width. Error: ", err, " Detail: ", command)
}
(*settings).fieldWidth = width
case "field_height":
height, err := strconv.Atoi(command[2])
if err != nil {
fmt.Fprintln(os.Stderr, errorStr+"Unable to parse height. Error: ", err, " Detail: ", command)
}
(*settings).fieldHeight = height
case "max_rounds":
rounds, err := strconv.Atoi(command[2])
if err != nil {
fmt.Fprintln(os.Stderr, errorStr+"Unable to parse max rounds. Error: ", err, " Detail: ", command)
}
(*settings).maxRounds = rounds
default:
fmt.Fprintln(os.Stderr, errorStr+"Unhandled settings field. Detail:", command)
}
}
// ParseUpdate takes a Settings object to modify,
// and a command tuple of the format "update game round 0"
func ParseUpdate(state *State, command []string) {
switch command[2] {
case "round":
round, err := strconv.Atoi(command[3])
if err != nil {
fmt.Fprintln(os.Stderr, errorStr+"Unable to parse round. Error:", err, "Detail: ", command)
}
(*state).round = round
case "field":
field1D := strings.Split(command[3], ",")
// If field has been initialized
if len((*state).field.GetBoard()) > 0 {
(*state).field.SetField(field1D)
} else {
fmt.Fprintln(os.Stderr, errorStr+"Trying to set field before initializing")
}
case "snippets":
snippets, err := strconv.Atoi(command[3])
if err != nil {
fmt.Fprintln(os.Stderr, errorStr+"Unable to parse snippets. Error:", err, "Detail: ", command)
}
if command[1] == "player0" {
(*state).players[0].snippets = snippets
} else if command[1] == "player1" {
(*state).players[1].snippets = snippets
} else {
fmt.Fprintln(os.Stderr, errorStr+"Unhandled player enountered in update. Player: ", command[1])
}
case "bombs":
bombs, err := strconv.Atoi(command[3])
if err != nil {
fmt.Fprintln(os.Stderr, errorStr+"Unable to parse bombs. Error:", err, "Detail: ", command)
}
if command[1] == "player0" {
(*state).players[0].bombs = bombs
} else if command[1] == "player1" {
(*state).players[1].bombs = bombs
} else {
fmt.Fprintln(os.Stderr, errorStr+"Unhandled player enountered in update. Player: ", command[1])
}
default:
fmt.Fprintln(os.Stderr, errorStr+"Unhandled update type. Detail:", command)
}
}
// ParseAction takes a State object to modify,
// and a command tuple of the format "action character t"
// returns either "character" or "move"
func ParseAction(state *State, command []string) (commandType string) {
timeRemaining, err := strconv.Atoi(command[2])
if err != nil {
fmt.Fprintln(os.Stderr, errorStr+"Unable to parse time remaining. Detail: ", command)
}
(*state).timeRemaining = timeRemaining
return command[1]
}