Skip to content
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

found the way home #1254

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
11 changes: 11 additions & 0 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package core.basesyntax;

public class Main {
public static void main(String[] args) {
Robot robot = new Robot(Direction.LEFT, -3, -4);
robot.turnLeft();
RobotRoute.moveRobot (robot, 2, 5);
System.out.println("Robot is located at position: (" + robot.getX() + ", " + robot.getY() + ")");
System.out.println("Robot's direction is: " + robot.getDirection());
}
}
1 change: 1 addition & 0 deletions src/main/java/core/basesyntax/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public int getY() {

public void turnLeft() {
switch (direction) {

case UP:
direction = Direction.LEFT;
break;
Expand Down
32 changes: 30 additions & 2 deletions src/main/java/core/basesyntax/RobotRoute.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
package core.basesyntax;

public class RobotRoute {
public void moveRobot(Robot robot, int toX, int toY) {
//write your solution here
public static void moveRobot(Robot robot, int toX, int toY) {
while (robot.getX() != toX) {
if (robot.getX() < toX) {
while (robot.getDirection() != Direction.RIGHT) {
robot.turnRight();
}

robot.stepForward();
} else {
while (robot.getDirection() != Direction.LEFT) {
robot.turnRight();
}
robot.stepForward();
}
}
while (robot.getY() != toY) {
if (robot.getY() < toY) {
while (robot.getDirection() != Direction.UP) {
robot.turnRight();
}
robot.stepForward();
} else {
while (robot.getDirection() != Direction.DOWN) {
robot.turnRight();
}
robot.stepForward();
}
}
}
}


Loading