Sfoglia il codice sorgente

robot-simulator: add to track

Stuart Kent 9 anni fa
parent
commit
78295f4e69

+ 7
- 1
config.json Vedi File

@@ -47,7 +47,8 @@
47 47
     "largest-series-product",
48 48
     "queen-attack",
49 49
     "minesweeper",
50
-    "series"
50
+    "series",
51
+    "robot-simulator"
51 52
   ],
52 53
   "exercises": [
53 54
     {
@@ -269,6 +270,11 @@
269 270
       "slug": "series",
270 271
       "difficulty": 1,
271 272
       "topics": []
273
+    },
274
+    {
275
+      "slug": "robot-simulator",
276
+      "difficulty": 1,
277
+      "topics": []
272 278
     }
273 279
   ],
274 280
   "deprecated": [

+ 17
- 0
exercises/robot-simulator/build.gradle Vedi File

@@ -0,0 +1,17 @@
1
+apply plugin: "java"
2
+apply plugin: "eclipse"
3
+apply plugin: "idea"
4
+
5
+repositories {
6
+  mavenCentral()
7
+}
8
+
9
+dependencies {
10
+  testCompile "junit:junit:4.12"
11
+}
12
+test {
13
+  testLogging {
14
+    exceptionFormat = 'full'
15
+    events = ["passed", "failed", "skipped"]
16
+  }
17
+}

+ 1
- 0
exercises/robot-simulator/src/example/java/GridPosition.java Vedi File

@@ -0,0 +1 @@
1
+../../main/java/GridPosition.java

+ 1
- 0
exercises/robot-simulator/src/example/java/Orientation.java Vedi File

@@ -0,0 +1 @@
1
+../../main/java/Orientation.java

+ 71
- 0
exercises/robot-simulator/src/example/java/Robot.java Vedi File

@@ -0,0 +1,71 @@
1
+final class Robot {
2
+
3
+    private GridPosition gridPosition;
4
+
5
+    private Orientation orientation;
6
+
7
+    Robot(final GridPosition initialGridPosition, final Orientation initialOrientation) {
8
+        this.gridPosition = initialGridPosition;
9
+        this.orientation = initialOrientation;
10
+    }
11
+
12
+    GridPosition getGridPosition() {
13
+        return gridPosition;
14
+    }
15
+
16
+    Orientation getOrientation() {
17
+        return orientation;
18
+    }
19
+
20
+    Robot advance() {
21
+        switch (orientation) {
22
+            case NORTH:
23
+                gridPosition = new GridPosition(gridPosition.x, gridPosition.y + 1);
24
+                break;
25
+            case EAST:
26
+                gridPosition = new GridPosition(gridPosition.x + 1, gridPosition.y);
27
+                break;
28
+            case SOUTH:
29
+                gridPosition = new GridPosition(gridPosition.x, gridPosition.y - 1);
30
+                break;
31
+            case WEST:
32
+                gridPosition = new GridPosition(gridPosition.x - 1, gridPosition.y);
33
+                break;
34
+        }
35
+
36
+        return this;
37
+    }
38
+
39
+    Robot simulate(final String instructions) {
40
+        for (final char instruction : instructions.toCharArray()) {
41
+            switch (instruction) {
42
+                case 'A':
43
+                    advance();
44
+                    break;
45
+                case 'R':
46
+                    turnRight();
47
+                    break;
48
+                case 'L':
49
+                    turnLeft();
50
+                    break;
51
+                default:
52
+                    throw new IllegalArgumentException(String.format("Invalid instruction: '%s'", instruction));
53
+            }
54
+        }
55
+
56
+        return this;
57
+    }
58
+
59
+    Robot turnLeft() {
60
+        final int newOrientationOrdinal = Math.floorMod(orientation.ordinal() - 1, Orientation.values().length);
61
+        orientation = Orientation.values()[newOrientationOrdinal];
62
+        return this;
63
+    }
64
+
65
+    Robot turnRight() {
66
+        final int newOrientationOrdinal = Math.floorMod(orientation.ordinal() + 1, Orientation.values().length);
67
+        orientation = Orientation.values()[newOrientationOrdinal];
68
+        return this;
69
+    }
70
+
71
+}

+ 29
- 0
exercises/robot-simulator/src/main/java/GridPosition.java Vedi File

@@ -0,0 +1,29 @@
1
+final class GridPosition {
2
+
3
+    final int x;
4
+
5
+    final int y;
6
+
7
+    GridPosition(final int x, final int y) {
8
+        this.x = x;
9
+        this.y = y;
10
+    }
11
+
12
+    /*
13
+     * This equals method is of deliberately narrow scope (only allows comparison with another GridPosition) to increase
14
+     * readability. In general, one should provide a full implementation of Object.equals(Object obj) and a
15
+     * corresponding implementation of Object.hashCode(). See
16
+     *
17
+     * https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object)
18
+     *
19
+     * and
20
+     *
21
+     * https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#hashCode()
22
+     *
23
+     * for more information.
24
+     */
25
+    boolean equals(final GridPosition gridPosition) {
26
+        return this.x == gridPosition.x && this.y == gridPosition.y;
27
+    }
28
+
29
+}

+ 5
- 0
exercises/robot-simulator/src/main/java/Orientation.java Vedi File

@@ -0,0 +1,5 @@
1
+enum Orientation {
2
+
3
+    NORTH, EAST, SOUTH, WEST
4
+
5
+}

+ 5
- 0
exercises/robot-simulator/src/main/java/Robot.java Vedi File

@@ -0,0 +1,5 @@
1
+final class Robot {
2
+
3
+
4
+
5
+}

+ 244
- 0
exercises/robot-simulator/src/test/java/RobotTest.java Vedi File

@@ -0,0 +1,244 @@
1
+import org.junit.Ignore;
2
+import org.junit.Test;
3
+
4
+import static org.junit.Assert.assertTrue;
5
+
6
+public final class RobotTest {
7
+
8
+    @Test
9
+    public void testRobotIsCreatedWithCorrectInitialPosition() {
10
+        final GridPosition initialGridPosition = new GridPosition(0, 0);
11
+        final Robot robot = new Robot(initialGridPosition, Orientation.NORTH);
12
+
13
+        assertTrue(robot.getGridPosition().equals(initialGridPosition));
14
+    }
15
+
16
+    @Ignore
17
+    @Test
18
+    public void testRobotIsCreatedWithCorrectInitialOrientation() {
19
+        final Orientation initialOrientation = Orientation.NORTH;
20
+        final Robot robot = new Robot(new GridPosition(0, 0), initialOrientation);
21
+
22
+        assertTrue(robot.getOrientation().equals(initialOrientation));
23
+    }
24
+
25
+    @Ignore
26
+    @Test
27
+    public void testTurningRightDoesNotChangePosition() {
28
+        final GridPosition initialGridPosition = new GridPosition(0, 0);
29
+        final Robot robot = new Robot(initialGridPosition, Orientation.NORTH);
30
+
31
+        robot.turnRight();
32
+
33
+        assertTrue(robot.getGridPosition().equals(initialGridPosition));
34
+    }
35
+
36
+    @Ignore
37
+    @Test
38
+    public void testTurningRightCorrectlyChangesOrientationFromNorthToEast() {
39
+        final Robot robot = new Robot(new GridPosition(0, 0), Orientation.NORTH);
40
+
41
+        robot.turnRight();
42
+
43
+        final Orientation expectedOrientation = Orientation.EAST;
44
+
45
+        assertTrue(robot.getOrientation().equals(expectedOrientation));
46
+    }
47
+
48
+    @Ignore
49
+    @Test
50
+    public void testTurningRightCorrectlyChangesOrientationFromEastToSouth() {
51
+        final Robot robot = new Robot(new GridPosition(0, 0), Orientation.EAST);
52
+
53
+        robot.turnRight();
54
+
55
+        final Orientation expectedOrientation = Orientation.SOUTH;
56
+
57
+        assertTrue(robot.getOrientation().equals(expectedOrientation));
58
+    }
59
+
60
+    @Ignore
61
+    @Test
62
+    public void testTurningRightCorrectlyChangesOrientationFromSouthToWest() {
63
+        final Robot robot = new Robot(new GridPosition(0, 0), Orientation.SOUTH);
64
+
65
+        robot.turnRight();
66
+
67
+        final Orientation expectedOrientation = Orientation.WEST;
68
+
69
+        assertTrue(robot.getOrientation().equals(expectedOrientation));
70
+    }
71
+
72
+    @Ignore
73
+    @Test
74
+    public void testTurningRightCorrectlyChangesOrientationFromWestToNorth() {
75
+        final Robot robot = new Robot(new GridPosition(0, 0), Orientation.WEST);
76
+
77
+        robot.turnRight();
78
+
79
+        final Orientation expectedOrientation = Orientation.NORTH;
80
+
81
+        assertTrue(robot.getOrientation().equals(expectedOrientation));
82
+    }
83
+
84
+    @Ignore
85
+    @Test
86
+    public void testTurningLeftDoesNotChangePosition() {
87
+        final GridPosition initialGridPosition = new GridPosition(0, 0);
88
+        final Robot robot = new Robot(initialGridPosition, Orientation.NORTH);
89
+
90
+        robot.turnLeft();
91
+
92
+        assertTrue(robot.getGridPosition().equals(initialGridPosition));
93
+    }
94
+
95
+    @Ignore
96
+    @Test
97
+    public void testTurningLeftCorrectlyChangesOrientationFromNorthToWest() {
98
+        final Robot robot = new Robot(new GridPosition(0, 0), Orientation.NORTH);
99
+
100
+        robot.turnLeft();
101
+
102
+        final Orientation expectedOrientation = Orientation.WEST;
103
+
104
+        assertTrue(robot.getOrientation().equals(expectedOrientation));
105
+    }
106
+
107
+    @Ignore
108
+    @Test
109
+    public void testTurningLeftCorrectlyChangesOrientationFromWestToSouth() {
110
+        final Robot robot = new Robot(new GridPosition(0, 0), Orientation.WEST);
111
+
112
+        robot.turnLeft();
113
+
114
+        final Orientation expectedOrientation = Orientation.SOUTH;
115
+
116
+        assertTrue(robot.getOrientation().equals(expectedOrientation));
117
+    }
118
+
119
+    @Ignore
120
+    @Test
121
+    public void testTurningLeftCorrectlyChangesOrientationFromSouthToEast() {
122
+        final Robot robot = new Robot(new GridPosition(0, 0), Orientation.SOUTH);
123
+
124
+        robot.turnLeft();
125
+
126
+        final Orientation expectedOrientation = Orientation.EAST;
127
+
128
+        assertTrue(robot.getOrientation().equals(expectedOrientation));
129
+    }
130
+
131
+    @Ignore
132
+    @Test
133
+    public void testTurningLeftCorrectlyChangesOrientationFromEastToNorth() {
134
+        final Robot robot = new Robot(new GridPosition(0, 0), Orientation.EAST);
135
+
136
+        robot.turnLeft();
137
+
138
+        final Orientation expectedOrientation = Orientation.NORTH;
139
+
140
+        assertTrue(robot.getOrientation().equals(expectedOrientation));
141
+    }
142
+
143
+    @Ignore
144
+    @Test
145
+    public void testAdvancingDoesNotChangeOrientation() {
146
+        final Orientation initialOrientation = Orientation.NORTH;
147
+        final Robot robot = new Robot(new GridPosition(0, 0), initialOrientation);
148
+
149
+        robot.advance();
150
+
151
+        assertTrue(robot.getOrientation().equals(initialOrientation));
152
+    }
153
+
154
+    @Ignore
155
+    @Test
156
+    public void testAdvancingWhenFacingNorthIncreasesYCoordinateByOne() {
157
+        final Robot robot = new Robot(new GridPosition(0, 0), Orientation.NORTH);
158
+
159
+        robot.advance();
160
+
161
+        final GridPosition expectedGridPosition = new GridPosition(0, 1);
162
+
163
+        assertTrue(robot.getGridPosition().equals(expectedGridPosition));
164
+    }
165
+
166
+    @Ignore
167
+    @Test
168
+    public void testAdvancingWhenFacingSouthDecreasesYCoordinateByOne() {
169
+        final Robot robot = new Robot(new GridPosition(0, 0), Orientation.SOUTH);
170
+
171
+        robot.advance();
172
+
173
+        final GridPosition expectedGridPosition = new GridPosition(0, -1);
174
+
175
+        assertTrue(robot.getGridPosition().equals(expectedGridPosition));
176
+    }
177
+
178
+    @Ignore
179
+    @Test
180
+    public void testAdvancingWhenFacingEastIncreasesXCoordinateByOne() {
181
+        final Robot robot = new Robot(new GridPosition(0, 0), Orientation.EAST);
182
+
183
+        robot.advance();
184
+
185
+        final GridPosition expectedGridPosition = new GridPosition(1, 0);
186
+
187
+        assertTrue(robot.getGridPosition().equals(expectedGridPosition));
188
+    }
189
+
190
+    @Ignore
191
+    @Test
192
+    public void testAdvancingWhenFacingWestDecreasesXCoordinateByOne() {
193
+        final Robot robot = new Robot(new GridPosition(0, 0), Orientation.WEST);
194
+
195
+        robot.advance();
196
+
197
+        final GridPosition expectedGridPosition = new GridPosition(-1, 0);
198
+
199
+        assertTrue(robot.getGridPosition().equals(expectedGridPosition));
200
+    }
201
+
202
+    @Ignore
203
+    @Test
204
+    public void testInstructionsToMoveWestAndNorth() {
205
+        final Robot robot = new Robot(new GridPosition(0, 0), Orientation.NORTH);
206
+
207
+        robot.simulate("LAAARALA");
208
+
209
+        final GridPosition expectedGridPosition = new GridPosition(-4, 1);
210
+        final Orientation expectedOrientation = Orientation.WEST;
211
+
212
+        assertTrue(robot.getGridPosition().equals(expectedGridPosition));
213
+        assertTrue(robot.getOrientation().equals(expectedOrientation));
214
+    }
215
+
216
+    @Ignore
217
+    @Test
218
+    public void testInstructionsToMoveWestAndSouth() {
219
+        final Robot robot = new Robot(new GridPosition(2, -7), Orientation.EAST);
220
+
221
+        robot.simulate("RRAAAAALA");
222
+
223
+        final GridPosition expectedGridPosition = new GridPosition(-3, -8);
224
+        final Orientation expectedOrientation = Orientation.SOUTH;
225
+
226
+        assertTrue(robot.getGridPosition().equals(expectedGridPosition));
227
+        assertTrue(robot.getOrientation().equals(expectedOrientation));
228
+    }
229
+
230
+    @Ignore
231
+    @Test
232
+    public void testInstructionsToMoveEastAndNorth() {
233
+        final Robot robot = new Robot(new GridPosition(8, 4), Orientation.SOUTH);
234
+
235
+        robot.simulate("LAAARRRALLLL");
236
+
237
+        final GridPosition expectedGridPosition = new GridPosition(11, 5);
238
+        final Orientation expectedOrientation = Orientation.NORTH;
239
+
240
+        assertTrue(robot.getGridPosition().equals(expectedGridPosition));
241
+        assertTrue(robot.getOrientation().equals(expectedOrientation));
242
+    }
243
+
244
+}

+ 1
- 0
exercises/settings.gradle Vedi File

@@ -30,6 +30,7 @@ include 'queen-attack'
30 30
 include 'raindrops'
31 31
 include 'rna-transcription'
32 32
 include 'robot-name'
33
+include 'robot-simulator'
33 34
 include 'roman-numerals'
34 35
 include 'prime-factors'
35 36
 include 'scrabble-score'