Skip to content

Commit b90577a

Browse files
authored
Merge pull request #357 from code-dot-org/molly/neighborhood-tracker-updates
Neighborhood Validation Updates
2 parents beda50f + 47fb268 commit b90577a

File tree

12 files changed

+104
-16
lines changed

12 files changed

+104
-16
lines changed

org-code-javabuilder/neighborhood/src/main/java/org/code/neighborhood/Painter.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,16 @@ public void takePaint() {
143143

144144
/** @return True if there is paint in the square where the painter is standing. */
145145
public boolean isOnPaint() {
146-
return this.grid.getSquare(this.xLocation, this.yLocation).hasColor();
146+
boolean isOnPaint = this.grid.getSquare(this.xLocation, this.yLocation).hasColor();
147+
this.sendBooleanMessage(NeighborhoodSignalKey.IS_ON_PAINT, isOnPaint);
148+
return isOnPaint;
147149
}
148150

149151
/** @return True if there is a paint bucket in the square where the painter is standing. */
150152
public boolean isOnBucket() {
151-
return this.grid.getSquare(this.xLocation, this.yLocation).containsPaint();
153+
boolean isOnBucket = this.grid.getSquare(this.xLocation, this.yLocation).containsPaint();
154+
this.sendBooleanMessage(NeighborhoodSignalKey.IS_ON_BUCKET, isOnBucket);
155+
return isOnBucket;
152156
}
153157

154158
/** @return True if the painter's personal bucket has paint in it. */
@@ -161,12 +165,14 @@ public boolean hasPaint() {
161165

162166
/** @return True if there is no barrier one square ahead in the requested direction. */
163167
public boolean canMove(String direction) {
164-
return this.isValidMovement(Direction.fromString(direction));
168+
boolean canMove = this.isValidMovement(Direction.fromString(direction));
169+
this.sendBooleanMessage(NeighborhoodSignalKey.CAN_MOVE, canMove);
170+
return canMove;
165171
}
166172

167173
/** @return True if there is no barrier one square ahead in the current direction. */
168174
public boolean canMove() {
169-
return this.isValidMovement(this.direction);
175+
return this.canMove(this.direction.toString());
170176
}
171177

172178
/** @return the color of the square where the painter is standing. */
@@ -297,6 +303,13 @@ private void sendOutputMessage(NeighborhoodSignalKey signalKey, HashMap<String,
297303
this.outputAdapter.sendMessage(new NeighborhoodSignalMessage(signalKey, details));
298304
}
299305

306+
private void sendBooleanMessage(NeighborhoodSignalKey signalKey, boolean result) {
307+
HashMap<String, String> details = this.getSignalDetails();
308+
String resultString = String.valueOf(result);
309+
details.put(BOOLEAN_RESULT, resultString);
310+
this.sendOutputMessage(signalKey, details);
311+
}
312+
300313
private void sendInitializationMessage() {
301314
HashMap<String, String> initDetails = this.getSignalDetails();
302315
initDetails.put(DIRECTION, this.direction.getDirectionString());

org-code-javabuilder/neighborhood/src/main/java/org/code/neighborhood/support/NeighborhoodSignalKey.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,11 @@ public enum NeighborhoodSignalKey {
2020
// Hide all paint buckets
2121
HIDE_BUCKETS,
2222
// Show all paint buckets
23-
SHOW_BUCKETS
23+
SHOW_BUCKETS,
24+
// isOnBucket was called (used for validation only)
25+
IS_ON_BUCKET,
26+
// isOnPaint was called (used for validation only)
27+
IS_ON_PAINT,
28+
// canMove was called (used for validation only)
29+
CAN_MOVE
2430
}

org-code-javabuilder/protocol/src/main/java/org/code/protocol/ClientMessageDetailKeys.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,6 @@ public class ClientMessageDetailKeys {
4141
public static final String DIRECTION = "direction";
4242
public static final String COLOR = "color";
4343
public static final String PAINT = "paint";
44+
// Used to report the result of calling a boolean method
45+
public static final String BOOLEAN_RESULT = "booleanResult";
4446
}

org-code-javabuilder/validation/src/main/java/org/code/validation/NeighborhoodActionType.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@ public enum NeighborhoodActionType {
1111
SHOW_PAINTER,
1212
TURN_LEFT,
1313
HIDE_BUCKETS,
14-
SHOW_BUCKETS
14+
SHOW_BUCKETS,
15+
CAN_MOVE,
16+
IS_ON_BUCKET,
17+
IS_ON_PAINT
1518
}
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package org.code.validation;
22

3-
/** User-facing class representing an (x, y) position. */
3+
/** User-facing class representing an (x, y) position and a direction. */
44
public class Position {
55
private final int x;
66
private final int y;
7+
private final String direction;
78

8-
public Position(int x, int y) {
9+
public Position(int x, int y, String direction) {
910
this.x = x;
1011
this.y = y;
12+
this.direction = direction;
1113
}
1214

1315
public int getX() {
@@ -17,4 +19,8 @@ public int getX() {
1719
public int getY() {
1820
return this.y;
1921
}
22+
23+
public String getDirection() {
24+
return this.direction;
25+
}
2026
}

org-code-javabuilder/validation/src/main/java/org/code/validation/support/NeighborhoodActionTypeMapper.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ public static NeighborhoodActionType convertNeighborhoodKeyToActionType(
3535
return NeighborhoodActionType.HIDE_BUCKETS;
3636
case SHOW_BUCKETS:
3737
return NeighborhoodActionType.SHOW_BUCKETS;
38+
case CAN_MOVE:
39+
return NeighborhoodActionType.CAN_MOVE;
40+
case IS_ON_BUCKET:
41+
return NeighborhoodActionType.IS_ON_BUCKET;
42+
case IS_ON_PAINT:
43+
return NeighborhoodActionType.IS_ON_PAINT;
3844
default:
3945
return null;
4046
}

org-code-javabuilder/validation/src/main/java/org/code/validation/support/NeighborhoodTracker.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ public void trackEvent(ClientMessage message) {
5050
final int x = message.getDetail().getInt(X);
5151
final int y = message.getDetail().getInt(Y);
5252
final int paint = message.getDetail().getInt(PAINT);
53-
final PainterTracker painterTracker = new PainterTracker(id, new Position(x, y), paint);
53+
final String direction = message.getDetail().getString(DIRECTION);
54+
final PainterTracker painterTracker =
55+
new PainterTracker(id, new Position(x, y, direction), paint);
5456
this.painterTrackers.put(id, painterTracker);
5557
return;
5658
}

org-code-javabuilder/validation/src/main/java/org/code/validation/support/PainterTracker.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@ public void trackEvent(PainterEvent event) {
3939
final int currentY = this.currentPosition.getY();
4040
switch (Direction.fromString(directionString)) {
4141
case NORTH:
42-
this.currentPosition = new Position(currentX, currentY - 1);
42+
this.currentPosition = new Position(currentX, currentY - 1, directionString);
4343
break;
4444
case EAST:
45-
this.currentPosition = new Position(currentX + 1, currentY);
45+
this.currentPosition = new Position(currentX + 1, currentY, directionString);
4646
break;
4747
case SOUTH:
48-
this.currentPosition = new Position(currentX, currentY + 1);
48+
this.currentPosition = new Position(currentX, currentY + 1, directionString);
4949
break;
5050
case WEST:
51-
this.currentPosition = new Position(currentX - 1, currentY);
51+
this.currentPosition = new Position(currentX - 1, currentY, directionString);
5252
break;
5353
}
5454
}

org-code-javabuilder/validation/src/test/java/org/code/validation/NeighborhoodLogTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ public void finalOutputContainsPaintReturnsCorrectlyForInvalidOutput() {
7979
}
8080

8181
private PainterLog createPainterLog(List<PainterEvent> events) {
82-
return new PainterLog("sampleId", new Position(0, 0), new Position(5, 5), 0, 5, events);
82+
return new PainterLog(
83+
"sampleId", new Position(0, 0, "east"), new Position(5, 5, "east"), 0, 5, events);
8384
}
8485

8586
private String[][] getSampleFinalOutput() {

org-code-javabuilder/validation/src/test/java/org/code/validation/PainterLogTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ public void setUp() {
2424
sampleEvents.add(new PainterEvent(NeighborhoodActionType.PAINT, null));
2525
sampleEvents.add(new PainterEvent(NeighborhoodActionType.TAKE_PAINT, null));
2626
unitUnderTest =
27-
new PainterLog("sampleId", new Position(0, 0), new Position(5, 5), 0, 5, sampleEvents);
27+
new PainterLog(
28+
"sampleId",
29+
new Position(0, 0, "east"),
30+
new Position(5, 5, "north"),
31+
0,
32+
5,
33+
sampleEvents);
2834
}
2935

3036
@Test

0 commit comments

Comments
 (0)