-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobot.go
More file actions
165 lines (151 loc) · 3.81 KB
/
robot.go
File metadata and controls
165 lines (151 loc) · 3.81 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
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
package gobot
import (
"fmt"
"errors"
)
// Robot stores details of the Grid (table) he is placed on, and other information
type Robot struct {
Name string
Icon string
Placed bool
Direction string
Grid Grid
CoordinateConverter Converter
}
// DecorateSquare returns a string containing an ASCII representation of this robot
func (r Robot) DecorateSquare() string {
var decoration string
switch r.Direction {
case "NORTH":
decoration = "⇑"
break
case "SOUTH":
decoration = "⇓"
break
case "EAST":
decoration = "⇒"
break
case "WEST":
decoration = "⇐"
break
}
return decoration + r.Icon
}
// IsValidDirection takes a string and returns true if this is a valid direction, and false if invalid
func (r *Robot) IsValidDirection(direction string) bool {
return direction == "NORTH" || direction == "SOUTH" || direction == "WEST" || direction == "EAST"
}
// HasValidDirection returns true if the robot currently has a valid direction, and false if invalid
func (r *Robot) HasValidDirection() bool {
return r.IsValidDirection(r.Direction)
}
// GetCoordinates returns the robots current X,Y coordinate from its (SW=0,0) perspective
func (r *Robot) GetCoordinates() (int, int, error) {
x, y, err := r.Grid.GetObjectCoordinates(r)
nx, ny := r.CoordinateConverter.FromGridXY(x, y)
return nx, ny, err
}
// HasValidCoordinates returns true if the robot has valid coordinates, and false if invalid
func (r *Robot) HasValidCoordinates() bool {
_, _, err := r.GetCoordinates()
if err != nil {
return false
}
return true
}
// IsPlaced returns true if the robot is currently placed on the table, and false if not
func (r *Robot) IsPlaced() bool {
if r.HasValidCoordinates() == false {
return false
}
return true
}
// Report produces a (string) report of the robots coordinates and direction, and an error if invalid direction or coordinates
func (r *Robot) Report() (string, error) {
x, y, err := r.GetCoordinates()
if err != nil {
return "", err
}
if r.HasValidDirection() == false {
return "", fmt.Errorf("Could not produce report, as %s has an invalid direction of %s", r.Name, r.Direction)
}
return fmt.Sprintf("%d,%d,%s", x, y, r.Direction), nil
}
// TurnLeft rotates robot by 90 degrees left
func (r *Robot) TurnLeft() error {
switch r.Direction {
case "NORTH":
r.Direction = "WEST"
break
case "SOUTH":
r.Direction = "EAST"
break
case "EAST":
r.Direction = "NORTH"
break
case "WEST":
r.Direction = "SOUTH"
break
default:
return errors.New("No direction set")
}
return nil
}
// TurnRight rotates robot by 90 degrees right
func (r *Robot) TurnRight() error {
switch r.Direction {
case "NORTH":
r.Direction = "EAST"
break
case "SOUTH":
r.Direction = "WEST"
break
case "EAST":
r.Direction = "SOUTH"
break
case "WEST":
r.Direction = "NORTH"
break
default:
return errors.New("No direction set")
}
return nil
}
// Move moves the robot by one in its current direction, if it wont fall
func (r *Robot) Move() error {
x, y, err := r.GetCoordinates()
nx, ny := r.CoordinateConverter.ToGridXY(x, y)
if err != nil {
return err
}
switch r.Direction {
case "NORTH":
ny = ny - 1
case "SOUTH":
ny = ny + 1
case "EAST":
nx = nx + 1
case "WEST":
nx = nx - 1
default:
return errors.New("No direction set. Can not move.")
}
err = r.Grid.MoveObject(nx, ny, r)
if err != nil {
return err
}
return nil
}
// Place positions the robot to an X/Y coordinate facing a particular direction
func (r *Robot) Place(x int, y int, direction string) error {
if r.IsValidDirection(direction) == false {
return fmt.Errorf("%s is an invalid direction", direction)
}
nx, ny := r.CoordinateConverter.ToGridXY(x, y)
err := r.Grid.MoveObject(nx, ny, r)
if err != nil {
return err
}
r.Direction = direction
return nil
}