Skip to content

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

Merged
merged 1 commit into from
Nov 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
"largest-series-product",
"queen-attack",
"minesweeper",
"series"
"series",
"robot-simulator"
],
"exercises": [
{
Expand Down Expand Up @@ -269,6 +270,11 @@
"slug": "series",
"difficulty": 1,
"topics": []
},
{
"slug": "robot-simulator",
"difficulty": 1,
"topics": []
}
],
"deprecated": [
Expand Down
17 changes: 17 additions & 0 deletions exercises/robot-simulator/build.gradle
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"]
}
}
71 changes: 71 additions & 0 deletions exercises/robot-simulator/src/example/java/Robot.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;
}

}
29 changes: 29 additions & 0 deletions exercises/robot-simulator/src/main/java/GridPosition.java
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;
}

/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

* 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you talk about the decision to overload instead of override equals()? Not saying it's wrong, just want to talk it through.

Copy link
Contributor Author

@stkent stkent Nov 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's what a full implementation of equals and hashCode looks like:

@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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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 equals() that include a URL to an accessible description of the rationale behind keeping equals() and hashCode() in sync, semantically?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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):

The reference solution is named something with example or Example in the path.

The solution does not need to be particularly great code, it is only used to verify that the exercise is coherent.

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;
}

}
5 changes: 5 additions & 0 deletions exercises/robot-simulator/src/main/java/Orientation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
enum Orientation {

NORTH, EAST, SOUTH, WEST

}
5 changes: 5 additions & 0 deletions exercises/robot-simulator/src/main/java/Robot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
final class Robot {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this class be public?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see whatcha doin' there. Cool!




}
Loading