-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrobot.go
208 lines (189 loc) · 4.65 KB
/
robot.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
package robot
//directions that things can be facing
const (
N = iota
E = iota
S = iota
W = iota
)
/*Action3 are a list of function that a robot can preform.*/
type Action3 struct {
Name string
ActionList []Action
}
/*Robot3 creats a robot for step 3 of the unit tests.*/
func Robot3(name, script string, actions chan Action3, log chan string) {
var actList []Action
cmd := make(chan Command)
acts := make(chan Action)
go Robot(cmd, acts)
for _, char := range script {
cmd <- Command(char)
if act := <-acts; act != nil {
actList = append(actList, act)
} else {
log <- "Robot " + name + " recieved an invalid command: " + string(char)
break
}
}
close(cmd)
actions <- Action3{name, actList}
}
//state state of a robot
type state struct {
pos *DirAt
active bool
}
/*Room3 runs the colision and orchestration of multiple robots.*/
func Room3(extent Rect, robots []Place, actions chan Action3, report chan []Place, log chan string) {
//Setup robot tracking
robotStates := make(map[string]*state)
for r, robot := range robots {
_, ok := robotStates[robot.Name]
switch {
case robot.Name == "":
log <- "A robot does not have a name"
case ok:
log <- "Robot " + robot.Name + " is listed twice"
case robotOnSquare(robot.DirAt.Pos, robotStates) != "":
log <- "Robot " + robot.Name + " was placed on an occupied space"
case !inRoom(robot.DirAt.Pos, extent):
log <- "Robot " + robot.Name + " was placed outside the room"
default:
robotStates[robot.Name] = &state{&(robots[r].DirAt), true}
continue
}
report <- robots
return
}
//Run the commands
clearState := getState()
for activeRobots(robotStates) {
robot := <-actions
robotState, ok := robotStates[robot.Name]
if !ok {
log <- "Robot " + robot.Name + " doesn't exist"
break
}
setState(*robotState.pos)
for _, action := range robot.ActionList {
action()
otherRobot := robotOnSquare(Pos{RU(X), RU(Y)}, robotStates)
switch {
case !inRoom(Pos{RU(X), RU(Y)}, extent):
log <- "Robot " + robot.Name + " bumped into the wall"
case otherRobot != robot.Name && otherRobot != "":
log <- "Robot " + robot.Name + " bumped into " + otherRobot
default:
*robotState.pos = getState()
}
setState(*robotState.pos)
}
robotStates[robot.Name].active = false
}
report <- robots
setState(clearState)
}
/*activeRobots checks if there is still an active robot.*/
func activeRobots(robots map[string]*state) bool {
for _, state := range robots {
if state.active {
return true
}
}
return false
}
/*robotOnSquare checks if there is a robot in a give position.*/
func robotOnSquare(square Pos, otherRobots map[string]*state) string {
for name, state := range otherRobots {
if square == state.pos.Pos {
return name
}
}
return ""
}
/*====================STEP 2====================*/
/*Action are function that a robot can preform.*/
type Action func()
/*Robot creats a robot for step 2.*/
func Robot(commands chan Command, actions chan Action) {
for command, ok := <-commands; ok; command, ok = <-commands {
switch command {
case 'A':
actions <- Advance
case 'L':
actions <- Left
case 'R':
actions <- Right
default:
actions <- nil
}
}
close(actions)
}
/*Room creates a room for a robot to operate in.*/
func Room(extent Rect, place DirAt, actions chan Action, places chan DirAt) {
cleanState := getState()
setState(place)
for action, ok := <-actions; ok; action, ok = <-actions {
oldState := getState()
action()
if !inRoom(Pos{RU(X), RU(Y)}, extent) {
setState(oldState)
}
}
places <- getState()
setState(cleanState)
}
/*inRoom checks if a position is in a room.*/
func inRoom(point Pos, room Rect) bool {
minX, minY := room.Min.Easting, room.Min.Northing
maxX, maxY := room.Max.Easting, room.Max.Northing
x, y := point.Easting, point.Northing
return minX <= x && x <= maxX && minY <= y && y <= maxY
}
/*setState sets the global variables.*/
func setState(state DirAt) {
Facing, X, Y = state.Dir, int(state.Pos.Easting), int(state.Pos.Northing)
}
/*getState gets the global variables.*/
func getState() DirAt {
return DirAt{Facing, Pos{RU(X), RU(Y)}}
}
/*====================STEP 1====================*/
/*String representaion heading as a string.*/
func (d Dir) String() string {
switch d {
case N:
return "north"
case E:
return "east"
case S:
return "south"
case W:
return "west"
default:
return ""
}
}
/*Advance moves the robot forward.*/
func Advance() {
switch Facing {
case N:
Y++
case S:
Y--
case E:
X++
case W:
X--
}
}
/*Right turns the robot to the right.*/
func Right() {
Facing = (Facing + 1) % 4
}
/*Left turns the robot to the left.*/
func Left() {
Facing = (Facing + 3) % 4
}