private int x = 0;
private int y = 0;
private Direction direction = Direction.UP;
public Robot(Direction direction,
int x,
int y
){ this.direction = direction;
this.x = x;
this.y = y;
}
public Direction getDirection() {
return direction;
}
public int getX() {
// текущая координата X
return x;
}
public int getY() {
// текущая координата Y
return y;
}
public void turnLeft() {
// повернуться на 90 градусов против часовой стрелки
switch (direction) {
case UP:
direction = Direction.LEFT;
break;
case LEFT:
direction = Direction.DOWN;
break;
case DOWN:
direction = Direction.RIGHT;
break;
case RIGHT:
direction = Direction.UP;
break;
}
System.
out.
println("Direction: "+direction
); }
public void turnRight() {
// повернуться на 90 градусов по часовой стрелке
switch (direction) {
case UP:
direction = Direction.RIGHT;
break;
case LEFT:
direction = Direction.UP;
break;
case DOWN:
direction = Direction.LEFT;
break;
case RIGHT:
direction = Direction.DOWN;
break;
}
System.
out.
println("Direction: "+direction
); }
public void stepForward() {
// шаг в направлении взгляда
// за один шаг робот изменяет одну свою координату на единицу
switch (direction) {
case UP:
y++;
break;
case LEFT:
x--;
break;
case DOWN:
y--;
break;
case RIGHT:
x++;
break;
}
System.
out.
println("Moving in direction: "+direction
); System.
out.
println("(x;y) = (" + x
+ ";" + y
+ ")"); }
public enum Direction {
UP,
DOWN,
LEFT,
RIGHT
}
}
cHVibGljIGNsYXNzIFJvYm90IHsKCiAgICBwcml2YXRlIGludCB4ID0gMDsKICAgIHByaXZhdGUgaW50IHkgPSAwOwogICAgcHJpdmF0ZSBEaXJlY3Rpb24gZGlyZWN0aW9uID0gRGlyZWN0aW9uLlVQOwoKICAgIHB1YmxpYyBSb2JvdChEaXJlY3Rpb24gZGlyZWN0aW9uLCBpbnQgeCwgaW50IHkpewogICAgICAgIHRoaXMuZGlyZWN0aW9uID0gZGlyZWN0aW9uOwogICAgICAgIHRoaXMueCA9IHg7CiAgICAgICAgdGhpcy55ID0geTsKICAgIH0KCiAgICBwdWJsaWMgRGlyZWN0aW9uIGdldERpcmVjdGlvbigpIHsKICAgICAgICByZXR1cm4gZGlyZWN0aW9uOwogICAgfQoKICAgIHB1YmxpYyBpbnQgZ2V0WCgpIHsKICAgICAgICAvLyDRgtC10LrRg9GJ0LDRjyDQutC+0L7RgNC00LjQvdCw0YLQsCBYCiAgICAgICAgcmV0dXJuIHg7CiAgICB9CgogICAgcHVibGljIGludCBnZXRZKCkgewogICAgICAgIC8vINGC0LXQutGD0YnQsNGPINC60L7QvtGA0LTQuNC90LDRgtCwIFkKICAgICAgICByZXR1cm4geTsKICAgIH0KCiAgICBwdWJsaWMgdm9pZCB0dXJuTGVmdCgpIHsKICAgICAgICAvLyDQv9C+0LLQtdGA0L3Rg9GC0YzRgdGPINC90LAgOTAg0LPRgNCw0LTRg9GB0L7QsiDQv9GA0L7RgtC40LIg0YfQsNGB0L7QstC+0Lkg0YHRgtGA0LXQu9C60LgKICAgICAgICBzd2l0Y2ggKGRpcmVjdGlvbikgewogICAgICAgICAgICBjYXNlIFVQOgogICAgICAgICAgICAgICAgZGlyZWN0aW9uID0gRGlyZWN0aW9uLkxFRlQ7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgY2FzZSBMRUZUOgogICAgICAgICAgICAgICAgZGlyZWN0aW9uID0gRGlyZWN0aW9uLkRPV047CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgY2FzZSBET1dOOgogICAgICAgICAgICAgICAgZGlyZWN0aW9uID0gRGlyZWN0aW9uLlJJR0hUOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIGNhc2UgUklHSFQ6CiAgICAgICAgICAgICAgICBkaXJlY3Rpb24gPSBEaXJlY3Rpb24uVVA7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICB9CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJEaXJlY3Rpb246ICIrZGlyZWN0aW9uKTsKICAgIH0KCiAgICBwdWJsaWMgdm9pZCB0dXJuUmlnaHQoKSB7CiAgICAgICAgLy8g0L/QvtCy0LXRgNC90YPRgtGM0YHRjyDQvdCwIDkwINCz0YDQsNC00YPRgdC+0LIg0L/QviDRh9Cw0YHQvtCy0L7QuSDRgdGC0YDQtdC70LrQtQogICAgICAgIHN3aXRjaCAoZGlyZWN0aW9uKSB7CiAgICAgICAgICAgIGNhc2UgVVA6CiAgICAgICAgICAgICAgICBkaXJlY3Rpb24gPSBEaXJlY3Rpb24uUklHSFQ7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgY2FzZSBMRUZUOgogICAgICAgICAgICAgICAgZGlyZWN0aW9uID0gRGlyZWN0aW9uLlVQOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIGNhc2UgRE9XTjoKICAgICAgICAgICAgICAgIGRpcmVjdGlvbiA9IERpcmVjdGlvbi5MRUZUOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIGNhc2UgUklHSFQ6CiAgICAgICAgICAgICAgICBkaXJlY3Rpb24gPSBEaXJlY3Rpb24uRE9XTjsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgIH0KICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIkRpcmVjdGlvbjogIitkaXJlY3Rpb24pOwogICAgfQoKICAgIHB1YmxpYyB2b2lkIHN0ZXBGb3J3YXJkKCkgewogICAgICAgIC8vINGI0LDQsyDQsiDQvdCw0L/RgNCw0LLQu9C10L3QuNC4INCy0LfQs9C70Y/QtNCwCiAgICAgICAgLy8g0LfQsCDQvtC00LjQvSDRiNCw0LMg0YDQvtCx0L7RgiDQuNC30LzQtdC90Y/QtdGCINC+0LTQvdGDINGB0LLQvtGOINC60L7QvtGA0LTQuNC90LDRgtGDINC90LAg0LXQtNC40L3QuNGG0YMKICAgICAgICBzd2l0Y2ggKGRpcmVjdGlvbikgewogICAgICAgICAgICBjYXNlIFVQOgogICAgICAgICAgICAgICAgeSsrOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIGNhc2UgTEVGVDoKICAgICAgICAgICAgICAgIHgtLTsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICBjYXNlIERPV046CiAgICAgICAgICAgICAgICB5LS07CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgY2FzZSBSSUdIVDoKICAgICAgICAgICAgICAgIHgrKzsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgIH0KICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIk1vdmluZyBpbiBkaXJlY3Rpb246ICIrZGlyZWN0aW9uKTsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIih4O3kpID0gKCIgKyB4ICsgIjsiICsgeSArICIpIik7CiAgICB9CgogICAgcHVibGljIGVudW0gRGlyZWN0aW9uIHsKICAgICAgICBVUCwKICAgICAgICBET1dOLAogICAgICAgIExFRlQsCiAgICAgICAgUklHSFQKICAgIH0KICAgIAp9