File tree Expand file tree Collapse file tree 1 file changed +65
-2
lines changed Expand file tree Collapse file tree 1 file changed +65
-2
lines changed Original file line number Diff line number Diff line change 1
1
package judge_route_circle
2
2
3
- func judgeCircle (moves string ) bool {
4
- var result bool
3
+ import (
4
+ "fmt"
5
+ "strings"
6
+ )
5
7
8
+ type Robot struct {
9
+ position Position
10
+ }
11
+
12
+ type Position struct {
13
+ x int
14
+ y int
15
+ }
16
+
17
+ func (r * Robot ) moveRight () {
18
+ r .position .y ++
19
+
20
+ }
21
+
22
+ func (r * Robot ) moveLeft () {
23
+ r .position .y --
24
+
25
+ }
26
+
27
+ func (r * Robot ) moveUp () {
28
+ r .position .x ++
29
+
30
+ }
31
+
32
+ func (r * Robot ) moveDown () {
33
+ r .position .x --
34
+
35
+ }
36
+
37
+ func (r * Robot ) Move (moves string ) {
38
+
39
+ for _ , step := range moves {
40
+ switch strings .ToUpper (string (step )) {
41
+ case "R" :
42
+ r .moveRight ()
43
+ case "L" :
44
+ r .moveLeft ()
45
+ case "U" :
46
+ r .moveUp ()
47
+ case "D" :
48
+ r .moveDown ()
49
+ default :
50
+ fmt .Printf ("Wrong movement!" )
51
+ }
52
+ }
53
+
54
+ }
55
+
56
+ func judgeCircle (moves string ) (result bool ) {
57
+
58
+ robot := Robot {
59
+ position : Position {
60
+ x : 0 ,
61
+ y : 0 ,
62
+ },
63
+ }
64
+
65
+ robot .Move (moves )
66
+ if robot .position .x == 0 && robot .position .y == 0 {
67
+ result = true
68
+ }
6
69
return result
7
70
8
71
}
You can’t perform that action at this time.
0 commit comments