-
-
Notifications
You must be signed in to change notification settings - Fork 708
robot-simulator: add to track #184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
apply plugin: "java" | ||
apply plugin: "eclipse" | ||
apply plugin: "idea" | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
testCompile "junit:junit:4.12" | ||
} | ||
test { | ||
testLogging { | ||
exceptionFormat = 'full' | ||
events = ["passed", "failed", "skipped"] | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../main/java/GridPosition.java |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../main/java/Orientation.java |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
final class Robot { | ||
|
||
private GridPosition gridPosition; | ||
|
||
private Orientation orientation; | ||
|
||
Robot(final GridPosition initialGridPosition, final Orientation initialOrientation) { | ||
this.gridPosition = initialGridPosition; | ||
this.orientation = initialOrientation; | ||
} | ||
|
||
GridPosition getGridPosition() { | ||
return gridPosition; | ||
} | ||
|
||
Orientation getOrientation() { | ||
return orientation; | ||
} | ||
|
||
Robot advance() { | ||
switch (orientation) { | ||
case NORTH: | ||
gridPosition = new GridPosition(gridPosition.x, gridPosition.y + 1); | ||
break; | ||
case EAST: | ||
gridPosition = new GridPosition(gridPosition.x + 1, gridPosition.y); | ||
break; | ||
case SOUTH: | ||
gridPosition = new GridPosition(gridPosition.x, gridPosition.y - 1); | ||
break; | ||
case WEST: | ||
gridPosition = new GridPosition(gridPosition.x - 1, gridPosition.y); | ||
break; | ||
} | ||
|
||
return this; | ||
} | ||
|
||
Robot simulate(final String instructions) { | ||
for (final char instruction : instructions.toCharArray()) { | ||
switch (instruction) { | ||
case 'A': | ||
advance(); | ||
break; | ||
case 'R': | ||
turnRight(); | ||
break; | ||
case 'L': | ||
turnLeft(); | ||
break; | ||
default: | ||
throw new IllegalArgumentException(String.format("Invalid instruction: '%s'", instruction)); | ||
} | ||
} | ||
|
||
return this; | ||
} | ||
|
||
Robot turnLeft() { | ||
final int newOrientationOrdinal = Math.floorMod(orientation.ordinal() - 1, Orientation.values().length); | ||
orientation = Orientation.values()[newOrientationOrdinal]; | ||
return this; | ||
} | ||
|
||
Robot turnRight() { | ||
final int newOrientationOrdinal = Math.floorMod(orientation.ordinal() + 1, Orientation.values().length); | ||
orientation = Orientation.values()[newOrientationOrdinal]; | ||
return this; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
final class GridPosition { | ||
|
||
final int x; | ||
|
||
final int y; | ||
|
||
GridPosition(final int x, final int y) { | ||
this.x = x; | ||
this.y = y; | ||
} | ||
|
||
/* | ||
* This equals method is of deliberately narrow scope (only allows comparison with another GridPosition) to increase | ||
* readability. In general, one should provide a full implementation of Object.equals(Object obj) and a | ||
* corresponding implementation of Object.hashCode(). See | ||
* | ||
* https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object) | ||
* | ||
* and | ||
* | ||
* https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#hashCode() | ||
* | ||
* for more information. | ||
*/ | ||
boolean equals(final GridPosition gridPosition) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you talk about the decision to overload instead of override There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's what a full implementation of @Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final GridPosition that = (GridPosition) o;
if (x != that.x) return false;
return y == that.y;
}
@Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
} Since this is a class supplied as part of the starter implementation, my thinking was that the above represented a lot of irrelevant detail a user might think they needed to understand in order to proceed with the problem. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Excellent point. Agreed. We also want to balance that with the principle that Exercism code should be exemplary. What do you think of some succinct comment above or within There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think your proposal is a good balance, and will take a stab at it locally with the expectation that we might iterate a couple of times to capture all that needs to be said! Related-ish: are reference solutions ever exposed to users? I ask in light of this comment (my emphasis):
I think that exposing users to exemplary reference solutions should be the norm; just wondering where Exercism as a whole stands on that front? |
||
return this.x == gridPosition.x && this.y == gridPosition.y; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
enum Orientation { | ||
|
||
NORTH, EAST, SOUTH, WEST | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
final class Robot { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this class be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't think it needs to be; the tests are located in the same package, so package-private access is sufficient for the class and it's exposed methods! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see whatcha doin' there. Cool! |
||
|
||
|
||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍