-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.go
More file actions
63 lines (55 loc) · 1.23 KB
/
command.go
File metadata and controls
63 lines (55 loc) · 1.23 KB
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
package gobot
import (
"fmt"
"errors"
"strconv"
)
// Command represents a command for the robot to process
type Command struct {
Command string
Args []string
}
// Response represents the output from the command. Containing an Error, and words to Speak
type Response struct {
Error error
Speak string
}
// HandleCommand handles maps a Command input to a Robot function, executes it, and responds with a CommandResponse
func (r *Robot) HandleCommand(cmd Command) Response {
cr := Response{}
switch cmd.Command {
case "PLACE":
if len(cmd.Args) != 3 {
cr.Error = errors.New("Malformed place command")
return cr
}
x, _ := strconv.Atoi(cmd.Args[0])
y, _ := strconv.Atoi(cmd.Args[1])
err := r.Place(x, y, cmd.Args[2])
if err != nil {
cr.Error = err
return cr
}
r.Placed = true
return cr
}
if r.Placed == false {
cr.Error = fmt.Errorf("Must be PLACEd first")
return cr
}
switch cmd.Command {
case "MOVE":
cr.Error = r.Move()
case "LEFT":
cr.Error = r.TurnLeft()
case "RIGHT":
cr.Error = r.TurnRight()
case "REPORT":
report, err := r.Report()
cr.Speak = report
cr.Error = err
default:
cr.Speak = fmt.Sprintf("%s the gobot can not compute command: %s", r.Name, cmd.Command)
}
return cr
}